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
use {
crate::*,
anyhow::*,
crossbeam::channel::{select, unbounded, Receiver, Sender},
crossterm::{
cursor,
event::KeyModifiers,
execute,
style::{Colorize, Styler},
terminal, ExecutableCommand, QueueableCommand,
},
std::{env, io::Write, path::PathBuf},
termimad::{Event, EventSource},
};
#[derive(Debug)]
pub struct AppState {
pub name: String,
pub report: Option<Report>,
pub screen: (u16, u16),
pub computing: bool,
pub summary: bool,
}
impl AppState {
pub fn new() -> Result<Self> {
Ok(Self {
name: env::current_dir()?
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
report: None,
screen: termimad::terminal_size(),
computing: true,
summary: false,
})
}
}
fn goto(w: &mut W, y: u16) -> Result<()> {
execute!(
w,
cursor::MoveTo(0, y),
terminal::Clear(terminal::ClearType::CurrentLine)
)?;
Ok(())
}
impl AppState {
pub fn draw(&self, w: &mut W) -> Result<()> {
let width = self.screen.0 as usize;
goto(w, 1)?;
if self.computing {
// todo: maybe show the current line of the computed report ?
eprint!(
"{}",
format!("{:^w$}", "computing...", w = width)
.white()
.on_dark_grey()
);
}
if let Some(report) = &self.report {
goto(w, 0)?;
eprint!(
"{} ",
format!(" {} ", &self.name).white().bold().on_dark_grey()
);
if !report.warnings.is_empty() {
eprint!(
"{} ",
format!(" {} warnings ", report.warnings.len())
.black()
.bold()
.on_yellow()
);
}
if !report.errors.is_empty() {
eprint!(
"{} ",
format!(" {} errors ", report.errors.len())
.white()
.bold()
.on_red()
);
}
if report.warnings.is_empty() && report.errors.is_empty() {
eprint!("{} ", " pass! ".white().bold().on_dark_green());
}
let mut y = 2;
let mut w_idx = 0;
let mut e_idx = 0;
let h = self.screen.1 - 1;
while y < h {
goto(w, y)?;
if let Some(ref error) = report.errors.get(e_idx) {
eprint!(
"{} {}",
format!("{:>2} ", e_idx + 1).black().bold().on_red(),
&error.lines[0],
);
if !self.summary {
for line in &error.lines[1..] {
if y >= h - 1 {
break;
}
y += 1;
goto(w, y)?;
eprint!(" {}", &line);
}
}
e_idx += 1;
} else if let Some(ref warning) = report.warnings.get(w_idx) {
eprint!(
"{} {}",
format!("{:>2} ", w_idx + 1).black().bold().on_yellow(),
&warning.lines[0],
);
if !self.summary {
for line in &warning.lines[1..] {
if y >= h - 1 {
break;
}
y += 1;
goto(w, y)?;
eprint!(" {}", &line);
}
}
w_idx += 1;
}
y += 1;
}
}
goto(w, self.screen.1)?;
eprint!(
"{}",
format!(" {:<w$}", "hit q to quit", w = width - 1)
.white()
.on_dark_grey()
);
Ok(())
}
}