|
1 |
+ |
//! Log screen: the fast keyboard-only path for entering a session's sets.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! Two sub-modes. **Pick** fuzzy-filters exercise templates by substring
|
|
4 |
+ |
//! subsequence match; Enter selects the highlighted one. **Grid** shows
|
|
5 |
+ |
//! the sets already logged for (date, exercise) plus a pending row being
|
|
6 |
+ |
//! typed. Enter on the last column commits the pending row and opens a
|
|
7 |
+ |
//! fresh one, so the flow is:
|
|
8 |
+ |
//!
|
|
9 |
+ |
//! 100 [tab] 5 [tab] 3 [enter] → row saved, cursor back at load.
|
|
10 |
+ |
//!
|
|
11 |
+ |
//! Session date defaults to today; `[`/`]` step by a day.
|
|
12 |
+ |
|
|
13 |
+ |
use chrono::{Duration, Local, NaiveDate};
|
|
14 |
+ |
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
15 |
+ |
use ratatui::Frame;
|
|
16 |
+ |
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
|
17 |
+ |
use ratatui::style::{Modifier, Style};
|
|
18 |
+ |
use ratatui::text::{Line, Span};
|
|
19 |
+ |
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph};
|
|
20 |
+ |
use ripgrow_core::{Db, Exercise, SessionSet};
|
|
21 |
+ |
|
|
22 |
+ |
pub struct LogScreen {
|
|
23 |
+ |
date: NaiveDate,
|
|
24 |
+ |
exercises: Vec<Exercise>,
|
|
25 |
+ |
picker: PickState,
|
|
26 |
+ |
grid: Option<GridState>,
|
|
27 |
+ |
pub status: String,
|
|
28 |
+ |
}
|
|
29 |
+ |
|
|
30 |
+ |
struct PickState {
|
|
31 |
+ |
query: String,
|
|
32 |
+ |
list_state: ListState,
|
|
33 |
+ |
}
|
|
34 |
+ |
|
|
35 |
+ |
struct GridState {
|
|
36 |
+ |
exercise: Exercise,
|
|
37 |
+ |
committed: Vec<SessionSet>,
|
|
38 |
+ |
pending: PendingRow,
|
|
39 |
+ |
focus: GridField,
|
|
40 |
+ |
}
|
|
41 |
+ |
|
|
42 |
+ |
#[derive(Default)]
|
|
43 |
+ |
struct PendingRow {
|
|
44 |
+ |
load: String,
|
|
45 |
+ |
reps: String,
|
|
46 |
+ |
rpe: String,
|
|
47 |
+ |
}
|
|
48 |
+ |
|
|
49 |
+ |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
50 |
+ |
enum GridField {
|
|
51 |
+ |
Load,
|
|
52 |
+ |
Reps,
|
|
53 |
+ |
Rpe,
|
|
54 |
+ |
}
|
|
55 |
+ |
|
|
56 |
+ |
impl LogScreen {
|
|
57 |
+ |
pub fn load(db: &Db) -> Result<Self, ripgrow_core::Error> {
|
|
58 |
+ |
let exercises = db.list_exercises()?;
|
|
59 |
+ |
let mut list_state = ListState::default();
|
|
60 |
+ |
if !exercises.is_empty() {
|
|
61 |
+ |
list_state.select(Some(0));
|
|
62 |
+ |
}
|
|
63 |
+ |
Ok(Self {
|
|
64 |
+ |
date: Local::now().date_naive(),
|
|
65 |
+ |
exercises,
|
|
66 |
+ |
picker: PickState {
|
|
67 |
+ |
query: String::new(),
|
|
68 |
+ |
list_state,
|
|
69 |
+ |
},
|
|
70 |
+ |
grid: None,
|
|
71 |
+ |
status: String::new(),
|
|
72 |
+ |
})
|
|
73 |
+ |
}
|
|
74 |
+ |
|
|
75 |
+ |
pub fn on_key(&mut self, db: &Db, key: KeyEvent) {
|
|
76 |
+ |
if self.grid.is_some() {
|
|
77 |
+ |
self.on_grid_key(db, key);
|
|
78 |
+ |
} else {
|
|
79 |
+ |
self.on_pick_key(key);
|
|
80 |
+ |
}
|
|
81 |
+ |
}
|
|
82 |
+ |
|
|
83 |
+ |
// ---- picker sub-mode ------------------------------------------------
|
|
84 |
+ |
|
|
85 |
+ |
fn filtered_indices(&self) -> Vec<usize> {
|
|
86 |
+ |
let q = self.picker.query.trim().to_lowercase();
|
|
87 |
+ |
if q.is_empty() {
|
|
88 |
+ |
(0..self.exercises.len()).collect()
|
|
89 |
+ |
} else {
|
|
90 |
+ |
self.exercises
|
|
91 |
+ |
.iter()
|
|
92 |
+ |
.enumerate()
|
|
93 |
+ |
.filter(|(_, e)| subsequence_match(&e.name.to_lowercase(), &q))
|
|
94 |
+ |
.map(|(i, _)| i)
|
|
95 |
+ |
.collect()
|
|
96 |
+ |
}
|
|
97 |
+ |
}
|
|
98 |
+ |
|
|
99 |
+ |
fn on_pick_key(&mut self, key: KeyEvent) {
|
|
100 |
+ |
match key.code {
|
|
101 |
+ |
KeyCode::Esc => {
|
|
102 |
+ |
self.picker.query.clear();
|
|
103 |
+ |
}
|
|
104 |
+ |
KeyCode::Enter => {
|
|
105 |
+ |
let filt = self.filtered_indices();
|
|
106 |
+ |
if let Some(row) = self.picker.list_state.selected()
|
|
107 |
+ |
&& let Some(ex_idx) = filt.get(row)
|
|
108 |
+ |
&& let Some(ex) = self.exercises.get(*ex_idx)
|
|
109 |
+ |
{
|
|
110 |
+ |
self.enter_grid(ex.clone());
|
|
111 |
+ |
}
|
|
112 |
+ |
}
|
|
113 |
+ |
KeyCode::Down => self.move_pick(1),
|
|
114 |
+ |
KeyCode::Up => self.move_pick(-1),
|
|
115 |
+ |
KeyCode::Char('[') if key.modifiers == KeyModifiers::NONE => {
|
|
116 |
+ |
self.date -= Duration::days(1);
|
|
117 |
+ |
}
|
|
118 |
+ |
KeyCode::Char(']') if key.modifiers == KeyModifiers::NONE => {
|
|
119 |
+ |
self.date += Duration::days(1);
|
|
120 |
+ |
}
|
|
121 |
+ |
KeyCode::Backspace => {
|
|
122 |
+ |
self.picker.query.pop();
|
|
123 |
+ |
self.picker.list_state.select(Some(0));
|
|
124 |
+ |
}
|
|
125 |
+ |
KeyCode::Char(c) => {
|
|
126 |
+ |
self.picker.query.push(c);
|
|
127 |
+ |
self.picker.list_state.select(Some(0));
|
|
128 |
+ |
}
|
|
129 |
+ |
_ => {}
|
|
130 |
+ |
}
|
|
131 |
+ |
}
|
|
132 |
+ |
|
|
133 |
+ |
fn move_pick(&mut self, delta: isize) {
|
|
134 |
+ |
let n = self.filtered_indices().len();
|
|
135 |
+ |
if n == 0 {
|
|
136 |
+ |
return;
|
|
137 |
+ |
}
|
|
138 |
+ |
let cur = self.picker.list_state.selected().unwrap_or(0) as isize;
|
|
139 |
+ |
let next = (cur + delta).rem_euclid(n as isize) as usize;
|
|
140 |
+ |
self.picker.list_state.select(Some(next));
|
|
141 |
+ |
}
|
|
142 |
+ |
|
|
143 |
+ |
fn enter_grid(&mut self, exercise: Exercise) {
|
|
144 |
+ |
// Committed sets are loaded fresh from the DB by the caller when
|
|
145 |
+ |
// needed; construct the grid state with an empty vec here and let
|
|
146 |
+ |
// refresh_grid() populate it.
|
|
147 |
+ |
self.grid = Some(GridState {
|
|
148 |
+ |
exercise,
|
|
149 |
+ |
committed: Vec::new(),
|
|
150 |
+ |
pending: PendingRow::default(),
|
|
151 |
+ |
focus: GridField::Load,
|
|
152 |
+ |
});
|
|
153 |
+ |
}
|
|
154 |
+ |
|
|
155 |
+ |
fn refresh_grid(&mut self, db: &Db) {
|
|
156 |
+ |
if let Some(g) = self.grid.as_mut()
|
|
157 |
+ |
&& let Ok(list) = db.list_sets_for_session(g.exercise.id, self.date)
|
|
158 |
+ |
{
|
|
159 |
+ |
g.committed = list;
|
|
160 |
+ |
}
|
|
161 |
+ |
}
|
|
162 |
+ |
|
|
163 |
+ |
// ---- grid sub-mode --------------------------------------------------
|
|
164 |
+ |
|
|
165 |
+ |
fn on_grid_key(&mut self, db: &Db, key: KeyEvent) {
|
|
166 |
+ |
let Some(g) = self.grid.as_mut() else { return };
|
|
167 |
+ |
match (key.code, key.modifiers) {
|
|
168 |
+ |
(KeyCode::Esc, _) => {
|
|
169 |
+ |
self.grid = None;
|
|
170 |
+ |
return;
|
|
171 |
+ |
}
|
|
172 |
+ |
(KeyCode::Tab, _) => {
|
|
173 |
+ |
g.focus = match g.focus {
|
|
174 |
+ |
GridField::Load => GridField::Reps,
|
|
175 |
+ |
GridField::Reps => GridField::Rpe,
|
|
176 |
+ |
GridField::Rpe => GridField::Load,
|
|
177 |
+ |
};
|
|
178 |
+ |
return;
|
|
179 |
+ |
}
|
|
180 |
+ |
(KeyCode::BackTab, _) => {
|
|
181 |
+ |
g.focus = match g.focus {
|
|
182 |
+ |
GridField::Load => GridField::Rpe,
|
|
183 |
+ |
GridField::Reps => GridField::Load,
|
|
184 |
+ |
GridField::Rpe => GridField::Reps,
|
|
185 |
+ |
};
|
|
186 |
+ |
return;
|
|
187 |
+ |
}
|
|
188 |
+ |
(KeyCode::Enter, _) => {
|
|
189 |
+ |
if let Err(e) = commit_pending(db, g, self.date) {
|
|
190 |
+ |
self.status = format!("save failed: {e}");
|
|
191 |
+ |
} else {
|
|
192 |
+ |
self.status = "set saved".to_string();
|
|
193 |
+ |
}
|
|
194 |
+ |
self.refresh_grid(db);
|
|
195 |
+ |
return;
|
|
196 |
+ |
}
|
|
197 |
+ |
(KeyCode::Backspace, KeyModifiers::CONTROL) => {
|
|
198 |
+ |
if let Some(last) = g.committed.last().cloned() {
|
|
199 |
+ |
match db.delete_set(last.id) {
|
|
200 |
+ |
Ok(()) => {
|
|
201 |
+ |
self.status = "last set deleted".to_string();
|
|
202 |
+ |
self.refresh_grid(db);
|
|
203 |
+ |
}
|
|
204 |
+ |
Err(e) => self.status = format!("delete failed: {e}"),
|
|
205 |
+ |
}
|
|
206 |
+ |
}
|
|
207 |
+ |
return;
|
|
208 |
+ |
}
|
|
209 |
+ |
_ => {}
|
|
210 |
+ |
}
|
|
211 |
+ |
|
|
212 |
+ |
let target = match g.focus {
|
|
213 |
+ |
GridField::Load => &mut g.pending.load,
|
|
214 |
+ |
GridField::Reps => &mut g.pending.reps,
|
|
215 |
+ |
GridField::Rpe => &mut g.pending.rpe,
|
|
216 |
+ |
};
|
|
217 |
+ |
match key.code {
|
|
218 |
+ |
KeyCode::Backspace => {
|
|
219 |
+ |
target.pop();
|
|
220 |
+ |
}
|
|
221 |
+ |
KeyCode::Char(c) => target.push(c),
|
|
222 |
+ |
_ => {}
|
|
223 |
+ |
}
|
|
224 |
+ |
}
|
|
225 |
+ |
|
|
226 |
+ |
// ---- rendering ------------------------------------------------------
|
|
227 |
+ |
|
|
228 |
+ |
pub fn render(&mut self, frame: &mut Frame, area: Rect) {
|
|
229 |
+ |
let chunks = Layout::default()
|
|
230 |
+ |
.direction(Direction::Vertical)
|
|
231 |
+ |
.constraints([
|
|
232 |
+ |
Constraint::Length(1),
|
|
233 |
+ |
Constraint::Min(3),
|
|
234 |
+ |
Constraint::Length(1),
|
|
235 |
+ |
])
|
|
236 |
+ |
.split(area);
|
|
237 |
+ |
|
|
238 |
+ |
let date_hint = format!(
|
|
239 |
+ |
"session: {} ([/] to change date)",
|
|
240 |
+ |
self.date.format("%Y-%m-%d")
|
|
241 |
+ |
);
|
|
242 |
+ |
frame.render_widget(Paragraph::new(date_hint), chunks[0]);
|
|
243 |
+ |
|
|
244 |
+ |
match &mut self.grid {
|
|
245 |
+ |
Some(g) => render_grid(frame, chunks[1], g),
|
|
246 |
+ |
None => render_picker(frame, chunks[1], &mut self.picker, &self.exercises),
|
|
247 |
+ |
}
|
|
248 |
+ |
|
|
249 |
+ |
let hint = match &self.grid {
|
|
250 |
+ |
None => "type to filter enter pick [/] date esc clear",
|
|
251 |
+ |
Some(_) => {
|
|
252 |
+ |
"tab move enter save ctrl-backspace remove last esc back to picker"
|
|
253 |
+ |
}
|
|
254 |
+ |
};
|
|
255 |
+ |
frame.render_widget(
|
|
256 |
+ |
Paragraph::new(Line::from(vec![
|
|
257 |
+ |
Span::raw(hint),
|
|
258 |
+ |
Span::raw(" "),
|
|
259 |
+ |
Span::styled(
|
|
260 |
+ |
self.status.as_str(),
|
|
261 |
+ |
Style::default().add_modifier(Modifier::DIM),
|
|
262 |
+ |
),
|
|
263 |
+ |
])),
|
|
264 |
+ |
chunks[2],
|
|
265 |
+ |
);
|
|
266 |
+ |
}
|
|
267 |
+ |
}
|
|
268 |
+ |
|
|
269 |
+ |
fn commit_pending(
|
|
270 |
+ |
db: &Db,
|
|
271 |
+ |
g: &mut GridState,
|
|
272 |
+ |
date: NaiveDate,
|
|
273 |
+ |
) -> Result<(), ripgrow_core::Error> {
|
|
274 |
+ |
let load: f64 = parse_field("load", &g.pending.load)?;
|
|
275 |
+ |
let reps: i32 = parse_field("reps", &g.pending.reps)?;
|
|
276 |
+ |
let rpe: i32 = parse_field("rpe", &g.pending.rpe)?;
|
|
277 |
+ |
db.append_set(g.exercise.id, date, load, reps, rpe)?;
|
|
278 |
+ |
g.pending = PendingRow::default();
|
|
279 |
+ |
g.focus = GridField::Load;
|
|
280 |
+ |
Ok(())
|
|
281 |
+ |
}
|
|
282 |
+ |
|
|
283 |
+ |
fn parse_field<T: std::str::FromStr>(
|
|
284 |
+ |
field: &'static str,
|
|
285 |
+ |
raw: &str,
|
|
286 |
+ |
) -> Result<T, ripgrow_core::Error> {
|
|
287 |
+ |
raw.trim()
|
|
288 |
+ |
.parse()
|
|
289 |
+ |
.map_err(|_| ripgrow_core::Error::ParseField {
|
|
290 |
+ |
field,
|
|
291 |
+ |
value: raw.to_string(),
|
|
292 |
+ |
})
|
|
293 |
+ |
}
|
|
294 |
+ |
|
|
295 |
+ |
fn render_picker(
|
|
296 |
+ |
frame: &mut Frame,
|
|
297 |
+ |
area: Rect,
|
|
298 |
+ |
picker: &mut PickState,
|
|
299 |
+ |
exercises: &[Exercise],
|
|
300 |
+ |
) {
|
|
301 |
+ |
let chunks = Layout::default()
|
|
302 |
+ |
.direction(Direction::Vertical)
|
|
303 |
+ |
.constraints([Constraint::Length(3), Constraint::Min(1)])
|
|
304 |
+ |
.split(area);
|
|
305 |
+ |
|
|
306 |
+ |
let query_block = Block::default().borders(Borders::ALL).title(" pick exercise ");
|
|
307 |
+ |
let query = Paragraph::new(format!("> {}_", picker.query)).block(query_block);
|
|
308 |
+ |
frame.render_widget(query, chunks[0]);
|
|
309 |
+ |
|
|
310 |
+ |
let q = picker.query.trim().to_lowercase();
|
|
311 |
+ |
let items: Vec<ListItem> = exercises
|
|
312 |
+ |
.iter()
|
|
313 |
+ |
.filter(|e| q.is_empty() || subsequence_match(&e.name.to_lowercase(), &q))
|
|
314 |
+ |
.map(|e| ListItem::new(e.name.as_str()))
|
|
315 |
+ |
.collect();
|
|
316 |
+ |
let list = List::new(items)
|
|
317 |
+ |
.block(Block::default().borders(Borders::ALL).title(" matches "))
|
|
318 |
+ |
.highlight_style(Style::default().add_modifier(Modifier::REVERSED))
|
|
319 |
+ |
.highlight_symbol("> ");
|
|
320 |
+ |
frame.render_stateful_widget(list, chunks[1], &mut picker.list_state);
|
|
321 |
+ |
}
|
|
322 |
+ |
|
|
323 |
+ |
fn render_grid(frame: &mut Frame, area: Rect, g: &GridState) {
|
|
324 |
+ |
let block = Block::default()
|
|
325 |
+ |
.borders(Borders::ALL)
|
|
326 |
+ |
.title(format!(" {} ", g.exercise.name));
|
|
327 |
+ |
let inner = block.inner(area);
|
|
328 |
+ |
frame.render_widget(block, area);
|
|
329 |
+ |
|
|
330 |
+ |
let header = format!(
|
|
331 |
+ |
" # {:>8} {:>4} {:>3}",
|
|
332 |
+ |
format!("load ({})", g.exercise.load_unit),
|
|
333 |
+ |
"reps",
|
|
334 |
+ |
"rpe"
|
|
335 |
+ |
);
|
|
336 |
+ |
let mut lines: Vec<Line> = vec![Line::from(Span::styled(
|
|
337 |
+ |
header,
|
|
338 |
+ |
Style::default().add_modifier(Modifier::BOLD),
|
|
339 |
+ |
))];
|
|
340 |
+ |
|
|
341 |
+ |
for s in &g.committed {
|
|
342 |
+ |
lines.push(Line::from(format!(
|
|
343 |
+ |
" {:<3} {:>8} {:>4} {:>3}",
|
|
344 |
+ |
s.set_index, s.load, s.reps, s.rpe
|
|
345 |
+ |
)));
|
|
346 |
+ |
}
|
|
347 |
+ |
|
|
348 |
+ |
let load_span = focused_span(&g.pending.load, g.focus == GridField::Load, 8);
|
|
349 |
+ |
let reps_span = focused_span(&g.pending.reps, g.focus == GridField::Reps, 4);
|
|
350 |
+ |
let rpe_span = focused_span(&g.pending.rpe, g.focus == GridField::Rpe, 3);
|
|
351 |
+ |
let next_idx = g.committed.last().map(|s| s.set_index + 1).unwrap_or(1);
|
|
352 |
+ |
lines.push(Line::from(vec![
|
|
353 |
+ |
Span::raw(format!(" {:<3} ", next_idx)),
|
|
354 |
+ |
load_span,
|
|
355 |
+ |
Span::raw(" "),
|
|
356 |
+ |
reps_span,
|
|
357 |
+ |
Span::raw(" "),
|
|
358 |
+ |
rpe_span,
|
|
359 |
+ |
]));
|
|
360 |
+ |
|
|
361 |
+ |
frame.render_widget(Paragraph::new(lines), inner);
|
|
362 |
+ |
}
|
|
363 |
+ |
|
|
364 |
+ |
fn focused_span(value: &str, focused: bool, width: usize) -> Span<'_> {
|
|
365 |
+ |
let content = format!("{:>width$}", value, width = width);
|
|
366 |
+ |
if focused {
|
|
367 |
+ |
Span::styled(content, Style::default().add_modifier(Modifier::REVERSED))
|
|
368 |
+ |
} else {
|
|
369 |
+ |
Span::raw(content)
|
|
370 |
+ |
}
|
|
371 |
+ |
}
|
|
372 |
+ |
|
|
373 |
+ |
/// Case-insensitive subsequence match. `"sq"` matches `"squat"` and
|
|
374 |
+ |
/// `"back squat"` but not `"pushup"`.
|
|
375 |
+ |
fn subsequence_match(haystack: &str, needle: &str) -> bool {
|
|
376 |
+ |
let mut chars = haystack.chars();
|
|
377 |
+ |
needle.chars().all(|nc| chars.any(|hc| hc == nc))
|
|
378 |
+ |
}
|
|
379 |
+ |
|
|
380 |
+ |
#[cfg(test)]
|
|
381 |
+ |
mod tests {
|
|
382 |
+ |
use super::*;
|
|
383 |
+ |
use crossterm::event::KeyEvent;
|
|
384 |
+ |
use ripgrow_core::{ResistanceType, Unit};
|
|
385 |
+ |
|
|
386 |
+ |
fn key(c: KeyCode) -> KeyEvent {
|
|
387 |
+ |
KeyEvent::new(c, KeyModifiers::empty())
|
|
388 |
+ |
}
|
|
389 |
+ |
|
|
390 |
+ |
fn setup() -> (Db, i64) {
|
|
391 |
+ |
let db = Db::open_in_memory().unwrap();
|
|
392 |
+ |
db.init_profile("self", Unit::Kg).unwrap();
|
|
393 |
+ |
let sq = db
|
|
394 |
+ |
.create_exercise("squat", ResistanceType::Freeweight, "kg", 2.5, &[])
|
|
395 |
+ |
.unwrap();
|
|
396 |
+ |
db.create_exercise("bench", ResistanceType::Freeweight, "kg", 2.5, &[])
|
|
397 |
+ |
.unwrap();
|
|
398 |
+ |
db.create_exercise("row", ResistanceType::Freeweight, "kg", 2.5, &[])
|
|
399 |
+ |
.unwrap();
|
|
400 |
+ |
(db, sq)
|
|
401 |
+ |
}
|
|
402 |
+ |
|
|
403 |
+ |
#[test]
|
|
404 |
+ |
fn subsequence_match_examples() {
|
|
405 |
+ |
assert!(subsequence_match("squat", "sq"));
|
|
406 |
+ |
assert!(subsequence_match("back squat", "bsq"));
|
|
407 |
+ |
assert!(!subsequence_match("pushup", "sq"));
|
|
408 |
+ |
}
|
|
409 |
+ |
|
|
410 |
+ |
#[test]
|
|
411 |
+ |
fn typing_filters_and_enter_selects() {
|
|
412 |
+ |
let (db, sq) = setup();
|
|
413 |
+ |
let mut screen = LogScreen::load(&db).unwrap();
|
|
414 |
+ |
for c in "sq".chars() {
|
|
415 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
416 |
+ |
}
|
|
417 |
+ |
assert_eq!(screen.filtered_indices().len(), 1);
|
|
418 |
+ |
screen.on_key(&db, key(KeyCode::Enter));
|
|
419 |
+ |
assert!(screen.grid.is_some());
|
|
420 |
+ |
assert_eq!(screen.grid.as_ref().unwrap().exercise.id, sq);
|
|
421 |
+ |
}
|
|
422 |
+ |
|
|
423 |
+ |
#[test]
|
|
424 |
+ |
fn enter_on_last_column_commits_set() {
|
|
425 |
+ |
let (db, sq) = setup();
|
|
426 |
+ |
let mut screen = LogScreen::load(&db).unwrap();
|
|
427 |
+ |
for c in "squat".chars() {
|
|
428 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
429 |
+ |
}
|
|
430 |
+ |
screen.on_key(&db, key(KeyCode::Enter));
|
|
431 |
+ |
// Now in grid. Type load, tab, reps, tab, rpe, enter.
|
|
432 |
+ |
for c in "100".chars() {
|
|
433 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
434 |
+ |
}
|
|
435 |
+ |
screen.on_key(&db, key(KeyCode::Tab));
|
|
436 |
+ |
for c in "5".chars() {
|
|
437 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
438 |
+ |
}
|
|
439 |
+ |
screen.on_key(&db, key(KeyCode::Tab));
|
|
440 |
+ |
for c in "3".chars() {
|
|
441 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
442 |
+ |
}
|
|
443 |
+ |
screen.on_key(&db, key(KeyCode::Enter));
|
|
444 |
+ |
|
|
445 |
+ |
let today = chrono::Local::now().date_naive();
|
|
446 |
+ |
let list = db.list_sets_for_session(sq, today).unwrap();
|
|
447 |
+ |
assert_eq!(list.len(), 1);
|
|
448 |
+ |
assert_eq!(list[0].load, 100.0);
|
|
449 |
+ |
assert_eq!(list[0].reps, 5);
|
|
450 |
+ |
assert_eq!(list[0].rpe, 3);
|
|
451 |
+ |
// Pending row should be cleared and focus back on Load.
|
|
452 |
+ |
let g = screen.grid.as_ref().unwrap();
|
|
453 |
+ |
assert!(g.pending.load.is_empty());
|
|
454 |
+ |
assert_eq!(g.focus, GridField::Load);
|
|
455 |
+ |
}
|
|
456 |
+ |
|
|
457 |
+ |
#[test]
|
|
458 |
+ |
fn ctrl_backspace_removes_last_committed_set() {
|
|
459 |
+ |
let (db, sq) = setup();
|
|
460 |
+ |
let today = chrono::Local::now().date_naive();
|
|
461 |
+ |
db.append_set(sq, today, 100.0, 5, 3).unwrap();
|
|
462 |
+ |
db.append_set(sq, today, 100.0, 5, 3).unwrap();
|
|
463 |
+ |
let mut screen = LogScreen::load(&db).unwrap();
|
|
464 |
+ |
for c in "squat".chars() {
|
|
465 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
466 |
+ |
}
|
|
467 |
+ |
screen.on_key(&db, key(KeyCode::Enter));
|
|
468 |
+ |
screen.refresh_grid(&db);
|
|
469 |
+ |
assert_eq!(screen.grid.as_ref().unwrap().committed.len(), 2);
|
|
470 |
+ |
screen.on_key(
|
|
471 |
+ |
&db,
|
|
472 |
+ |
KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL),
|
|
473 |
+ |
);
|
|
474 |
+ |
assert_eq!(db.list_sets_for_session(sq, today).unwrap().len(), 1);
|
|
475 |
+ |
}
|
|
476 |
+ |
|
|
477 |
+ |
#[test]
|
|
478 |
+ |
fn bracket_keys_change_date() {
|
|
479 |
+ |
let db = Db::open_in_memory().unwrap();
|
|
480 |
+ |
db.init_profile("self", Unit::Kg).unwrap();
|
|
481 |
+ |
let mut screen = LogScreen::load(&db).unwrap();
|
|
482 |
+ |
let start = screen.date;
|
|
483 |
+ |
screen.on_key(&db, key(KeyCode::Char('[')));
|
|
484 |
+ |
assert_eq!(screen.date, start - Duration::days(1));
|
|
485 |
+ |
screen.on_key(&db, key(KeyCode::Char(']')));
|
|
486 |
+ |
screen.on_key(&db, key(KeyCode::Char(']')));
|
|
487 |
+ |
assert_eq!(screen.date, start + Duration::days(1));
|
|
488 |
+ |
}
|
|
489 |
+ |
|
|
490 |
+ |
#[test]
|
|
491 |
+ |
fn esc_from_grid_returns_to_picker() {
|
|
492 |
+ |
let (db, _sq) = setup();
|
|
493 |
+ |
let mut screen = LogScreen::load(&db).unwrap();
|
|
494 |
+ |
for c in "squat".chars() {
|
|
495 |
+ |
screen.on_key(&db, key(KeyCode::Char(c)));
|
|
496 |
+ |
}
|
|
497 |
+ |
screen.on_key(&db, key(KeyCode::Enter));
|
|
498 |
+ |
assert!(screen.grid.is_some());
|
|
499 |
+ |
screen.on_key(&db, key(KeyCode::Esc));
|
|
500 |
+ |
assert!(screen.grid.is_none());
|