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
|
use anyhow::Result;
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
use ratatui::{
backend::CrosstermBackend,
layout::Constraint,
style::{Style, Stylize},
text::Span,
widgets::{HighlightSpacing, Row, Table, TableState},
Terminal,
};
use std::io;
use crate::{exercise::Exercise, state::State};
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
let header = Row::new(["State", "Name", "Path"]);
let max_name_len = exercises
.iter()
.map(|exercise| exercise.name.len())
.max()
.unwrap_or(4) as u16;
let widths = [
Constraint::Length(7),
Constraint::Length(max_name_len),
Constraint::Fill(1),
];
let rows = exercises
.iter()
.zip(&state.progress)
.map(|(exercise, done)| {
let state = if *done {
"DONE".green()
} else {
"PENDING".yellow()
};
Row::new([
state,
Span::raw(&exercise.name),
Span::raw(exercise.path.to_string_lossy()),
])
})
.collect::<Vec<_>>();
Table::new(rows, widths)
.header(header)
.column_spacing(2)
.highlight_spacing(HighlightSpacing::Always)
.highlight_style(Style::new().bg(ratatui::style::Color::Rgb(50, 50, 50)))
.highlight_symbol("🦀")
}
pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> {
let mut stdout = io::stdout().lock();
stdout.execute(EnterAlternateScreen)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
terminal.clear()?;
let table = table(state, exercises);
let last_ind = exercises.len() - 1;
let mut selected = 0;
let mut table_state = TableState::default().with_selected(Some(selected));
'outer: loop {
terminal.draw(|frame| {
let area = frame.size();
frame.render_stateful_widget(&table, area, &mut table_state);
})?;
let key = loop {
match event::read()? {
Event::Key(key) => {
if key.kind != KeyEventKind::Press {
continue;
}
break key;
}
// Redraw
Event::Resize(_, _) => continue 'outer,
// Ignore
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
}
};
match key.code {
KeyCode::Char('q') => break,
KeyCode::Down | KeyCode::Char('j') => {
selected = selected.saturating_add(1).min(last_ind);
table_state.select(Some(selected));
}
KeyCode::Up | KeyCode::Char('k') => {
selected = selected.saturating_sub(1).max(0);
table_state.select(Some(selected));
}
KeyCode::Home | KeyCode::Char('g') => {
selected = 0;
table_state.select(Some(selected));
}
KeyCode::End | KeyCode::Char('G') => {
selected = last_ind;
table_state.select(Some(selected));
}
_ => (),
}
}
drop(terminal);
stdout.execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())
}
|