toiletcli/
escapes.rs

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Most common escapes to manipulate terminals.
//!
//! All enums implement [`Display`] trait.

use std::fmt::Display;

const ESC: &str = "\u{001b}";

/// Terminal manipulation.
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum System<'a>
{
  ResetState,
  SaveState,
  RestoreState,
  SetTitle(&'a str),
}

impl<'a> System<'a>
{
  fn code(&self) -> String
  {
    match self {
      System::ResetState => "c".to_string(),
      System::SaveState => "7".to_string(),
      System::RestoreState => "8".to_string(),
      System::SetTitle(value) => format!("]0;{}\u{0007}", value),
    }
  }
}

const CSI: &str = "\u{001b}[";

impl<'a> Display for System<'a>
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
  {
    if cfg!(feature = "mock_codes") {
      write!(f, "{{code {}}}", self.code())
    } else {
      write!(f, "{}{}", ESC, self.code())
    }
  }
}

/// Cursor behavior.
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Cursor
{
  Hide,
  Show,
  /// Save cursor position (SCO).
  Save,
  /// Restore cursor position (SCO).
  Restore,
  /// Position 0, 0.
  Reset,
  Up(u32),
  Down(u32),
  Right(u32),
  Left(u32),
  /// Move to column N.
  Column(u8),
  /// Set absolute position.
  Goto(u32, u32),
}

impl Cursor
{
  fn code(&self) -> String
  {
    match self {
      Cursor::Hide => "?25l".to_string(),
      Cursor::Show => "?25h".to_string(),
      Cursor::Save => "s".to_string(),
      Cursor::Restore => "u".to_string(),
      Cursor::Reset => "H".to_string(),
      Cursor::Up(value) => format!("{}A", value),
      Cursor::Down(value) => format!("{}B", value),
      Cursor::Right(value) => format!("{}C", value),
      Cursor::Left(value) => format!("{}D", value),
      Cursor::Column(value) => format!("{}G", value),
      Cursor::Goto(x, y) => format!("{};{}H", x, y),
    }
  }
}

impl Display for Cursor
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
  {
    if cfg!(feature = "mock_codes") {
      write!(f, "{{code {}}}", self.code())
    } else {
      write!(f, "{}{}", CSI, self.code())
    }
  }
}

/// Cursor movement to beginning of lines.
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Line
{
  /// First character of the current line.
  Home,
  Up(u32),
  Down(u32),
}

impl Line
{
  fn code(&self) -> String
  {
    match self {
      Line::Home => "0G".to_string(),
      Line::Up(value) => format!("{}E", value),
      Line::Down(value) => format!("{}F", value),
    }
  }
}

impl Display for Line
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
  {
    if cfg!(feature = "mock_codes") {
      write!(f, "{{code {}}}", self.code())
    } else {
      write!(f, "{}{}", CSI, self.code())
    }
  }
}

/// Symbol erasing.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Erase
{
  /// Insert N blank characters.
  After(u32),
  Screen,
  /// Whole screen, including scroll buffer.
  WholeScreen,
  /// Everything after.
  AllAfter,
  /// Everything before.
  AllBefore,
  Line,
  /// Everything after in a line.
  LineAfter,
  /// Everything before in a line.
  LineBefore,
}

impl Erase
{
  fn code(&self) -> String
  {
    match self {
      Erase::After(value) => format!("{}@", value),
      Erase::Screen => "2J".to_string(),
      Erase::WholeScreen => "3J".to_string(),
      Erase::AllAfter => "0J".to_string(),
      Erase::AllBefore => "1J".to_string(),
      Erase::Line => "2K".to_string(),
      Erase::LineAfter => "0K".to_string(),
      Erase::LineBefore => "1K".to_string(),
    }
  }
}

impl Display for Erase
{
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
  {
    if cfg!(feature = "mock_codes") {
      write!(f, "{{code {}}}", self.code())
    } else {
      write!(f, "{}{}", CSI, self.code())
    }
  }
}