aboutsummaryrefslogtreecommitdiff
path: root/src/sequences/buffer.rs
blob: 12d00a6e5179ce69009e950a41ca8415ce851f6c (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
sequence!(
    /// Switches to the alternate buffer.
    ///
    /// Use the [`SwitchBufferToNormal`](struct.SwitchBufferToNormal.html) sequence to switch
    /// back to the normal buffer.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::{SwitchBufferToAlternate, SwitchBufferToNormal};
    ///
    /// let mut stdout = stdout();
    /// write!(stdout, "{}", SwitchBufferToAlternate);
    /// // Your app on alternate screen
    /// write!(stdout, "{}", SwitchBufferToNormal);
    /// ```
    struct SwitchBufferToAlternate => csi!("?1049h")
);

sequence!(
    /// Switches to the normal buffer.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::{SwitchBufferToAlternate, SwitchBufferToNormal};
    ///
    /// let mut stdout = stdout();
    /// write!(stdout, "{}", SwitchBufferToAlternate);
    /// // Your app on alternate screen
    /// write!(stdout, "{}", SwitchBufferToNormal);
    /// ```
    struct SwitchBufferToNormal => csi!("?1049l")
);

sequence!(
    /// Scrolls up by the given number of rows.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::ScrollBufferUp;
    ///
    /// let mut stdout = stdout();
    /// // Scroll up by 5 lines
    /// write!(stdout, "{}", ScrollBufferUp(5));
    /// ```
    struct ScrollBufferUp(u16) =>
    |this, f| write!(f, csi!("{}S"), this.0)
);

sequence!(
    /// Scrolls down by the given number of rows.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::ScrollBufferDown;
    ///
    /// let mut stdout = stdout();
    /// // Scroll down by 10 lines
    /// write!(stdout, "{}", ScrollBufferDown(10));
    /// ```
    struct ScrollBufferDown(u16) =>
    |this, f| write!(f, csi!("{}T"), this.0)
);

sequence!(
    /// Clears part of the line.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::ClearLine;
    ///
    /// let mut stdout = stdout();
    /// // Clear the whole line
    /// write!(stdout, "{}", ClearLine::All);
    /// ```
    enum ClearLine {
        /// Clears from the cursor position to end of the line.
        Right => csi!("K"),
        /// Clears from the cursor position to beginning of the line.
        Left => csi!("1K"),
        /// Clears the whole line.
        All => csi!("2K"),
    }
);

sequence!(
    /// Clears part of the buffer.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::ClearBuffer;
    ///
    /// let mut stdout = stdout();
    /// // Clear the entire buffer
    /// write!(stdout, "{}", ClearBuffer::All);
    /// ```
    enum ClearBuffer {
        /// Clears from the cursor position to end of the screen.
        Below => csi!("J"),
        /// Clears from the cursor position to beginning of the screen.
        Above => csi!("1J"),
        /// Clears the entire buffer.
        All => csi!("2J"),
        /// Clears the entire buffer and all saved lines in the scrollback buffer.
        SavedLines => csi!("3J"),
    }
);

#[cfg(test)]
test_sequences!(
    switch_buffer_to_alternate(
        SwitchBufferToAlternate => "\x1B[?1049h",
    ),
    switch_buffer_to_main(
        SwitchBufferToNormal => "\x1B[?1049l",
    ),
    scroll_buffer_up(
        ScrollBufferUp(10) => "\x1B[10S",
    ),
    scroll_buffer_down(
        ScrollBufferDown(10) => "\x1B[10T",
    ),
    clear_line(
        ClearLine::Right => "\x1B[K",
        ClearLine::Left => "\x1B[1K",
        ClearLine::All => "\x1B[2K",
    ),
    clear_buffer(
        ClearBuffer::Below => "\x1B[J",
        ClearBuffer::Above => "\x1B[1J",
        ClearBuffer::All => "\x1B[2J",
        ClearBuffer::SavedLines => "\x1B[3J",
    ),
);