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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
use std::io::{Stdout, Write, BufWriter};
use std::sync::{Arc, Mutex};
use termion;
use termion::screen::AlternateScreen;
use termion::input::MouseTerminal;
use termion::raw::{IntoRawMode, RawTerminal};
use parse_ansi::parse_bytes;
use crate::fail::HResult;
pub type TermMode = AlternateScreen<MouseTerminal<RawTerminal<BufWriter<Stdout>>>>;
#[derive(Clone)]
pub struct Screen {
screen: Arc<Mutex<Option<TermMode>>>
}
impl Screen {
pub fn new() -> HResult<Screen> {
let screen = BufWriter::new(std::io::stdout()).into_raw_mode()?;
let mut screen = MouseTerminal::from(screen);
let mut screen = AlternateScreen::from(screen);
screen.cursor_hide()?;
Ok(Screen {
screen: Arc::new(Mutex::new(Some(screen)))
})
}
pub fn drop_screen(&mut self) {
self.cursor_show();
self.to_main_screen();
self.screen = Arc::new(Mutex::new(None));
// Terminal stays fucked without this. Why?
std::process::Command::new("reset").arg("-I").spawn();
}
pub fn reset_screen(&mut self) -> HResult<()> {
let screen = Screen::new()?.screen.lock()?.take()?;
*self.screen.lock()? = Some(screen);
Ok(())
}
}
impl Write for Screen {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.screen.lock().unwrap().as_mut().unwrap().write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.screen.lock().unwrap().as_mut().unwrap().flush()
}
}
pub trait ScreenExt: Write {
fn cursor_hide(&mut self) -> HResult<()> {
write!(self, "{}", termion::cursor::Hide)?;
self.flush()?;
Ok(())
}
fn cursor_show(&mut self) -> HResult<()> {
write!(self, "{}", termion::cursor::Show)?;
self.flush()?;
Ok(())
}
fn reset(&mut self) -> HResult<()> {
write!(self, "{}", termion::style::Reset)?;
Ok(())
}
fn clear(&mut self) -> HResult<()> {
write!(self, "{}", termion::clear::All)?;
Ok(())
}
fn write_str(&mut self, str: &str) -> HResult<()> {
write!(self, "{}", str)?;
Ok(())
}
fn goto_xy(&mut self, x: usize, y: usize) -> HResult<()> {
let x = x as u16;
let y = y as u16;
write!(self, "{}", goto_xy(x + 1, y + 1))?;
Ok(())
}
fn xsize(&self) -> HResult<usize> {
let (xsize, _) = termion::terminal_size()?;
Ok((xsize - 1) as usize)
}
fn ysize(&self) -> HResult<usize> {
let (_, ysize) = termion::terminal_size()?;
Ok((ysize - 1) as usize)
}
fn set_title(&mut self, title: &str) -> HResult<()> {
write!(self, "\x1b]2;hunter: {}", title)?;
write!(self, "\x1bkhunter: {}\x1b\\", title)?;
Ok(())
}
fn to_main_screen(&mut self) -> HResult<()> {
write!(self, "{}", termion::screen::ToMainScreen)?;
self.flush()?;
Ok(())
}
}
impl ScreenExt for Screen {}
impl ScreenExt for TermMode {}
pub fn xsize() -> u16 {
let (xsize, _) = termion::terminal_size().unwrap();
xsize
}
pub fn ysize() -> u16 {
let (_, ysize) = termion::terminal_size().unwrap();
ysize
}
pub fn sized_string(string: &str, xsize: u16) -> String {
string.chars().fold("".to_string(), |acc, ch| {
let width: usize = unicode_width::UnicodeWidthStr::width_cjk(acc.as_str());
if width + 1 >= xsize as usize {
acc
} else {
acc + &ch.to_string()
}
})
}
fn is_ansi(ansi_pos: &Vec<(usize, usize)>, char_pos: &usize) -> bool {
ansi_pos.iter().fold(false, |is_ansi, (start, end)| {
if char_pos >= start && char_pos <= end {
true
} else { is_ansi }
})
}
fn ansi_len_at(ansi_pos: &Vec<(usize, usize)>, char_pos: &usize) -> usize {
ansi_pos.iter().fold(0, |len, (start, end)| {
if char_pos >= end {
len + (end-start)
} else { len }
})
}
pub fn sized_string_u(string: &str, xsize: usize) -> String {
let ansi_pos = parse_bytes(string.as_bytes()).map(|m| {
(m.start(), m.end())
}).collect();
let sized = string.chars().enumerate().fold("".to_string(), |acc, (i, ch)| {
let width: usize = unicode_width::UnicodeWidthStr::width_cjk(acc.as_str());
let ansi_len = ansi_len_at(&ansi_pos, &i);
if width >= xsize as usize + ansi_len {
acc
} else {
acc + &ch.to_string()
}
});
let ansi_len = ansi_len_at(&ansi_pos, &(sized.len().saturating_sub(1)));
let padded = format!("{:padding$}", sized, padding=xsize + ansi_len + 1);
padded
}
// Do these as constants
pub fn highlight_color() -> String {
format!(
"{}",
termion::color::Fg(termion::color::LightGreen),
//termion::color::Bg(termion::color::Black)
)
}
pub fn normal_color() -> String {
format!(
"{}",
termion::color::Fg(termion::color::White),
//termion::color::Bg(termion::color::Black)
)
}
pub fn color_red() -> String {
format!("{}", termion::color::Fg(termion::color::Red))
}
pub fn color_yellow() -> String {
format!("{}", termion::color::Fg(termion::color::Yellow))
}
pub fn color_green() -> String {
format!("{}", termion::color::Fg(termion::color::Green))
}
pub fn from_lscolor(color: &lscolors::Color) -> String {
match color {
lscolors::Color::Black => format!("{}", termion::color::Fg(termion::color::Black)),
lscolors::Color::Red => format!("{}", termion::color::Fg(termion::color::Red)),
lscolors::Color::Green => format!("{}", termion::color::Fg(termion::color::Green)),
lscolors::Color::Yellow => format!("{}", termion::color::Fg(termion::color::Yellow)),
lscolors::Color::Blue => format!("{}", termion::color::Fg(termion::color::Blue)),
lscolors::Color::Magenta => format!("{}", termion::color::Fg(termion::color::Magenta)),
lscolors::Color::Cyan => format!("{}", termion::color::Fg(termion::color::Cyan)),
lscolors::Color::White => format!("{}", termion::color::Fg(termion::color::White)),
_ => format!("{}", normal_color()),
}
}
// pub fn cursor_left(n: u16) -> String {
// format!("{}", termion::cursor::Left(n))
// }
pub fn gotoy(y: u16) -> String {
format!("{}", termion::cursor::Goto(1, y))
}
|