Add UiState

This commit is contained in:
mo8it 2024-04-07 16:33:00 +02:00
parent 2db86833a9
commit d988054ad8

View file

@ -10,19 +10,27 @@ use ratatui::{
style::{Style, Stylize},
text::Span,
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
Terminal,
Frame, Terminal,
};
use std::io;
use crate::{exercise::Exercise, state::State};
fn rows<'s, 'e, 'i>(
struct UiState<'a> {
pub table: Table<'a>,
selected: usize,
table_state: TableState,
last_ind: usize,
}
impl<'a> UiState<'a> {
pub fn rows<'s, 'i>(
state: &'s State,
exercises: &'e [Exercise],
) -> impl Iterator<Item = Row<'e>> + 'i
exercises: &'a [Exercise],
) -> impl Iterator<Item = Row<'a>> + 'i
where
's: 'i,
'e: 'i,
'a: 'i,
{
exercises
.iter()
@ -50,7 +58,7 @@ where
})
}
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
pub fn new(state: &State, exercises: &'a [Exercise]) -> Self {
let header = Row::new(["Next", "State", "Name", "Path"]);
let max_name_len = exercises
@ -66,48 +74,70 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
Constraint::Fill(1),
];
Table::new(rows(state, exercises), widths)
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))
.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,
}
}
pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
let mut stdout = io::stdout().lock();
fn select(&mut self, ind: usize) {
self.selected = ind;
self.table_state.select(Some(ind));
}
stdout.execute(EnterAlternateScreen)?;
enable_raw_mode()?;
pub fn select_next(&mut self) {
self.select(self.selected.saturating_add(1).min(self.last_ind));
}
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
terminal.clear()?;
pub fn select_previous(&mut self) {
self.select(self.selected.saturating_sub(1));
}
let mut table = table(state, exercises);
#[inline]
pub fn select_first(&mut self) {
self.select(0);
}
let last_ind = exercises.len() - 1;
let mut selected = 0;
let mut table_state = TableState::default().with_selected(Some(selected));
#[inline]
pub fn select_last(&mut self) {
self.select(self.last_ind);
}
'outer: loop {
terminal.draw(|frame| {
pub fn draw(&mut self, frame: &mut Frame) {
let area = frame.size();
frame.render_stateful_widget(
&table,
&self.table,
Rect {
x: 0,
y: 0,
width: area.width,
height: area.height - 1,
},
&mut table_state,
&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("↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit"),
Span::raw(footer),
Rect {
x: 0,
y: area.height - 1,
@ -115,7 +145,21 @@ pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
height: 1,
},
);
})?;
}
}
pub fn list(state: &mut 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 mut ui_state = UiState::new(state, exercises);
'outer: loop {
terminal.draw(|frame| ui_state.draw(frame))?;
let key = loop {
match event::read()? {
@ -135,25 +179,13 @@ pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
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));
}
KeyCode::Down | KeyCode::Char('j') => ui_state.select_next(),
KeyCode::Up | KeyCode::Char('k') => ui_state.select_previous(),
KeyCode::Home | KeyCode::Char('g') => ui_state.select_first(),
KeyCode::End | KeyCode::Char('G') => ui_state.select_last(),
KeyCode::Char('c') => {
state.set_next_exercise_ind(selected)?;
table = table.rows(rows(state, exercises));
state.set_next_exercise_ind(ui_state.selected)?;
ui_state.table = ui_state.table.rows(UiState::rows(state, exercises));
}
_ => (),
}