|
| 1 | +use tuirealm::ratatui::crossterm::event::{KeyCode as CrosstermKeyCode, KeyEvent}; |
| 2 | +use tuirealm::ratatui::{ |
| 3 | + layout::{Constraint, Direction, Layout, Rect}, |
| 4 | + symbols::Marker, |
| 5 | + text::{Line, Span, Text}, |
| 6 | + widgets::{Axis, Block, Borders, Chart, Dataset, GraphType, Paragraph, Widget}, |
| 7 | +}; |
| 8 | +use tuirealm::{ |
| 9 | + command::{Cmd, CmdResult}, |
| 10 | + AttrValue, Attribute, Frame, MockComponent, State, |
| 11 | +}; |
| 12 | + |
| 13 | +use super::{ |
| 14 | + Fraction, ResultsComponent, WORDS_PER_MINUTE_MOVING_AVERAGE_WIDTH, WORDS_PER_MINUTE_PER_CPS, |
| 15 | +}; |
| 16 | + |
| 17 | +impl MockComponent for ResultsComponent { |
| 18 | + fn view(&mut self, frame: &mut Frame, area: Rect) { |
| 19 | + let buffer = frame.buffer_mut(); |
| 20 | + |
| 21 | + buffer.set_style(area, self.theme.default); |
| 22 | + |
| 23 | + // Chunks |
| 24 | + let chunks = Layout::default() |
| 25 | + .direction(Direction::Vertical) |
| 26 | + .constraints([Constraint::Min(1), Constraint::Length(1)]) |
| 27 | + .split(area); |
| 28 | + |
| 29 | + let result_chunks = Layout::default() |
| 30 | + .direction(Direction::Vertical) |
| 31 | + .margin(1) // Graph looks tremendously better with just a little margin |
| 32 | + .constraints([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)]) |
| 33 | + .split(chunks[0]); |
| 34 | + |
| 35 | + let info_chunks = Layout::default() |
| 36 | + .direction(Direction::Horizontal) |
| 37 | + .constraints([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)]) |
| 38 | + .split(result_chunks[0]); |
| 39 | + |
| 40 | + // Handling the incomplete tests |
| 41 | + // TODO: Show a better screen here |
| 42 | + let msg = if self.results.missed_words.is_empty() { |
| 43 | + "Press 'q' to quit or 'r' for another test" |
| 44 | + } else { |
| 45 | + "Press 'q' to quit, 'r' for another test or 'p' to practice missed words" |
| 46 | + }; |
| 47 | + |
| 48 | + let exit = Span::styled(msg, self.theme.results_restart_prompt); |
| 49 | + buffer.set_span(chunks[1].x, chunks[1].y, &exit, chunks[1].width); |
| 50 | + |
| 51 | + // Sections |
| 52 | + let mut overview_text = Text::styled("", self.theme.results_overview); |
| 53 | + overview_text.extend([ |
| 54 | + Line::from(format!( |
| 55 | + "Adjusted WPM: {:.1}", |
| 56 | + self.results.timing.overall_cps |
| 57 | + * WORDS_PER_MINUTE_PER_CPS |
| 58 | + * f64::from(self.results.accuracy.overall) |
| 59 | + )), |
| 60 | + Line::from(format!( |
| 61 | + "Accuracy: {:.1}%", |
| 62 | + f64::from(self.results.accuracy.overall) * 100f64 |
| 63 | + )), |
| 64 | + Line::from(format!( |
| 65 | + "Raw WPM: {:.1}", |
| 66 | + self.results.timing.overall_cps * WORDS_PER_MINUTE_PER_CPS |
| 67 | + )), |
| 68 | + Line::from(format!( |
| 69 | + "Correct Keypresses: {}", |
| 70 | + self.results.accuracy.overall |
| 71 | + )), |
| 72 | + ]); |
| 73 | + let overview = Paragraph::new(overview_text).block( |
| 74 | + Block::default() |
| 75 | + .title(Span::styled("Overview", self.theme.title)) |
| 76 | + .borders(Borders::ALL) |
| 77 | + .border_type(self.theme.border_type) |
| 78 | + .border_style(self.theme.results_overview_border), |
| 79 | + ); |
| 80 | + overview.render(info_chunks[0], buffer); |
| 81 | + |
| 82 | + let mut worst_keys: Vec<(&KeyEvent, &Fraction)> = self |
| 83 | + .results |
| 84 | + .accuracy |
| 85 | + .per_key |
| 86 | + .iter() |
| 87 | + .filter(|(key, _)| matches!(key.code, CrosstermKeyCode::Char(_))) |
| 88 | + .collect(); |
| 89 | + |
| 90 | + // Unstable because we don't care about order, just results |
| 91 | + worst_keys.sort_unstable_by_key(|x| x.1); |
| 92 | + |
| 93 | + let mut worst_text = Text::styled("", self.theme.results_worst_keys); |
| 94 | + worst_text.extend( |
| 95 | + worst_keys |
| 96 | + .iter() |
| 97 | + .filter_map(|(key, acc)| { |
| 98 | + if let CrosstermKeyCode::Char(character) = key.code { |
| 99 | + let key_accuracy = f64::from(**acc) * 100.0; |
| 100 | + if key_accuracy != 100.0 { |
| 101 | + Some(format!("- {} at {:.1}% accuracy", character, key_accuracy)) |
| 102 | + } else { |
| 103 | + None |
| 104 | + } |
| 105 | + } else { |
| 106 | + None |
| 107 | + } |
| 108 | + }) |
| 109 | + .take(5) |
| 110 | + .map(Line::from), |
| 111 | + ); |
| 112 | + |
| 113 | + let worst = Paragraph::new(worst_text).block( |
| 114 | + Block::default() |
| 115 | + .title(Span::styled("Worst Keys", self.theme.title)) |
| 116 | + .borders(Borders::ALL) |
| 117 | + .border_type(self.theme.border_type) |
| 118 | + .border_style(self.theme.results_worst_keys_border), |
| 119 | + ); |
| 120 | + |
| 121 | + worst.render(info_chunks[1], buffer); |
| 122 | + |
| 123 | + let words_per_minute_sliding_moving_average: Vec<(f64, f64)> = self |
| 124 | + .results |
| 125 | + .timing |
| 126 | + .per_event |
| 127 | + .windows(WORDS_PER_MINUTE_MOVING_AVERAGE_WIDTH) |
| 128 | + .enumerate() |
| 129 | + .map(|(i, window): (usize, &[f64])| { |
| 130 | + ( |
| 131 | + (i + WORDS_PER_MINUTE_MOVING_AVERAGE_WIDTH) as f64, |
| 132 | + window.len() as f64 / window.iter().copied().sum::<f64>() |
| 133 | + * WORDS_PER_MINUTE_PER_CPS, |
| 134 | + ) |
| 135 | + }) |
| 136 | + .collect(); |
| 137 | + |
| 138 | + // Render the chart if possible. |
| 139 | + if !words_per_minute_sliding_moving_average.is_empty() { |
| 140 | + let minimum_average = words_per_minute_sliding_moving_average |
| 141 | + .iter() |
| 142 | + .map(|(_, x)| x) |
| 143 | + .fold(f64::INFINITY, |a: f64, &b: &f64| a.min(b)); |
| 144 | + |
| 145 | + let maximum_average = words_per_minute_sliding_moving_average |
| 146 | + .iter() |
| 147 | + .map(|(_, x)| x) |
| 148 | + .fold(f64::NEG_INFINITY, |a: f64, &b: &f64| a.max(b)); |
| 149 | + |
| 150 | + let wpm_datasets = vec![Dataset::default() |
| 151 | + .name("WPM") |
| 152 | + .marker(Marker::Braille) |
| 153 | + .graph_type(GraphType::Line) |
| 154 | + .style(self.theme.results_chart) |
| 155 | + .data(&words_per_minute_sliding_moving_average)]; |
| 156 | + |
| 157 | + let y_label_minimum = minimum_average as u16; |
| 158 | + let y_label_maximum = (maximum_average as u16).max(y_label_minimum + 6); |
| 159 | + |
| 160 | + let wpm_chart = Chart::new(wpm_datasets) |
| 161 | + .block(Block::default().title(vec![Span::styled("Chart", self.theme.title)])) |
| 162 | + .x_axis( |
| 163 | + Axis::default() |
| 164 | + .title(Span::styled("Keypresses", self.theme.results_chart_x)) |
| 165 | + .bounds([0.0, self.results.timing.per_event.len() as f64]), |
| 166 | + ) |
| 167 | + .y_axis( |
| 168 | + Axis::default() |
| 169 | + .title(Span::styled( |
| 170 | + "WPM (10-keypress rolling average)", |
| 171 | + self.theme.results_chart_y, |
| 172 | + )) |
| 173 | + .bounds([minimum_average, maximum_average]) |
| 174 | + .labels( |
| 175 | + (y_label_minimum..y_label_maximum) |
| 176 | + .step_by(5) |
| 177 | + .map(|n| Span::raw(format!("{}", n))) |
| 178 | + .collect::<Vec<_>>(), |
| 179 | + ), |
| 180 | + ); |
| 181 | + wpm_chart.render(result_chunks[1], buffer); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // DEFAULT IMPLEMENTATIONS ROUGHLY |
| 186 | + fn query(&self, _attr: Attribute) -> Option<AttrValue> { |
| 187 | + None |
| 188 | + } |
| 189 | + |
| 190 | + fn attr(&mut self, _attr: Attribute, _value: AttrValue) {} |
| 191 | + |
| 192 | + fn state(&self) -> State { |
| 193 | + State::None |
| 194 | + } |
| 195 | + |
| 196 | + fn perform(&mut self, _cmd: Cmd) -> CmdResult { |
| 197 | + CmdResult::None |
| 198 | + } |
| 199 | +} |
0 commit comments