|
1 |
+ |
//! History screen. Per-exercise view: current progression state, top-set
|
|
2 |
+ |
//! load sparkline, and the last ten sessions in a compact table.
|
|
3 |
+ |
|
|
4 |
+ |
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
5 |
+ |
use ratatui::Frame;
|
|
6 |
+ |
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
|
7 |
+ |
use ratatui::style::{Modifier, Style};
|
|
8 |
+ |
use ratatui::text::{Line, Span};
|
|
9 |
+ |
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Sparkline};
|
|
10 |
+ |
use ripgrow_core::{Db, Exercise, SessionSet, State};
|
|
11 |
+ |
|
|
12 |
+ |
pub struct HistoryScreen {
|
|
13 |
+ |
exercises: Vec<Exercise>,
|
|
14 |
+ |
list_state: ListState,
|
|
15 |
+ |
pub status: String,
|
|
16 |
+ |
}
|
|
17 |
+ |
|
|
18 |
+ |
pub enum HistoryAction {
|
|
19 |
+ |
None,
|
|
20 |
+ |
Quit,
|
|
21 |
+ |
}
|
|
22 |
+ |
|
|
23 |
+ |
impl HistoryScreen {
|
|
24 |
+ |
pub fn load(db: &Db) -> Result<Self, ripgrow_core::Error> {
|
|
25 |
+ |
let exercises = db.list_exercises()?;
|
|
26 |
+ |
let mut list_state = ListState::default();
|
|
27 |
+ |
if !exercises.is_empty() {
|
|
28 |
+ |
list_state.select(Some(0));
|
|
29 |
+ |
}
|
|
30 |
+ |
Ok(Self {
|
|
31 |
+ |
exercises,
|
|
32 |
+ |
list_state,
|
|
33 |
+ |
status: String::new(),
|
|
34 |
+ |
})
|
|
35 |
+ |
}
|
|
36 |
+ |
|
|
37 |
+ |
pub fn on_key(&mut self, _db: &Db, key: KeyEvent) -> HistoryAction {
|
|
38 |
+ |
match (key.code, key.modifiers) {
|
|
39 |
+ |
(KeyCode::Char('q'), _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
|
|
40 |
+ |
HistoryAction::Quit
|
|
41 |
+ |
}
|
|
42 |
+ |
(KeyCode::Down | KeyCode::Char('j'), _) => {
|
|
43 |
+ |
self.move_selection(1);
|
|
44 |
+ |
HistoryAction::None
|
|
45 |
+ |
}
|
|
46 |
+ |
(KeyCode::Up | KeyCode::Char('k'), _) => {
|
|
47 |
+ |
self.move_selection(-1);
|
|
48 |
+ |
HistoryAction::None
|
|
49 |
+ |
}
|
|
50 |
+ |
_ => HistoryAction::None,
|
|
51 |
+ |
}
|
|
52 |
+ |
}
|
|
53 |
+ |
|
|
54 |
+ |
fn move_selection(&mut self, delta: isize) {
|
|
55 |
+ |
if self.exercises.is_empty() {
|
|
56 |
+ |
return;
|
|
57 |
+ |
}
|
|
58 |
+ |
let n = self.exercises.len() as isize;
|
|
59 |
+ |
let cur = self.list_state.selected().unwrap_or(0) as isize;
|
|
60 |
+ |
let next = (cur + delta).rem_euclid(n) as usize;
|
|
61 |
+ |
self.list_state.select(Some(next));
|
|
62 |
+ |
}
|
|
63 |
+ |
|
|
64 |
+ |
fn selected(&self) -> Option<&Exercise> {
|
|
65 |
+ |
self.list_state.selected().and_then(|i| self.exercises.get(i))
|
|
66 |
+ |
}
|
|
67 |
+ |
|
|
68 |
+ |
pub fn render(&mut self, frame: &mut Frame, area: Rect, db: &Db) {
|
|
69 |
+ |
let chunks = Layout::default()
|
|
70 |
+ |
.direction(Direction::Vertical)
|
|
71 |
+ |
.constraints([Constraint::Min(3), Constraint::Length(1)])
|
|
72 |
+ |
.split(area);
|
|
73 |
+ |
|
|
74 |
+ |
let panes = Layout::default()
|
|
75 |
+ |
.direction(Direction::Horizontal)
|
|
76 |
+ |
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
|
77 |
+ |
.split(chunks[0]);
|
|
78 |
+ |
|
|
79 |
+ |
let items: Vec<ListItem> = if self.exercises.is_empty() {
|
|
80 |
+ |
vec![ListItem::new("no templates yet")]
|
|
81 |
+ |
} else {
|
|
82 |
+ |
self.exercises
|
|
83 |
+ |
.iter()
|
|
84 |
+ |
.map(|e| ListItem::new(e.name.as_str()))
|
|
85 |
+ |
.collect()
|
|
86 |
+ |
};
|
|
87 |
+ |
let list = List::new(items)
|
|
88 |
+ |
.block(Block::default().borders(Borders::ALL).title(" exercises "))
|
|
89 |
+ |
.highlight_style(Style::default().add_modifier(Modifier::REVERSED))
|
|
90 |
+ |
.highlight_symbol("> ");
|
|
91 |
+ |
frame.render_stateful_widget(list, panes[0], &mut self.list_state);
|
|
92 |
+ |
|
|
93 |
+ |
match self.selected() {
|
|
94 |
+ |
Some(ex) => render_detail(frame, panes[1], db, ex),
|
|
95 |
+ |
None => {
|
|
96 |
+ |
let block = Block::default().borders(Borders::ALL).title(" detail ");
|
|
97 |
+ |
frame.render_widget(
|
|
98 |
+ |
Paragraph::new("no exercise selected").block(block),
|
|
99 |
+ |
panes[1],
|
|
100 |
+ |
);
|
|
101 |
+ |
}
|
|
102 |
+ |
}
|
|
103 |
+ |
|
|
104 |
+ |
frame.render_widget(
|
|
105 |
+ |
Paragraph::new(Line::from(vec![
|
|
106 |
+ |
Span::raw("j/k move q quit "),
|
|
107 |
+ |
Span::styled(
|
|
108 |
+ |
self.status.as_str(),
|
|
109 |
+ |
Style::default().add_modifier(Modifier::DIM),
|
|
110 |
+ |
),
|
|
111 |
+ |
])),
|
|
112 |
+ |
chunks[1],
|
|
113 |
+ |
);
|
|
114 |
+ |
}
|
|
115 |
+ |
}
|
|
116 |
+ |
|
|
117 |
+ |
fn render_detail(frame: &mut Frame, area: Rect, db: &Db, ex: &Exercise) {
|
|
118 |
+ |
let block = Block::default()
|
|
119 |
+ |
.borders(Borders::ALL)
|
|
120 |
+ |
.title(format!(" {} ", ex.name));
|
|
121 |
+ |
let inner = block.inner(area);
|
|
122 |
+ |
frame.render_widget(block, area);
|
|
123 |
+ |
|
|
124 |
+ |
let series = db.top_loads_over_time(ex.id).unwrap_or_default();
|
|
125 |
+ |
let state_str = db
|
|
126 |
+ |
.read_progression_state(ex.id)
|
|
127 |
+ |
.ok()
|
|
128 |
+ |
.flatten()
|
|
129 |
+ |
.map(state_name)
|
|
130 |
+ |
.unwrap_or("(no state cached)");
|
|
131 |
+ |
|
|
132 |
+ |
let (header_area, spark_area, table_area) = split_detail(inner);
|
|
133 |
+ |
|
|
134 |
+ |
let header = format!(
|
|
135 |
+ |
"state: {} sessions: {}",
|
|
136 |
+ |
state_str,
|
|
137 |
+ |
series.len(),
|
|
138 |
+ |
);
|
|
139 |
+ |
frame.render_widget(Paragraph::new(header), header_area);
|
|
140 |
+ |
|
|
141 |
+ |
if !series.is_empty() {
|
|
142 |
+ |
// Sparkline takes u64. Multiply by 10 so 0.1 kg resolution survives
|
|
143 |
+ |
// the cast; ratatui normalizes across the series so absolute scale
|
|
144 |
+ |
// does not matter.
|
|
145 |
+ |
let data: Vec<u64> = series.iter().map(|(_, l)| (l * 10.0) as u64).collect();
|
|
146 |
+ |
let min = series.iter().map(|(_, l)| *l).fold(f64::INFINITY, f64::min);
|
|
147 |
+ |
let max = series
|
|
148 |
+ |
.iter()
|
|
149 |
+ |
.map(|(_, l)| *l)
|
|
150 |
+ |
.fold(f64::NEG_INFINITY, f64::max);
|
|
151 |
+ |
let title = format!(
|
|
152 |
+ |
" top-set load min {} max {} ({}) ",
|
|
153 |
+ |
min, max, ex.load_unit
|
|
154 |
+ |
);
|
|
155 |
+ |
let spark = Sparkline::default()
|
|
156 |
+ |
.block(Block::default().borders(Borders::ALL).title(title))
|
|
157 |
+ |
.data(&data);
|
|
158 |
+ |
frame.render_widget(spark, spark_area);
|
|
159 |
+ |
} else {
|
|
160 |
+ |
frame.render_widget(
|
|
161 |
+ |
Paragraph::new("no history yet — log some sets first")
|
|
162 |
+ |
.block(Block::default().borders(Borders::ALL).title(" top-set load ")),
|
|
163 |
+ |
spark_area,
|
|
164 |
+ |
);
|
|
165 |
+ |
}
|
|
166 |
+ |
|
|
167 |
+ |
render_recent_table(frame, table_area, db, ex);
|
|
168 |
+ |
}
|
|
169 |
+ |
|
|
170 |
+ |
fn split_detail(area: Rect) -> (Rect, Rect, Rect) {
|
|
171 |
+ |
let chunks = Layout::default()
|
|
172 |
+ |
.direction(Direction::Vertical)
|
|
173 |
+ |
.constraints([
|
|
174 |
+ |
Constraint::Length(1),
|
|
175 |
+ |
Constraint::Length(6),
|
|
176 |
+ |
Constraint::Min(1),
|
|
177 |
+ |
])
|
|
178 |
+ |
.split(area);
|
|
179 |
+ |
(chunks[0], chunks[1], chunks[2])
|
|
180 |
+ |
}
|
|
181 |
+ |
|
|
182 |
+ |
fn render_recent_table(frame: &mut Frame, area: Rect, db: &Db, ex: &Exercise) {
|
|
183 |
+ |
let sessions = db
|
|
184 |
+ |
.list_sessions_for_exercise(ex.id)
|
|
185 |
+ |
.unwrap_or_default();
|
|
186 |
+ |
let recent: Vec<&Vec<SessionSet>> = sessions.iter().rev().take(10).collect();
|
|
187 |
+ |
|
|
188 |
+ |
let mut lines: Vec<Line> = vec![Line::from(Span::styled(
|
|
189 |
+ |
format!(
|
|
190 |
+ |
" {:<10} {:>4} {:>8} {:>4} {:>3}",
|
|
191 |
+ |
"date", "sets", "top load", "reps", "rpe"
|
|
192 |
+ |
),
|
|
193 |
+ |
Style::default().add_modifier(Modifier::BOLD),
|
|
194 |
+ |
))];
|
|
195 |
+ |
|
|
196 |
+ |
for session in &recent {
|
|
197 |
+ |
let date = session[0].session_date;
|
|
198 |
+ |
let top_load = top_load(session);
|
|
199 |
+ |
let top_reps = session.iter().map(|s| s.reps).max().unwrap_or(0);
|
|
200 |
+ |
let max_rpe = session.iter().map(|s| s.rpe).max().unwrap_or(0);
|
|
201 |
+ |
lines.push(Line::from(format!(
|
|
202 |
+ |
" {:<10} {:>4} {:>8} {:>4} {:>3}",
|
|
203 |
+ |
date.format("%Y-%m-%d"),
|
|
204 |
+ |
session.len(),
|
|
205 |
+ |
top_load,
|
|
206 |
+ |
top_reps,
|
|
207 |
+ |
max_rpe
|
|
208 |
+ |
)));
|
|
209 |
+ |
}
|
|
210 |
+ |
|
|
211 |
+ |
if recent.is_empty() {
|
|
212 |
+ |
lines.push(Line::from(" (no sessions logged)"));
|
|
213 |
+ |
}
|
|
214 |
+ |
|
|
215 |
+ |
frame.render_widget(
|
|
216 |
+ |
Paragraph::new(lines).block(
|
|
217 |
+ |
Block::default().borders(Borders::ALL).title(" recent sessions "),
|
|
218 |
+ |
),
|
|
219 |
+ |
area,
|
|
220 |
+ |
);
|
|
221 |
+ |
}
|
|
222 |
+ |
|
|
223 |
+ |
fn top_load(session: &[SessionSet]) -> f64 {
|
|
224 |
+ |
session
|
|
225 |
+ |
.iter()
|
|
226 |
+ |
.map(|s| s.load)
|
|
227 |
+ |
.fold(f64::NEG_INFINITY, f64::max)
|
|
228 |
+ |
}
|
|
229 |
+ |
|
|
230 |
+ |
fn state_name(s: State) -> &'static str {
|
|
231 |
+ |
match s {
|
|
232 |
+ |
State::Progressing => "progressing",
|
|
233 |
+ |
State::Probation => "probation",
|
|
234 |
+ |
State::Deloading => "deloading",
|
|
235 |
+ |
State::Rebuilding => "rebuilding",
|
|
236 |
+ |
}
|
|
237 |
+ |
}
|
|
238 |
+ |
|
|
239 |
+ |
#[cfg(test)]
|
|
240 |
+ |
mod tests {
|
|
241 |
+ |
use super::*;
|
|
242 |
+ |
use crossterm::event::KeyEvent;
|
|
243 |
+ |
use ripgrow_core::{ResistanceType, Unit};
|
|
244 |
+ |
|
|
245 |
+ |
fn key(c: KeyCode) -> KeyEvent {
|
|
246 |
+ |
KeyEvent::new(c, KeyModifiers::empty())
|
|
247 |
+ |
}
|
|
248 |
+ |
|
|
249 |
+ |
fn setup() -> (Db, i64) {
|
|
250 |
+ |
let db = Db::open_in_memory().unwrap();
|
|
251 |
+ |
db.init_profile("self", Unit::Kg).unwrap();
|
|
252 |
+ |
let id = db
|
|
253 |
+ |
.create_exercise("squat", ResistanceType::Freeweight, "kg", 2.5, &[])
|
|
254 |
+ |
.unwrap();
|
|
255 |
+ |
(db, id)
|
|
256 |
+ |
}
|
|
257 |
+ |
|
|
258 |
+ |
#[test]
|
|
259 |
+ |
fn q_signals_quit() {
|
|
260 |
+ |
let (db, _) = setup();
|
|
261 |
+ |
let mut screen = HistoryScreen::load(&db).unwrap();
|
|
262 |
+ |
assert!(matches!(
|
|
263 |
+ |
screen.on_key(&db, key(KeyCode::Char('q'))),
|
|
264 |
+ |
HistoryAction::Quit
|
|
265 |
+ |
));
|
|
266 |
+ |
}
|
|
267 |
+ |
|
|
268 |
+ |
#[test]
|
|
269 |
+ |
fn j_k_wraps_selection() {
|
|
270 |
+ |
let db = Db::open_in_memory().unwrap();
|
|
271 |
+ |
db.init_profile("self", Unit::Kg).unwrap();
|
|
272 |
+ |
db.create_exercise("a", ResistanceType::Freeweight, "kg", 2.5, &[])
|
|
273 |
+ |
.unwrap();
|
|
274 |
+ |
db.create_exercise("b", ResistanceType::Freeweight, "kg", 2.5, &[])
|
|
275 |
+ |
.unwrap();
|
|
276 |
+ |
let mut screen = HistoryScreen::load(&db).unwrap();
|
|
277 |
+ |
assert_eq!(screen.list_state.selected(), Some(0));
|
|
278 |
+ |
screen.on_key(&db, key(KeyCode::Char('j')));
|
|
279 |
+ |
assert_eq!(screen.list_state.selected(), Some(1));
|
|
280 |
+ |
screen.on_key(&db, key(KeyCode::Char('j')));
|
|
281 |
+ |
assert_eq!(screen.list_state.selected(), Some(0), "wraps to top");
|
|
282 |
+ |
}
|
|
283 |
+ |
|
|
284 |
+ |
#[test]
|
|
285 |
+ |
fn selection_reflects_current_exercise() {
|
|
286 |
+ |
let (db, id) = setup();
|
|
287 |
+ |
let screen = HistoryScreen::load(&db).unwrap();
|
|
288 |
+ |
assert_eq!(screen.selected().map(|e| e.id), Some(id));
|
|
289 |
+ |
}
|
|
290 |
+ |
}
|