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
|
//! A window is an abstraction for the UI. Buffers (like [Threads]) draw "to" windows.
use std::io::Write;
use termion::{color, cursor};
use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone, Copy)]
pub struct Area {
pub x: u16,
pub y: u16,
pub w: u16,
pub h: u16,
}
pub enum Line {
Normal(String),
Highlight(String),
}
pub fn truncate_dots(s: &str, to: usize) -> String {
//TODO Don't do this every tick
let graphemes = s.graphemes(true).collect::<Vec<_>>();
if graphemes.len() >= to {
format!("{}...", graphemes.iter().take(to - 3).fold(String::new(), |acc, s| format!("{}{}", acc, s)))
} else {
graphemes.join("")
}
}
pub struct Statusbar(String);
impl Statusbar {
pub fn new(s: String) -> Self {
Self(s)
}
pub fn draw<W: Write>(&self, buf: &mut W, area: Area) -> Result<(), std::io::Error> {
write!(buf, "{}", cursor::Goto(area.x, area.y))?;
write!(buf, "{}", truncate_dots(&self.0, area.w as usize))?;
Ok(())
}
}
pub struct Window {
pub lines: Vec<Line>,
scroll: usize,
}
impl Window {
pub fn new() -> Self {
Self {
lines: Vec::new(),
scroll: 0,
}
}
pub fn draw<W: Write>(&mut self, buf: &mut W, area: Area) -> Result<(), std::io::Error> {
let mut y = area.y;
let mut lines = self.lines.iter().skip(self.scroll);
while y < area.h + 1 {
match lines.next() {
Some(line) => {
write!(buf, "{}", cursor::Goto(area.x, y))?;
match line {
Line::Normal(s) => write!(buf, "{}", truncate_dots(s, area.w as usize))?,
Line::Highlight(s) => write!(
buf,
"{}{}{}",
color::Fg(color::Red),
truncate_dots(s, area.w as usize),
color::Fg(color::Reset),
)?,
}
y += 1;
}
None => break,
}
}
self.lines.clear();
Ok(())
}
pub fn scroll_down(&mut self, until: usize) {
if self.scroll < until - 1 {
self.scroll += 1;
}
}
pub fn scroll_up(&mut self) {
if self.scroll != 0 {
self.scroll -= 1;
}
}
}
|