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
use std::cmp::{self, Ordering};
pub enum Pos {
Back,
Front,
Both,
}
impl Pos {
fn num(&self) -> u32 {
match *self {
Pos::Back => 0,
Pos::Front => 1,
Pos::Both => 2,
}
}
}
pub fn move_cursor(i: i32, j: i32) -> String {
let code0 = match i.cmp(&0) {
Ordering::Less => format!("\x1B[{}B", -i),
Ordering::Equal => format!(""),
Ordering::Greater => format!("\x1B[{}A", i),
};
let code1 = match j.cmp(&0) {
Ordering::Less => format!("\x1B[{}D", -j),
Ordering::Equal => format!(""),
Ordering::Greater => format!("\x1B[{}C", j),
};
format!("{}{}", code0, code1)
}
pub fn move_line(n: i32) -> String {
match n.cmp(&0) {
Ordering::Less => format!("\x1B[{}F", -n),
Ordering::Equal => format!(""),
Ordering::Greater => format!("\x1B[{}E", n),
}
}
pub fn set_column(n: u32) -> String {
format!("\x1B[{}G", cmp::max(1, n))
}
pub fn set_position(i: u32, j: u32) -> String {
format!("\x1B[{};{}H", cmp::max(1, i), cmp::max(1, j))
}
pub fn clear_display(pos: Pos) -> String {
format!("\x1B[{}J", pos.num())
}
pub fn clear_line(pos: Pos) -> String {
format!("\x1B[{}K", pos.num())
}
pub fn scroll(n: i32) -> String {
match n.cmp(&0) {
Ordering::Less => format!("\x1B[{}T", -n),
Ordering::Equal => format!(""),
Ordering::Greater => format!("\x1B[{}S", n),
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test {
($name: ident; $ctrl: expr => $result: expr) => {
#[test]
fn $name() {
assert_eq!($result, $ctrl)
}
};
}
test!(move_cursor0; move_cursor(2, -4) => "\x1B[2A\x1B[4D");
test!(move_cursor1; move_cursor(-2, 0) => "\x1B[2B");
test!(move_cursor2; move_cursor(0, 6) => "\x1B[6C");
test!(move_cursor3; move_cursor(0, 0) => "");
test!(move_line0; move_line(4) => "\x1B[4E");
test!(move_line1; move_line(-4) => "\x1B[4F");
test!(set_column0; set_column(3) => "\x1B[3G");
test!(set_column1; set_column(0) => "\x1B[1G");
test!(set_position0; set_position(3, 5) => "\x1B[3;5H");
test!(set_position1; set_position(0, 0) => "\x1B[1;1H");
test!(clear_display0; clear_display(Pos::Back) => "\x1B[0J");
test!(clear_display1; clear_display(Pos::Front) => "\x1B[1J");
test!(clear_display2; clear_display(Pos::Both) => "\x1B[2J");
test!(clear_line0; clear_line(Pos::Back) => "\x1B[0K");
test!(clear_line1; clear_line(Pos::Front) => "\x1B[1K");
test!(clear_line2; clear_line(Pos::Both) => "\x1B[2K");
test!(scroll0; scroll(7) => "\x1B[7S");
test!(scroll1; scroll(-5) => "\x1B[5T");
}