Consistently refer to drills as drills now, rename from lesson/practice
Also fix some issues in the stats screen.
This commit is contained in:
@@ -4,19 +4,36 @@ use ratatui::style::Style;
|
||||
use ratatui::text::{Line, Span};
|
||||
use ratatui::widgets::{Block, Paragraph, Widget};
|
||||
|
||||
use crate::session::lesson::LessonState;
|
||||
use crate::session::result::LessonResult;
|
||||
use crate::session::drill::DrillState;
|
||||
use crate::session::result::DrillResult;
|
||||
use crate::ui::theme::Theme;
|
||||
|
||||
pub struct StatsSidebar<'a> {
|
||||
lesson: &'a LessonState,
|
||||
last_result: Option<&'a LessonResult>,
|
||||
drill: &'a DrillState,
|
||||
last_result: Option<&'a DrillResult>,
|
||||
history: &'a [DrillResult],
|
||||
theme: &'a Theme,
|
||||
}
|
||||
|
||||
impl<'a> StatsSidebar<'a> {
|
||||
pub fn new(lesson: &'a LessonState, last_result: Option<&'a LessonResult>, theme: &'a Theme) -> Self {
|
||||
Self { lesson, last_result, theme }
|
||||
pub fn new(
|
||||
drill: &'a DrillState,
|
||||
last_result: Option<&'a DrillResult>,
|
||||
history: &'a [DrillResult],
|
||||
theme: &'a Theme,
|
||||
) -> Self {
|
||||
Self { drill, last_result, history, theme }
|
||||
}
|
||||
}
|
||||
|
||||
/// Format a delta value with arrow indicator
|
||||
fn format_delta(delta: f64, suffix: &str) -> String {
|
||||
if delta > 0.0 {
|
||||
format!("\u{2191}+{:.1}{suffix}", delta)
|
||||
} else if delta < 0.0 {
|
||||
format!("\u{2193}{:.1}{suffix}", delta)
|
||||
} else {
|
||||
format!("={suffix}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +43,11 @@ impl Widget for StatsSidebar<'_> {
|
||||
|
||||
let has_last = self.last_result.is_some();
|
||||
|
||||
// Split sidebar into current stats and last lesson sections
|
||||
// Split sidebar into current stats and last drill sections
|
||||
let sections = if has_last {
|
||||
Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([Constraint::Min(10), Constraint::Min(10)])
|
||||
.constraints([Constraint::Min(10), Constraint::Min(12)])
|
||||
.split(area)
|
||||
} else {
|
||||
Layout::default()
|
||||
@@ -39,14 +56,14 @@ impl Widget for StatsSidebar<'_> {
|
||||
.split(area)
|
||||
};
|
||||
|
||||
// Current lesson stats
|
||||
// Current drill stats
|
||||
{
|
||||
let wpm = self.lesson.wpm();
|
||||
let accuracy = self.lesson.accuracy();
|
||||
let progress = self.lesson.progress() * 100.0;
|
||||
let correct = self.lesson.correct_count();
|
||||
let incorrect = self.lesson.typo_count();
|
||||
let elapsed = self.lesson.elapsed_secs();
|
||||
let wpm = self.drill.wpm();
|
||||
let accuracy = self.drill.accuracy();
|
||||
let progress = self.drill.progress() * 100.0;
|
||||
let correct = self.drill.correct_count();
|
||||
let incorrect = self.drill.typo_count();
|
||||
let elapsed = self.drill.elapsed_secs();
|
||||
|
||||
let wpm_str = format!("{wpm:.0}");
|
||||
let acc_str = format!("{accuracy:.1}%");
|
||||
@@ -104,51 +121,94 @@ impl Widget for StatsSidebar<'_> {
|
||||
paragraph.render(sections[0], buf);
|
||||
}
|
||||
|
||||
// Last lesson stats
|
||||
// Last drill stats with session impact deltas
|
||||
if let Some(last) = self.last_result {
|
||||
let wpm_str = format!("{:.0}", last.wpm);
|
||||
let acc_str = format!("{:.1}%", last.accuracy);
|
||||
let chars_str = format!("{}", last.total_chars);
|
||||
let time_str = format!("{:.1}s", last.elapsed_secs);
|
||||
let errors_str = format!("{}", last.incorrect);
|
||||
|
||||
let lines = vec![
|
||||
// Compute deltas: compare last drill to the average of all prior drills
|
||||
// (excluding the last one which is the current result)
|
||||
let prior_count = self.history.len().saturating_sub(1);
|
||||
let (wpm_delta, acc_delta) = if prior_count > 0 {
|
||||
let prior = &self.history[..prior_count];
|
||||
let avg_wpm = prior.iter().map(|r| r.wpm).sum::<f64>() / prior.len() as f64;
|
||||
let avg_acc = prior.iter().map(|r| r.accuracy).sum::<f64>() / prior.len() as f64;
|
||||
(last.wpm - avg_wpm, last.accuracy - avg_acc)
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
|
||||
let wpm_delta_str = format_delta(wpm_delta, "");
|
||||
let acc_delta_str = format_delta(acc_delta, "%");
|
||||
|
||||
let wpm_delta_color = if wpm_delta > 0.0 {
|
||||
colors.success()
|
||||
} else if wpm_delta < 0.0 {
|
||||
colors.error()
|
||||
} else {
|
||||
colors.text_pending()
|
||||
};
|
||||
|
||||
let acc_delta_color = if acc_delta > 0.0 {
|
||||
colors.success()
|
||||
} else if acc_delta < 0.0 {
|
||||
colors.error()
|
||||
} else {
|
||||
colors.text_pending()
|
||||
};
|
||||
|
||||
let mut lines = vec![
|
||||
Line::from(vec![
|
||||
Span::styled("WPM: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(wpm_str, Style::default().fg(colors.accent())),
|
||||
]),
|
||||
Line::from(""),
|
||||
Line::from(vec![
|
||||
Span::styled("Accuracy: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(
|
||||
acc_str,
|
||||
Style::default().fg(if last.accuracy >= 95.0 {
|
||||
colors.success()
|
||||
} else if last.accuracy >= 85.0 {
|
||||
colors.warning()
|
||||
} else {
|
||||
colors.error()
|
||||
}),
|
||||
),
|
||||
]),
|
||||
Line::from(""),
|
||||
Line::from(vec![
|
||||
Span::styled("Chars: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(chars_str, Style::default().fg(colors.fg())),
|
||||
]),
|
||||
Line::from(vec![
|
||||
Span::styled("Errors: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(errors_str, Style::default().fg(colors.error())),
|
||||
]),
|
||||
Line::from(""),
|
||||
Line::from(vec![
|
||||
Span::styled("Time: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(time_str, Style::default().fg(colors.fg())),
|
||||
]),
|
||||
];
|
||||
|
||||
if prior_count > 0 {
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(" vs avg: ", Style::default().fg(colors.text_pending())),
|
||||
Span::styled(wpm_delta_str, Style::default().fg(wpm_delta_color)),
|
||||
]));
|
||||
}
|
||||
|
||||
lines.push(Line::from(""));
|
||||
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled("Accuracy: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(
|
||||
acc_str,
|
||||
Style::default().fg(if last.accuracy >= 95.0 {
|
||||
colors.success()
|
||||
} else if last.accuracy >= 85.0 {
|
||||
colors.warning()
|
||||
} else {
|
||||
colors.error()
|
||||
}),
|
||||
),
|
||||
]));
|
||||
|
||||
if prior_count > 0 {
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled(" vs avg: ", Style::default().fg(colors.text_pending())),
|
||||
Span::styled(acc_delta_str, Style::default().fg(acc_delta_color)),
|
||||
]));
|
||||
}
|
||||
|
||||
lines.push(Line::from(""));
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled("Errors: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(errors_str, Style::default().fg(colors.error())),
|
||||
]));
|
||||
lines.push(Line::from(""));
|
||||
lines.push(Line::from(vec![
|
||||
Span::styled("Time: ", Style::default().fg(colors.fg())),
|
||||
Span::styled(time_str, Style::default().fg(colors.fg())),
|
||||
]));
|
||||
|
||||
let block = Block::bordered()
|
||||
.title(" Last Lesson ")
|
||||
.title(" Last Drill ")
|
||||
.border_style(Style::default().fg(colors.border()))
|
||||
.style(Style::default().bg(colors.bg()));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user