aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/res/osc94.rs
blob: ea8538d7fd684de5533471fa280a994f1b24b390 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::osc94::{OSC94Guard, OSC94State};

/// Process `OSC 9;4` status.
///
/// Provides support for the `OSC 9;4` protocol. You can inject it into the execution flow
/// through Mingling's resource injection system, and use it to control your process state.
///
/// Typically, `ResOSC94` is registered via `OSC94Setup`, and then injected into functions
/// through Mingling's resource injection system.
///
/// # Registration
///
/// Before use, the `OSC94Setup` must be registered with the program:
///
/// ```
/// # use mingling::MockProgramCollect as ThisProgram;
/// use mingling::setup::OSC94Setup;
/// use mingling::Program;
///
/// let mut program = Program::<ThisProgram>::new();
/// program.with_setup(OSC94Setup);
/// ```
///
/// # Example
///
/// ```
/// use mingling::res::ResOSC94;
/// use mingling::osc94::OSC94State;
///
/// let osc94 = ResOSC94::default();
/// let mut guard = osc94.get_mut();
///
/// guard.set_progress(0.5);
/// assert_eq!(guard.state(), OSC94State::Normal(0.5));
/// ```
#[derive(Debug, Default, Clone, Copy)]
pub struct ResOSC94 {
    pub(crate) is_support: bool,
}

impl ResOSC94 {
    /// Get a guard for modifying progress.
    ///
    /// The returned [`OSC94Guard`] allows you to set the process state and progress.
    /// If the current environment supports the `OSC 9;4` protocol, state changes will
    /// be sent to the terminal in real time.
    ///
    /// # Returns
    ///
    /// Returns an [`OSC94Guard`] with an initial state of [`OSC94State::Clean`].
    ///
    /// # Example
    ///
    /// ```
    /// use mingling::res::ResOSC94;
    /// use mingling::osc94::OSC94State;
    ///
    /// let osc94 = ResOSC94::default();
    /// let guard = osc94.get_mut();
    /// assert_eq!(guard.state(), OSC94State::Clean);
    /// ```
    #[must_use]
    pub const fn get_mut(&self) -> OSC94Guard {
        OSC94Guard {
            is_support: self.is_support,
            msg: OSC94State::Clean,
        }
    }
}