mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 05:52:23 -05:00
Add UiState
This commit is contained in:
parent
2db86833a9
commit
d988054ad8
1 changed files with 134 additions and 102 deletions
236
src/list.rs
236
src/list.rs
|
@ -10,112 +10,156 @@ use ratatui::{
|
||||||
style::{Style, Stylize},
|
style::{Style, Stylize},
|
||||||
text::Span,
|
text::Span,
|
||||||
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
||||||
Terminal,
|
Frame, Terminal,
|
||||||
};
|
};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
use crate::{exercise::Exercise, state::State};
|
use crate::{exercise::Exercise, state::State};
|
||||||
|
|
||||||
fn rows<'s, 'e, 'i>(
|
struct UiState<'a> {
|
||||||
state: &'s State,
|
pub table: Table<'a>,
|
||||||
exercises: &'e [Exercise],
|
selected: usize,
|
||||||
) -> impl Iterator<Item = Row<'e>> + 'i
|
table_state: TableState,
|
||||||
where
|
last_ind: usize,
|
||||||
's: 'i,
|
|
||||||
'e: 'i,
|
|
||||||
{
|
|
||||||
exercises
|
|
||||||
.iter()
|
|
||||||
.zip(state.progress())
|
|
||||||
.enumerate()
|
|
||||||
.map(|(ind, (exercise, done))| {
|
|
||||||
let next = if ind == state.next_exercise_ind() {
|
|
||||||
">>>>".bold().red()
|
|
||||||
} else {
|
|
||||||
Span::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
let exercise_state = if *done {
|
|
||||||
"DONE".green()
|
|
||||||
} else {
|
|
||||||
"PENDING".yellow()
|
|
||||||
};
|
|
||||||
|
|
||||||
Row::new([
|
|
||||||
next,
|
|
||||||
exercise_state,
|
|
||||||
Span::raw(&exercise.name),
|
|
||||||
Span::raw(exercise.path.to_string_lossy()),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
|
impl<'a> UiState<'a> {
|
||||||
let header = Row::new(["Next", "State", "Name", "Path"]);
|
pub fn rows<'s, 'i>(
|
||||||
|
state: &'s State,
|
||||||
|
exercises: &'a [Exercise],
|
||||||
|
) -> impl Iterator<Item = Row<'a>> + 'i
|
||||||
|
where
|
||||||
|
's: 'i,
|
||||||
|
'a: 'i,
|
||||||
|
{
|
||||||
|
exercises
|
||||||
|
.iter()
|
||||||
|
.zip(state.progress())
|
||||||
|
.enumerate()
|
||||||
|
.map(|(ind, (exercise, done))| {
|
||||||
|
let next = if ind == state.next_exercise_ind() {
|
||||||
|
">>>>".bold().red()
|
||||||
|
} else {
|
||||||
|
Span::default()
|
||||||
|
};
|
||||||
|
|
||||||
let max_name_len = exercises
|
let exercise_state = if *done {
|
||||||
.iter()
|
"DONE".green()
|
||||||
.map(|exercise| exercise.name.len())
|
} else {
|
||||||
.max()
|
"PENDING".yellow()
|
||||||
.unwrap_or(4) as u16;
|
};
|
||||||
|
|
||||||
let widths = [
|
Row::new([
|
||||||
Constraint::Length(4),
|
next,
|
||||||
Constraint::Length(7),
|
exercise_state,
|
||||||
Constraint::Length(max_name_len),
|
Span::raw(&exercise.name),
|
||||||
Constraint::Fill(1),
|
Span::raw(exercise.path.to_string_lossy()),
|
||||||
];
|
])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
Table::new(rows(state, exercises), widths)
|
pub fn new(state: &State, exercises: &'a [Exercise]) -> Self {
|
||||||
.header(header)
|
let header = Row::new(["Next", "State", "Name", "Path"]);
|
||||||
.column_spacing(2)
|
|
||||||
.highlight_spacing(HighlightSpacing::Always)
|
let max_name_len = exercises
|
||||||
.highlight_style(Style::new().bg(ratatui::style::Color::Rgb(50, 50, 50)))
|
.iter()
|
||||||
.highlight_symbol("🦀")
|
.map(|exercise| exercise.name.len())
|
||||||
.block(Block::default().borders(Borders::BOTTOM))
|
.max()
|
||||||
|
.unwrap_or(4) as u16;
|
||||||
|
|
||||||
|
let widths = [
|
||||||
|
Constraint::Length(4),
|
||||||
|
Constraint::Length(7),
|
||||||
|
Constraint::Length(max_name_len),
|
||||||
|
Constraint::Fill(1),
|
||||||
|
];
|
||||||
|
|
||||||
|
let rows = Self::rows(state, exercises);
|
||||||
|
|
||||||
|
let table = 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("🦀")
|
||||||
|
.block(Block::default().borders(Borders::BOTTOM));
|
||||||
|
|
||||||
|
let selected = 0;
|
||||||
|
let table_state = TableState::default().with_selected(Some(selected));
|
||||||
|
let last_ind = exercises.len() - 1;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
table,
|
||||||
|
selected,
|
||||||
|
table_state,
|
||||||
|
last_ind,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select(&mut self, ind: usize) {
|
||||||
|
self.selected = ind;
|
||||||
|
self.table_state.select(Some(ind));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select_next(&mut self) {
|
||||||
|
self.select(self.selected.saturating_add(1).min(self.last_ind));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select_previous(&mut self) {
|
||||||
|
self.select(self.selected.saturating_sub(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn select_first(&mut self) {
|
||||||
|
self.select(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn select_last(&mut self) {
|
||||||
|
self.select(self.last_ind);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&mut self, frame: &mut Frame) {
|
||||||
|
let area = frame.size();
|
||||||
|
|
||||||
|
frame.render_stateful_widget(
|
||||||
|
&self.table,
|
||||||
|
Rect {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: area.width,
|
||||||
|
height: area.height - 1,
|
||||||
|
},
|
||||||
|
&mut self.table_state,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Help footer
|
||||||
|
let footer =
|
||||||
|
"↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit";
|
||||||
|
frame.render_widget(
|
||||||
|
Span::raw(footer),
|
||||||
|
Rect {
|
||||||
|
x: 0,
|
||||||
|
y: area.height - 1,
|
||||||
|
width: area.width,
|
||||||
|
height: 1,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
|
pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
|
||||||
let mut stdout = io::stdout().lock();
|
let mut stdout = io::stdout().lock();
|
||||||
|
|
||||||
stdout.execute(EnterAlternateScreen)?;
|
stdout.execute(EnterAlternateScreen)?;
|
||||||
enable_raw_mode()?;
|
enable_raw_mode()?;
|
||||||
|
|
||||||
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
|
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
|
||||||
terminal.clear()?;
|
terminal.clear()?;
|
||||||
|
|
||||||
let mut table = table(state, exercises);
|
let mut ui_state = UiState::new(state, exercises);
|
||||||
|
|
||||||
let last_ind = exercises.len() - 1;
|
|
||||||
let mut selected = 0;
|
|
||||||
let mut table_state = TableState::default().with_selected(Some(selected));
|
|
||||||
|
|
||||||
'outer: loop {
|
'outer: loop {
|
||||||
terminal.draw(|frame| {
|
terminal.draw(|frame| ui_state.draw(frame))?;
|
||||||
let area = frame.size();
|
|
||||||
|
|
||||||
frame.render_stateful_widget(
|
|
||||||
&table,
|
|
||||||
Rect {
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: area.width,
|
|
||||||
height: area.height - 1,
|
|
||||||
},
|
|
||||||
&mut table_state,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Help footer
|
|
||||||
frame.render_widget(
|
|
||||||
Span::raw("↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit"),
|
|
||||||
Rect {
|
|
||||||
x: 0,
|
|
||||||
y: area.height - 1,
|
|
||||||
width: area.width,
|
|
||||||
height: 1,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let key = loop {
|
let key = loop {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
|
@ -135,25 +179,13 @@ pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
|
||||||
|
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Char('q') => break,
|
KeyCode::Char('q') => break,
|
||||||
KeyCode::Down | KeyCode::Char('j') => {
|
KeyCode::Down | KeyCode::Char('j') => ui_state.select_next(),
|
||||||
selected = selected.saturating_add(1).min(last_ind);
|
KeyCode::Up | KeyCode::Char('k') => ui_state.select_previous(),
|
||||||
table_state.select(Some(selected));
|
KeyCode::Home | KeyCode::Char('g') => ui_state.select_first(),
|
||||||
}
|
KeyCode::End | KeyCode::Char('G') => ui_state.select_last(),
|
||||||
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));
|
|
||||||
}
|
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
state.set_next_exercise_ind(selected)?;
|
state.set_next_exercise_ind(ui_state.selected)?;
|
||||||
table = table.rows(rows(state, exercises));
|
ui_state.table = ui_state.table.rows(UiState::rows(state, exercises));
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue