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
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::{color, cursor};
use unicode_segmentation::UnicodeSegmentation;
use std::io;
use std::io::Write;
use std::{thread, time};
pub fn select_from_list(list: &[String], selected: Option<usize>) -> usize {
let mut selection = selected.unwrap_or(0);
let selected = get_color();
let color = color::Fg(selected.as_ref());
let mut stdout = io::stdout()
.into_raw_mode()
.expect("Failed to get std out raw mode");
let mut stdin = termion::async_stdin().keys();
write!(stdout, "\x1B[?25l",).expect("Failed to write hide cursor");
loop {
write_list(&list, selection, color);
let input = stdin.next();
if let Some(Ok(key)) = input {
match key {
termion::event::Key::Esc => break,
termion::event::Key::Char('q') => break,
termion::event::Key::Char('\n') => break,
termion::event::Key::Up => {
if selection > 0 {
selection -= 1;
}
}
termion::event::Key::Down => {
if selection < (list.len() - 1) {
selection += 1;
}
}
_ => {}
}
}
clear_list(&list);
thread::sleep(time::Duration::from_millis(50));
}
write!(stdout, "\x1B[?25h",).expect("Failed to write show cursor");
selection
}
pub fn write_column(first: String, second: String, offset: Option<usize>) {
let offset = offset.unwrap_or(20);
let diff = offset - first.graphemes(true).count();
let mut stdout = io::stdout()
.into_raw_mode()
.expect("Failed to get std out raw mode");
write!(stdout, "\x1B[{}C{}", -1, first).expect("Failed to write first column");
write!(stdout, "\x1B[{}C{}\n\r", diff, second).expect("Failed to write second column");
}
fn write_list(list: &[String], selection: usize, color: color::Fg<&dyn termion::color::Color>) {
let mut stdout = io::stdout()
.into_raw_mode()
.expect("Failed to get std out raw mode");
for (i, item) in list.iter().enumerate() {
if i == selection {
write!(stdout, "{}{}\n\r", color, item).expect("Failed to write list");
} else {
write!(stdout, "{}{}\n\r", color::Fg(color::Reset), item).expect("Failed to write list");
}
}
write!(stdout, "{}", color::Fg(color::Reset)).expect("Failed to write reset color");
}
fn clear_list(list: &[String]) {
let mut stdout = io::stdout()
.into_raw_mode()
.expect("Failed to get std out raw mode");
for _ in list {
write!(stdout, "{}", cursor::Up(1)).expect("Failed to write cursor up");
}
}
fn get_color() -> Box<dyn color::Color> {
let conf = crate::config::get_gut_config();
let selected = match conf.get("color") {
Some(color) => color.to_string(),
None => "Red".to_string(),
};
if selected.contains("Black") {
return Box::new(color::Black);
} else if selected.contains("Red") {
return Box::new(color::Red);
} else if selected.contains("Green") {
return Box::new(color::Green);
} else if selected.contains("Yellow") {
return Box::new(color::Yellow);
} else if selected.contains("Blue") {
return Box::new(color::Blue);
} else if selected.contains("Magenta") {
return Box::new(color::Magenta);
} else if selected.contains("Cyan") {
return Box::new(color::Cyan);
} else if selected.contains("White") {
return Box::new(color::White);
} else {
return Box::new(color::Red);
}
}