Repository navigation
Expand file tree
/
Copy pathui.rs
More file actions
205 lines (174 loc) · 7.6 KB
/
Copy pathui.rs
File metadata and controls
205 lines (174 loc) · 7.6 KB
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
use tui::{
buffer::Buffer,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::{Axis, Chart, Dataset, GraphType, Paragraph, Widget, Wrap},
};
use unicode_width::UnicodeWidthStr;
use webbrowser::Browser;
use crate::thok::{Outcome, Thok};
const HORIZONTAL_MARGIN: u16 = 5;
const VERTICAL_MARGIN: u16 = 2;
impl Widget for &Thok {
fn render(self, area: Rect, buf: &mut Buffer) {
// styles
let bold_style = Style::default().add_modifier(Modifier::BOLD);
let green_bold_style = Style::default().patch(bold_style).fg(Color::Green);
let red_bold_style = Style::default().patch(bold_style).fg(Color::Red);
let dim_bold_style = Style::default()
.patch(bold_style)
.add_modifier(Modifier::DIM);
let underlined_dim_bold_style = Style::default()
.patch(dim_bold_style)
.add_modifier(Modifier::UNDERLINED);
let italic_style = Style::default().add_modifier(Modifier::ITALIC);
let magenta_style = Style::default().fg(Color::Magenta);
match !self.has_finished() {
true => {
let max_chars_per_line = area.width - (HORIZONTAL_MARGIN * 2);
let mut prompt_occupied_lines =
((self.prompt.width() as f64 / max_chars_per_line as f64).ceil() + 1.0) as u16;
let time_left_lines = if self.number_of_secs.is_some() { 2 } else { 0 };
if self.prompt.width() <= max_chars_per_line as usize {
prompt_occupied_lines = 1;
}
let chunks = Layout::default()
.direction(Direction::Vertical)
.horizontal_margin(HORIZONTAL_MARGIN)
.constraints(
[
Constraint::Length(
((area.height as f64 - prompt_occupied_lines as f64) / 2.0) as u16,
),
Constraint::Length(time_left_lines),
Constraint::Length(prompt_occupied_lines),
Constraint::Length(
((area.height as f64 - prompt_occupied_lines as f64) / 2.0) as u16,
),
]
.as_ref(),
)
.split(area);
let mut spans = self
.input
.iter()
.enumerate()
.map(|(idx, input)| {
let expected = self.get_expected_char(idx).to_string();
match input.outcome {
Outcome::Incorrect => Span::styled(
match expected.as_str() {
" " => "·".to_owned(),
_ => expected,
},
red_bold_style,
),
Outcome::Correct => Span::styled(expected, green_bold_style),
}
})
.collect::<Vec<Span>>();
spans.push(Span::styled(
self.get_expected_char(self.cursor_pos).to_string(),
underlined_dim_bold_style,
));
spans.push(Span::styled(
self.prompt[(self.cursor_pos + 1)..self.prompt.len()].to_string(),
dim_bold_style,
));
let widget = Paragraph::new(Spans::from(spans))
.alignment(if prompt_occupied_lines == 1 {
// when the prompt is small enough to fit on one line
// centering the text gives a nice zen feeling
Alignment::Center
} else {
Alignment::Left
})
.wrap(Wrap { trim: true });
widget.render(chunks[2], buf);
if self.seconds_remaining.is_some() {
let timer = Paragraph::new(Span::styled(
format!("{:.1}", self.seconds_remaining.unwrap()),
dim_bold_style,
))
.alignment(Alignment::Center);
timer.render(chunks[1], buf);
}
}
false => {
let chunks = Layout::default()
.direction(Direction::Vertical)
.horizontal_margin(HORIZONTAL_MARGIN)
.vertical_margin(VERTICAL_MARGIN)
.constraints(
[
Constraint::Min(1),
Constraint::Length(1),
Constraint::Length(1), // for padding
Constraint::Length(1),
]
.as_ref(),
)
.split(area);
let mut highest_wpm = 0.0;
for ts in &self.wpm_coords {
if ts.1 > highest_wpm {
highest_wpm = ts.1 as f64;
}
}
let datasets = vec![Dataset::default()
.marker(tui::symbols::Marker::Braille)
.style(magenta_style)
.graph_type(GraphType::Line)
.data(&self.wpm_coords)];
let mut overall_duration = match self.wpm_coords.last() {
Some(x) => x.0,
_ => self.seconds_remaining.unwrap_or(1.0),
};
overall_duration = if overall_duration < 1.0 {
1.0
} else {
overall_duration
};
let chart = Chart::new(datasets)
.x_axis(
Axis::default()
.title("seconds")
.bounds([1.0, overall_duration])
.labels(vec![
Span::styled("1", bold_style),
Span::styled(format!("{:.2}", overall_duration), bold_style),
]),
)
.y_axis(
Axis::default()
.title("wpm")
.bounds([0.0, highest_wpm.round()])
.labels(vec![
Span::styled("0", bold_style),
Span::styled(format!("{}", highest_wpm.round()), bold_style),
]),
);
chart.render(chunks[0], buf);
let stats = Paragraph::new(Span::styled(
format!(
"{} wpm {}% acc {:.2} sd",
self.wpm, self.accuracy, self.std_dev
),
bold_style,
))
.alignment(Alignment::Center);
stats.render(chunks[1], buf);
let legend = Paragraph::new(Span::styled(
String::from(if Browser::is_available() {
"(r)etry / (n)ew / (t)weet / (esc)ape"
} else {
"(r)etry / (n)ew / (esc)ape"
}),
italic_style,
));
legend.render(chunks[3], buf);
}
}
}
}