From 570bc9f32d7ef0bf741fab44d15f7cd54a1f3fc1 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Aug 2024 00:14:12 +0200 Subject: [PATCH 1/8] Start list without Ratatui --- .typos.toml | 3 - Cargo.lock | 163 +--------------- Cargo.toml | 2 +- rustlings-macros/Cargo.toml | 2 +- src/exercise.rs | 2 +- src/init.rs | 2 +- src/list.rs | 142 +++++++------- src/list/state.rs | 361 +++++++++++++++++------------------- src/main.rs | 1 + src/progress_bar.rs | 93 +++------- src/run.rs | 2 +- src/term.rs | 12 +- src/watch/state.rs | 14 +- src/watch/terminal_event.rs | 4 +- 14 files changed, 303 insertions(+), 500 deletions(-) diff --git a/.typos.toml b/.typos.toml index a74498ab..2de6d580 100644 --- a/.typos.toml +++ b/.typos.toml @@ -2,6 +2,3 @@ extend-exclude = [ "CHANGELOG.md", ] - -[default.extend-words] -"ratatui" = "ratatui" diff --git a/Cargo.lock b/Cargo.lock index a66f4ba3..93b2051b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,12 +14,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "allocator-api2" -version = "0.2.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" - [[package]] name = "anstream" version = "0.6.15" @@ -93,21 +87,6 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" -[[package]] -name = "cassowary" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" - -[[package]] -name = "castaway" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0abae9be0aaf9ea96a3b1b8b1b55c602ca751eba1b1500220cea4ecbafe7c0d5" -dependencies = [ - "rustversion", -] - [[package]] name = "cfg-if" version = "1.0.0" @@ -160,20 +139,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" -[[package]] -name = "compact_str" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6050c3a16ddab2e412160b31f2c871015704239bca62f72f6e5f0be631d3f644" -dependencies = [ - "castaway", - "cfg-if", - "itoa", - "rustversion", - "ryu", - "static_assertions", -] - [[package]] name = "crossbeam-channel" version = "0.5.13" @@ -214,12 +179,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "either" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" - [[package]] name = "equivalent" version = "1.0.1" @@ -268,10 +227,6 @@ name = "hashbrown" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" -dependencies = [ - "ahash", - "allocator-api2", -] [[package]] name = "heck" @@ -315,31 +270,12 @@ dependencies = [ "libc", ] -[[package]] -name = "instability" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b23a0c8dfe501baac4adf6ebbfa6eddf8f0c07f56b058cc1288017e32397846c" -dependencies = [ - "quote", - "syn", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" -[[package]] -name = "itertools" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" -dependencies = [ - "either", -] - [[package]] name = "itoa" version = "1.0.11" @@ -405,15 +341,6 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" -[[package]] -name = "lru" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37ee39891760e7d94734f6f63fedc29a2e4a152f836120753a72503f09fcf904" -dependencies = [ - "hashbrown", -] - [[package]] name = "memchr" version = "2.7.4" @@ -513,12 +440,6 @@ dependencies = [ "windows-targets 0.52.6", ] -[[package]] -name = "paste" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" - [[package]] name = "proc-macro2" version = "1.0.86" @@ -530,34 +451,13 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] -[[package]] -name = "ratatui" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ba6a365afbe5615999275bea2446b970b10a41102500e27ce7678d50d978303" -dependencies = [ - "bitflags 2.6.0", - "cassowary", - "compact_str", - "crossterm", - "instability", - "itertools", - "lru", - "paste", - "strum", - "strum_macros", - "unicode-segmentation", - "unicode-truncate", - "unicode-width", -] - [[package]] name = "redox_syscall" version = "0.5.3" @@ -587,9 +487,9 @@ dependencies = [ "ahash", "anyhow", "clap", + "crossterm", "notify-debouncer-mini", "os_pipe", - "ratatui", "rustlings-macros", "serde", "serde_json", @@ -606,12 +506,6 @@ dependencies = [ "toml_edit", ] -[[package]] -name = "rustversion" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" - [[package]] name = "ryu" version = "1.0.18" @@ -710,40 +604,12 @@ version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" -[[package]] -name = "strum" -version = "0.26.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" -dependencies = [ - "strum_macros", -] - -[[package]] -name = "strum_macros" -version = "0.26.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "rustversion", - "syn", -] - [[package]] name = "syn" version = "2.0.75" @@ -796,29 +662,6 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" -[[package]] -name = "unicode-segmentation" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" - -[[package]] -name = "unicode-truncate" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf" -dependencies = [ - "itertools", - "unicode-segmentation", - "unicode-width", -] - -[[package]] -name = "unicode-width" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d" - [[package]] name = "utf8parse" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index a8b81eb2..4b3e98c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,9 +49,9 @@ include = [ ahash = { version = "0.8.11", default-features = false } anyhow = "1.0.86" clap = { version = "4.5.16", features = ["derive"] } +crossterm = { version = "0.28.1", default-features = false, features = ["windows", "events"] } notify-debouncer-mini = { version = "0.4.1", default-features = false } os_pipe = "1.2.1" -ratatui = { version = "0.28.0", default-features = false, features = ["crossterm"] } rustlings-macros = { path = "rustlings-macros", version = "=6.2.0" } serde_json = "1.0.125" serde.workspace = true diff --git a/rustlings-macros/Cargo.toml b/rustlings-macros/Cargo.toml index 8a852018..3ed56a18 100644 --- a/rustlings-macros/Cargo.toml +++ b/rustlings-macros/Cargo.toml @@ -16,7 +16,7 @@ include = [ proc-macro = true [dependencies] -quote = "1.0.36" +quote = "1.0.37" serde.workspace = true toml_edit.workspace = true diff --git a/src/exercise.rs b/src/exercise.rs index 5318b9a5..ac5c6e6e 100644 --- a/src/exercise.rs +++ b/src/exercise.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use ratatui::crossterm::style::{style, StyledContent, Stylize}; +use crossterm::style::{style, StyledContent, Stylize}; use std::{ fmt::{self, Display, Formatter}, io::Write, diff --git a/src/init.rs b/src/init.rs index 95b04e9f..2c172dca 100644 --- a/src/init.rs +++ b/src/init.rs @@ -1,5 +1,5 @@ use anyhow::{bail, Context, Result}; -use ratatui::crossterm::style::Stylize; +use crossterm::style::Stylize; use serde::Deserialize; use std::{ env::set_current_dir, diff --git a/src/list.rs b/src/list.rs index 6ff69596..754c5e2f 100644 --- a/src/list.rs +++ b/src/list.rs @@ -1,95 +1,101 @@ use anyhow::{Context, Result}; -use ratatui::{ - backend::CrosstermBackend, - crossterm::{ - event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, - QueueableCommand, +use crossterm::{ + cursor, + event::{ + self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseEventKind, }, - Terminal, + terminal::{ + disable_raw_mode, enable_raw_mode, DisableLineWrap, EnableLineWrap, EnterAlternateScreen, + LeaveAlternateScreen, + }, + QueueableCommand, }; use std::io::{self, StdoutLock, Write}; use crate::app_state::AppState; -use self::state::{Filter, UiState}; +use self::state::{Filter, ListState}; mod state; fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> { - let mut terminal = Terminal::new(CrosstermBackend::new(stdout))?; - terminal.clear()?; + let mut list_state = ListState::new(app_state, stdout)?; - let mut ui_state = UiState::new(app_state); + loop { + match event::read().context("Failed to read terminal event")? { + Event::Key(key) => { + match key.kind { + KeyEventKind::Release => continue, + KeyEventKind::Press | KeyEventKind::Repeat => (), + } - 'outer: loop { - terminal.try_draw(|frame| ui_state.draw(frame).map_err(io::Error::other))?; + list_state.message.clear(); - let key = loop { - match event::read().context("Failed to read terminal event")? { - Event::Key(key) => match key.kind { - KeyEventKind::Press | KeyEventKind::Repeat => break key, - KeyEventKind::Release => (), - }, - // Redraw - Event::Resize(_, _) => continue 'outer, - // Ignore - Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (), + match key.code { + KeyCode::Char('q') => return Ok(()), + KeyCode::Down | KeyCode::Char('j') => list_state.select_next(), + KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(), + KeyCode::Home | KeyCode::Char('g') => list_state.select_first(), + KeyCode::End | KeyCode::Char('G') => list_state.select_last(), + KeyCode::Char('d') => { + let message = if list_state.filter() == Filter::Done { + list_state.set_filter(Filter::None); + "Disabled filter DONE" + } else { + list_state.set_filter(Filter::Done); + "Enabled filter DONE │ Press d again to disable the filter" + }; + + list_state.message.push_str(message); + } + KeyCode::Char('p') => { + let message = if list_state.filter() == Filter::Pending { + list_state.set_filter(Filter::None); + "Disabled filter PENDING" + } else { + list_state.set_filter(Filter::Pending); + "Enabled filter PENDING │ Press p again to disable the filter" + }; + + list_state.message.push_str(message); + } + KeyCode::Char('r') => { + list_state.reset_selected()?; + } + KeyCode::Char('c') => { + return list_state.selected_to_current_exercise(); + } + // Redraw to remove the message. + KeyCode::Esc => (), + _ => continue, + } + + list_state.redraw(stdout)?; } - }; + Event::Mouse(event) => { + match event.kind { + MouseEventKind::ScrollDown => list_state.select_next(), + MouseEventKind::ScrollUp => list_state.select_previous(), + _ => continue, + } - ui_state.message.clear(); - - match key.code { - KeyCode::Char('q') => break, - 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('d') => { - let message = if ui_state.filter == Filter::Done { - ui_state.filter = Filter::None; - "Disabled filter DONE" - } else { - ui_state.filter = Filter::Done; - "Enabled filter DONE │ Press d again to disable the filter" - }; - - ui_state = ui_state.with_updated_rows(); - ui_state.message.push_str(message); + list_state.redraw(stdout)?; } - KeyCode::Char('p') => { - let message = if ui_state.filter == Filter::Pending { - ui_state.filter = Filter::None; - "Disabled filter PENDING" - } else { - ui_state.filter = Filter::Pending; - "Enabled filter PENDING │ Press p again to disable the filter" - }; - - ui_state = ui_state.with_updated_rows(); - ui_state.message.push_str(message); - } - KeyCode::Char('r') => { - ui_state = ui_state.with_reset_selected()?; - } - KeyCode::Char('c') => { - ui_state.selected_to_current_exercise()?; - break; - } - _ => (), + // Redraw + Event::Resize(_, _) => list_state.redraw(stdout)?, + // Ignore + Event::FocusGained | Event::FocusLost => (), } } - - Ok(()) } pub fn list(app_state: &mut AppState) -> Result<()> { let mut stdout = io::stdout().lock(); stdout .queue(EnterAlternateScreen)? - .queue(EnableMouseCapture)? - .flush()?; + .queue(cursor::Hide)? + .queue(DisableLineWrap)? + .queue(EnableMouseCapture)?; enable_raw_mode()?; let res = handle_list(app_state, &mut stdout); @@ -97,6 +103,8 @@ pub fn list(app_state: &mut AppState) -> Result<()> { // Restore the terminal even if we got an error. stdout .queue(LeaveAlternateScreen)? + .queue(cursor::Show)? + .queue(EnableLineWrap)? .queue(DisableMouseCapture)? .flush()?; disable_raw_mode()?; diff --git a/src/list/state.rs b/src/list/state.rs index 48a404f3..cf147b44 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -1,14 +1,19 @@ use anyhow::{Context, Result}; -use ratatui::{ - layout::{Constraint, Rect}, - style::{Style, Stylize}, - text::{Span, Text}, - widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState}, - Frame, +use crossterm::{ + cursor::{MoveDown, MoveTo}, + style::{Color, ResetColor, SetForegroundColor}, + terminal::{self, BeginSynchronizedUpdate, EndSynchronizedUpdate}, + QueueableCommand, +}; +use std::{ + fmt::Write as _, + io::{self, StdoutLock, Write as _}, }; -use std::{fmt::Write, mem}; -use crate::{app_state::AppState, progress_bar::progress_bar_ratatui}; +use crate::{app_state::AppState, term::clear_terminal, MAX_EXERCISE_NAME_LEN}; + +// +1 for padding. +const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; #[derive(Copy, Clone, PartialEq, Eq)] pub enum Filter { @@ -17,230 +22,213 @@ pub enum Filter { None, } -pub struct UiState<'a> { - pub table: Table<'static>, +pub struct ListState<'a> { pub message: String, - pub filter: Filter, + filter: Filter, app_state: &'a mut AppState, - table_state: TableState, - n_rows: usize, + n_rows_with_filter: usize, + name_col_width: usize, + offset: usize, + selected: Option, } -impl<'a> UiState<'a> { - pub fn with_updated_rows(mut self) -> Self { - let current_exercise_ind = self.app_state.current_exercise_ind(); - - self.n_rows = 0; - let rows = self - .app_state - .exercises() - .iter() - .enumerate() - .filter_map(|(ind, exercise)| { - let exercise_state = if exercise.done { - if self.filter == Filter::Pending { - return None; - } - - "DONE".green() - } else { - if self.filter == Filter::Done { - return None; - } - - "PENDING".yellow() - }; - - self.n_rows += 1; - - let next = if ind == current_exercise_ind { - ">>>>".bold().red() - } else { - Span::default() - }; - - Some(Row::new([ - next, - exercise_state, - Span::raw(exercise.name), - Span::raw(exercise.path), - ])) - }); - - self.table = self.table.rows(rows); - - if self.n_rows == 0 { - self.table_state.select(None); - } else { - self.table_state.select(Some( - self.table_state - .selected() - .map_or(0, |selected| selected.min(self.n_rows - 1)), - )); - } - - self - } - - pub fn new(app_state: &'a mut AppState) -> Self { - let header = Row::new(["Next", "State", "Name", "Path"]); - - let max_name_len = app_state +impl<'a> ListState<'a> { + pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result { + let name_col_width = app_state .exercises() .iter() .map(|exercise| exercise.name.len()) .max() - .unwrap_or(4) as u16; + .map_or(4, |max| max.max(4)); - let widths = [ - Constraint::Length(4), - Constraint::Length(7), - Constraint::Length(max_name_len), - Constraint::Fill(1), - ]; - - let table = Table::default() - .widths(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)); + clear_terminal(stdout)?; + stdout.write_all(b" Current State Name ")?; + stdout.write_all(&SPACE[..name_col_width - 4])?; + stdout.write_all(b"Path\r\n")?; let selected = app_state.current_exercise_ind(); - let table_state = TableState::default() - .with_offset(selected.saturating_sub(10)) - .with_selected(Some(selected)); + let n_rows_with_filter = app_state.exercises().len(); - let filter = Filter::None; - let n_rows = app_state.exercises().len(); - - let slf = Self { - table, + let mut slf = Self { message: String::with_capacity(128), - filter, + filter: Filter::None, app_state, - table_state, - n_rows, + n_rows_with_filter, + name_col_width, + offset: selected.saturating_sub(10), + selected: Some(selected), }; - slf.with_updated_rows() + slf.redraw(stdout)?; + + Ok(slf) + } + + #[inline] + pub fn filter(&self) -> Filter { + self.filter + } + + pub fn set_filter(&mut self, filter: Filter) { + self.filter = filter; + self.n_rows_with_filter = match filter { + Filter::Done => self + .app_state + .exercises() + .iter() + .filter(|exercise| !exercise.done) + .count(), + Filter::Pending => self + .app_state + .exercises() + .iter() + .filter(|exercise| exercise.done) + .count(), + Filter::None => self.app_state.exercises().len(), + }; + + if self.n_rows_with_filter == 0 { + self.selected = None; + } else { + self.selected = Some( + self.selected + .map_or(0, |selected| selected.min(self.n_rows_with_filter - 1)), + ); + } } pub fn select_next(&mut self) { - if self.n_rows > 0 { - let next = self - .table_state - .selected() - .map_or(0, |selected| (selected + 1).min(self.n_rows - 1)); - self.table_state.select(Some(next)); + if self.n_rows_with_filter > 0 { + let next = self.selected.map_or(0, |selected| { + (selected + 1).min(self.n_rows_with_filter - 1) + }); + self.selected = Some(next); } } pub fn select_previous(&mut self) { - if self.n_rows > 0 { + if self.n_rows_with_filter > 0 { let previous = self - .table_state - .selected() + .selected .map_or(0, |selected| selected.saturating_sub(1)); - self.table_state.select(Some(previous)); + self.selected = Some(previous); } } pub fn select_first(&mut self) { - if self.n_rows > 0 { - self.table_state.select(Some(0)); + if self.n_rows_with_filter > 0 { + self.selected = Some(0); } } pub fn select_last(&mut self) { - if self.n_rows > 0 { - self.table_state.select(Some(self.n_rows - 1)); + if self.n_rows_with_filter > 0 { + self.selected = Some(self.n_rows_with_filter - 1); } } - pub fn draw(&mut self, frame: &mut Frame) -> Result<()> { - let area = frame.area(); - let narrow = area.width < 95; + pub fn redraw(&mut self, stdout: &mut StdoutLock) -> io::Result<()> { + stdout.queue(BeginSynchronizedUpdate)?; + stdout.queue(MoveTo(0, 1))?; + let (width, height) = terminal::size()?; + let narrow = width < 95; let narrow_u16 = u16::from(narrow); - let table_height = area.height - 3 - narrow_u16; + let max_n_rows_to_display = height.saturating_sub(narrow_u16 + 4); - frame.render_stateful_widget( - &self.table, - Rect { - x: 0, - y: 0, - width: area.width, - height: table_height, - }, - &mut self.table_state, - ); + let displayed_exercises = self + .app_state + .exercises() + .iter() + .enumerate() + .filter(|(_, exercise)| match self.filter { + Filter::Done => exercise.done, + Filter::Pending => !exercise.done, + Filter::None => true, + }) + .skip(self.offset) + .take(max_n_rows_to_display as usize); - frame.render_widget( - Paragraph::new(progress_bar_ratatui( - self.app_state.n_done(), - self.app_state.exercises().len() as u16, - area.width, - )?) - .block(Block::default().borders(Borders::BOTTOM)), - Rect { - x: 0, - y: table_height, - width: area.width, - height: 2, - }, - ); - - let message = if self.message.is_empty() { - // Help footer. - let mut text = Text::default(); - let mut spans = Vec::with_capacity(4); - spans.push(Span::raw( - "↓/j ↑/k home/g end/G │ ontinue at │ eset exercise │", - )); - - if narrow { - text.push_line(mem::take(&mut spans)); - spans.push(Span::raw("filter ")); + let mut n_displayed_rows: u16 = 0; + let current_exercise_ind = self.app_state.current_exercise_ind(); + for (ind, exercise) in displayed_exercises { + if self.selected == Some(n_displayed_rows as usize) { + write!(stdout, "🦀")?; } else { - spans.push(Span::raw(" filter ")); + stdout.write_all(b" ")?; } - match self.filter { - Filter::Done => { - spans.push("one".underlined().magenta()); - spans.push(Span::raw("/

ending")); - } - Filter::Pending => { - spans.push(Span::raw("one/")); - spans.push("

ending".underlined().magenta()); - } - Filter::None => spans.push(Span::raw("one/

ending")), + if ind == current_exercise_ind { + stdout.queue(SetForegroundColor(Color::Red))?; + stdout.write_all(b">>>>>>> ")?; + } else { + stdout.write_all(b" ")?; } - spans.push(Span::raw(" │ uit list")); - text.push_line(spans); - text - } else { - Text::from(self.message.as_str().light_blue()) - }; - frame.render_widget( - message, - Rect { - x: 0, - y: table_height + 2, - width: area.width, - height: 1 + narrow_u16, - }, - ); + if exercise.done { + stdout.queue(SetForegroundColor(Color::Yellow))?; + stdout.write_all(b"DONE ")?; + } else { + stdout.queue(SetForegroundColor(Color::Green))?; + stdout.write_all(b"PENDING ")?; + } + + stdout.queue(ResetColor)?; + + stdout.write_all(exercise.name.as_bytes())?; + stdout.write_all(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?; + + stdout.write_all(exercise.path.as_bytes())?; + stdout.write_all(b"\r\n")?; + + n_displayed_rows += 1; + } + + stdout.queue(MoveDown(max_n_rows_to_display - n_displayed_rows))?; + + // TODO + // let message = if self.message.is_empty() { + // // Help footer. + // let mut text = Text::default(); + // let mut spans = Vec::with_capacity(4); + // spans.push(Span::raw( + // "↓/j ↑/k home/g end/G │ ontinue at │ eset exercise │", + // )); + + // if narrow { + // text.push_line(mem::take(&mut spans)); + // spans.push(Span::raw("filter ")); + // } else { + // spans.push(Span::raw(" filter ")); + // } + + // match self.filter { + // Filter::Done => { + // spans.push("one".underlined().magenta()); + // spans.push(Span::raw("/

ending")); + // } + // Filter::Pending => { + // spans.push(Span::raw("one/")); + // spans.push("

ending".underlined().magenta()); + // } + // Filter::None => spans.push(Span::raw("one/

ending")), + // } + + // spans.push(Span::raw(" │ uit list")); + // text.push_line(spans); + // text + // } else { + // Text::from(self.message.as_str().light_blue()) + // }; + + stdout.queue(EndSynchronizedUpdate)?; + stdout.flush()?; Ok(()) } - pub fn with_reset_selected(mut self) -> Result { - let Some(selected) = self.table_state.selected() else { - return Ok(self); + pub fn reset_selected(&mut self) -> Result<()> { + let Some(selected) = self.selected else { + return Ok(()); }; let ind = self @@ -259,11 +247,12 @@ impl<'a> UiState<'a> { let exercise_path = self.app_state.reset_exercise_by_ind(ind)?; write!(self.message, "The exercise {exercise_path} has been reset")?; - Ok(self.with_updated_rows()) + Ok(()) } pub fn selected_to_current_exercise(&mut self) -> Result<()> { - let Some(selected) = self.table_state.selected() else { + let Some(selected) = self.selected else { + // TODO: Don't exit list return Ok(()); }; diff --git a/src/main.rs b/src/main.rs index 0855d435..59513671 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ mod terminal_link; mod watch; const CURRENT_FORMAT_VERSION: u8 = 1; +const MAX_EXERCISE_NAME_LEN: usize = 32; /// Rustlings is a collection of small exercises to get you used to writing and reading Rust code #[derive(Parser)] diff --git a/src/progress_bar.rs b/src/progress_bar.rs index 7f07ad59..837c4c78 100644 --- a/src/progress_bar.rs +++ b/src/progress_bar.rs @@ -1,100 +1,53 @@ -use anyhow::{bail, Result}; -use ratatui::text::{Line, Span}; -use std::fmt::Write; +use std::io::{self, StdoutLock, Write}; -const PREFIX: &str = "Progress: ["; +use crossterm::{ + style::{Color, ResetColor, SetForegroundColor}, + QueueableCommand, +}; + +const PREFIX: &[u8] = b"Progress: ["; const PREFIX_WIDTH: u16 = PREFIX.len() as u16; // Leaving the last char empty (_) for `total` > 99. const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16; const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4; -const PROGRESS_EXCEEDS_MAX_ERR: &str = - "The progress of the progress bar is higher than the maximum"; - /// Terminal progress bar to be used when not using Ratataui. -pub fn progress_bar(progress: u16, total: u16, line_width: u16) -> Result { - use ratatui::crossterm::style::Stylize; - - if progress > total { - bail!(PROGRESS_EXCEEDS_MAX_ERR); - } +pub fn progress_bar( + stdout: &mut StdoutLock, + progress: u16, + total: u16, + line_width: u16, +) -> io::Result<()> { + debug_assert!(progress <= total); if line_width < MIN_LINE_WIDTH { - return Ok(format!("Progress: {progress}/{total} exercises")); + return write!(stdout, "Progress: {progress}/{total} exercises"); } - let mut line = String::with_capacity(usize::from(line_width)); - line.push_str(PREFIX); + stdout.write_all(PREFIX)?; let width = line_width - WRAPPER_WIDTH; let filled = (width * progress) / total; - let mut green_part = String::with_capacity(usize::from(filled + 1)); + stdout.queue(SetForegroundColor(Color::Green))?; for _ in 0..filled { - green_part.push('#'); + stdout.write_all(b"#")?; } if filled < width { - green_part.push('>'); + stdout.write_all(b">")?; } - write!(line, "{}", green_part.green()).unwrap(); let width_minus_filled = width - filled; if width_minus_filled > 1 { let red_part_width = width_minus_filled - 1; - let mut red_part = String::with_capacity(usize::from(red_part_width)); + stdout.queue(SetForegroundColor(Color::Red))?; for _ in 0..red_part_width { - red_part.push('-'); + stdout.write_all(b"-")?; } - write!(line, "{}", red_part.red()).unwrap(); } - writeln!(line, "] {progress:>3}/{total} exercises").unwrap(); - - Ok(line) -} - -/// Progress bar to be used with Ratataui. -// Not using Ratatui's Gauge widget to keep the progress bar consistent. -pub fn progress_bar_ratatui(progress: u16, total: u16, line_width: u16) -> Result> { - use ratatui::style::Stylize; - - if progress > total { - bail!(PROGRESS_EXCEEDS_MAX_ERR); - } - - if line_width < MIN_LINE_WIDTH { - return Ok(Line::raw(format!("Progress: {progress}/{total} exercises"))); - } - - let mut spans = Vec::with_capacity(4); - spans.push(Span::raw(PREFIX)); - - let width = line_width - WRAPPER_WIDTH; - let filled = (width * progress) / total; - - let mut green_part = String::with_capacity(usize::from(filled + 1)); - for _ in 0..filled { - green_part.push('#'); - } - - if filled < width { - green_part.push('>'); - } - spans.push(green_part.green()); - - let width_minus_filled = width - filled; - if width_minus_filled > 1 { - let red_part_width = width_minus_filled - 1; - let mut red_part = String::with_capacity(usize::from(red_part_width)); - for _ in 0..red_part_width { - red_part.push('-'); - } - spans.push(red_part.red()); - } - - spans.push(Span::raw(format!("] {progress:>3}/{total} exercises"))); - - Ok(Line::from(spans)) + stdout.queue(ResetColor)?; + write!(stdout, "] {progress:>3}/{total} exercises") } diff --git a/src/run.rs b/src/run.rs index 0bc965cd..09e53ec9 100644 --- a/src/run.rs +++ b/src/run.rs @@ -1,5 +1,5 @@ use anyhow::{bail, Result}; -use ratatui::crossterm::style::{style, Stylize}; +use crossterm::style::{style, Stylize}; use std::io::{self, Write}; use crate::{ diff --git a/src/term.rs b/src/term.rs index e1ac3da9..07edf900 100644 --- a/src/term.rs +++ b/src/term.rs @@ -1,7 +1,17 @@ use std::io::{self, BufRead, StdoutLock, Write}; +use crossterm::{ + cursor::MoveTo, + terminal::{Clear, ClearType}, + QueueableCommand, +}; + pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> { - stdout.write_all(b"\x1b[H\x1b[2J\x1b[3J") + stdout + .queue(MoveTo(0, 0))? + .queue(Clear(ClearType::All))? + .queue(Clear(ClearType::Purge)) + .map(|_| ()) } pub fn press_enter_prompt(stdout: &mut StdoutLock) -> io::Result<()> { diff --git a/src/watch/state.rs b/src/watch/state.rs index 6bf8e692..26c83d50 100644 --- a/src/watch/state.rs +++ b/src/watch/state.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use ratatui::crossterm::{ +use crossterm::{ style::{style, Stylize}, terminal, }; @@ -76,7 +76,8 @@ impl<'a> WatchState<'a> { self.done_status = DoneStatus::Pending; } - self.render() + self.render()?; + Ok(()) } pub fn handle_file_change(&mut self, exercise_ind: usize) -> Result<()> { @@ -120,7 +121,7 @@ impl<'a> WatchState<'a> { self.writer.flush() } - pub fn render(&mut self) -> Result<()> { + pub fn render(&mut self) -> io::Result<()> { // Prevent having the first line shifted if clearing wasn't successful. self.writer.write_all(b"\n")?; clear_terminal(&mut self.writer)?; @@ -155,14 +156,15 @@ impl<'a> WatchState<'a> { } let line_width = terminal::size()?.0; - let progress_bar = progress_bar( + progress_bar( + &mut self.writer, self.app_state.n_done(), self.app_state.exercises().len() as u16, line_width, )?; writeln!( self.writer, - "{progress_bar}Current exercise: {}", + "\nCurrent exercise: {}", self.app_state.current_exercise().terminal_link(), )?; @@ -171,7 +173,7 @@ impl<'a> WatchState<'a> { Ok(()) } - pub fn show_hint(&mut self) -> Result<()> { + pub fn show_hint(&mut self) -> io::Result<()> { self.show_hint = true; self.render() } diff --git a/src/watch/terminal_event.rs b/src/watch/terminal_event.rs index 3a1762d3..3e8c272a 100644 --- a/src/watch/terminal_event.rs +++ b/src/watch/terminal_event.rs @@ -1,4 +1,4 @@ -use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; +use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers}; use std::sync::mpsc::Sender; use super::WatchEvent; @@ -78,7 +78,7 @@ pub fn terminal_event_handler(tx: Sender, manual_run: bool) { return; } } - Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => continue, + Event::FocusGained | Event::FocusLost | Event::Mouse(_) => continue, } }; From 4e12725616abe1918d6a4f21b23288dfac237cc4 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Aug 2024 00:23:45 +0200 Subject: [PATCH 2/8] Don't exit the list on "to current" if nothing is selected --- src/list.rs | 4 +++- src/list/state.rs | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/list.rs b/src/list.rs index 754c5e2f..27a31d13 100644 --- a/src/list.rs +++ b/src/list.rs @@ -63,7 +63,9 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> list_state.reset_selected()?; } KeyCode::Char('c') => { - return list_state.selected_to_current_exercise(); + if list_state.selected_to_current_exercise()? { + return Ok(()); + } } // Redraw to remove the message. KeyCode::Esc => (), diff --git a/src/list/state.rs b/src/list/state.rs index cf147b44..645c768f 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -250,25 +250,27 @@ impl<'a> ListState<'a> { Ok(()) } - pub fn selected_to_current_exercise(&mut self) -> Result<()> { + // Return `true` if there was something to select. + pub fn selected_to_current_exercise(&mut self) -> Result { let Some(selected) = self.selected else { - // TODO: Don't exit list - return Ok(()); + self.message.push_str("Nothing selected to continue at!"); + return Ok(false); }; - let ind = self + let (ind, _) = self .app_state .exercises() .iter() .enumerate() - .filter_map(|(ind, exercise)| match self.filter { - Filter::Done => exercise.done.then_some(ind), - Filter::Pending => (!exercise.done).then_some(ind), - Filter::None => Some(ind), + .filter(|(_, exercise)| match self.filter { + Filter::Done => exercise.done, + Filter::Pending => !exercise.done, + Filter::None => true, }) .nth(selected) .context("Invalid selection index")?; - self.app_state.set_current_exercise_ind(ind) + self.app_state.set_current_exercise_ind(ind)?; + Ok(true) } } From b779c431268da50989257056d21a870a61a1702e Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Aug 2024 17:17:56 +0200 Subject: [PATCH 3/8] Almost done with list display --- src/list.rs | 35 +++-- src/list/state.rs | 334 ++++++++++++++++++++++++++------------------ src/main.rs | 1 - src/progress_bar.rs | 53 ------- src/term.rs | 48 +++++++ src/watch/state.rs | 2 +- 6 files changed, 260 insertions(+), 213 deletions(-) delete mode 100644 src/progress_bar.rs diff --git a/src/list.rs b/src/list.rs index 27a31d13..a571eeec 100644 --- a/src/list.rs +++ b/src/list.rs @@ -38,15 +38,15 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> KeyCode::Home | KeyCode::Char('g') => list_state.select_first(), KeyCode::End | KeyCode::Char('G') => list_state.select_last(), KeyCode::Char('d') => { - let message = if list_state.filter() == Filter::Done { + if list_state.filter() == Filter::Done { list_state.set_filter(Filter::None); - "Disabled filter DONE" + list_state.message.push_str("Disabled filter DONE"); } else { list_state.set_filter(Filter::Done); - "Enabled filter DONE │ Press d again to disable the filter" - }; - - list_state.message.push_str(message); + list_state.message.push_str( + "Enabled filter DONE │ Press d again to disable the filter", + ); + } } KeyCode::Char('p') => { let message = if list_state.filter() == Filter::Pending { @@ -71,23 +71,20 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> KeyCode::Esc => (), _ => continue, } - - list_state.redraw(stdout)?; } - Event::Mouse(event) => { - match event.kind { - MouseEventKind::ScrollDown => list_state.select_next(), - MouseEventKind::ScrollUp => list_state.select_previous(), - _ => continue, - } - - list_state.redraw(stdout)?; + Event::Mouse(event) => match event.kind { + MouseEventKind::ScrollDown => list_state.select_next(), + MouseEventKind::ScrollUp => list_state.select_previous(), + _ => continue, + }, + Event::Resize(width, height) => { + list_state.set_term_size(width, height); } - // Redraw - Event::Resize(_, _) => list_state.redraw(stdout)?, // Ignore - Event::FocusGained | Event::FocusLost => (), + Event::FocusGained | Event::FocusLost => continue, } + + list_state.redraw(stdout)?; } } diff --git a/src/list/state.rs b/src/list/state.rs index 645c768f..d8744352 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -1,19 +1,30 @@ use anyhow::{Context, Result}; use crossterm::{ - cursor::{MoveDown, MoveTo}, - style::{Color, ResetColor, SetForegroundColor}, - terminal::{self, BeginSynchronizedUpdate, EndSynchronizedUpdate}, + cursor::{MoveTo, MoveToNextLine}, + style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, + terminal::{self, BeginSynchronizedUpdate, Clear, ClearType, EndSynchronizedUpdate}, QueueableCommand, }; use std::{ fmt::Write as _, - io::{self, StdoutLock, Write as _}, + io::{self, StdoutLock, Write}, }; -use crate::{app_state::AppState, term::clear_terminal, MAX_EXERCISE_NAME_LEN}; +use crate::{app_state::AppState, term::progress_bar, MAX_EXERCISE_NAME_LEN}; -// +1 for padding. -const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; +fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> { + if CLEAR_LAST_CHAR { + // Avoids having the last written char as the last displayed one when + // the written width is higher than the terminal width. + // Happens on the Gnome terminal for example. + stdout.write_all(b" ")?; + } + + stdout + .queue(Clear(ClearType::UntilNewLine))? + .queue(MoveToNextLine(1))?; + Ok(()) +} #[derive(Copy, Clone, PartialEq, Eq)] pub enum Filter { @@ -30,10 +41,16 @@ pub struct ListState<'a> { name_col_width: usize, offset: usize, selected: Option, + term_width: u16, + term_height: u16, + separator: Vec, } impl<'a> ListState<'a> { pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result { + let (term_width, term_height) = terminal::size()?; + stdout.queue(Clear(ClearType::All))?; + let name_col_width = app_state .exercises() .iter() @@ -41,13 +58,8 @@ impl<'a> ListState<'a> { .max() .map_or(4, |max| max.max(4)); - clear_terminal(stdout)?; - stdout.write_all(b" Current State Name ")?; - stdout.write_all(&SPACE[..name_col_width - 4])?; - stdout.write_all(b"Path\r\n")?; - - let selected = app_state.current_exercise_ind(); let n_rows_with_filter = app_state.exercises().len(); + let selected = app_state.current_exercise_ind(); let mut slf = Self { message: String::with_capacity(128), @@ -57,6 +69,9 @@ impl<'a> ListState<'a> { name_col_width, offset: selected.saturating_sub(10), selected: Some(selected), + term_width, + term_height, + separator: "─".as_bytes().repeat(term_width as usize), }; slf.redraw(stdout)?; @@ -64,6 +79,145 @@ impl<'a> ListState<'a> { Ok(slf) } + pub fn redraw(&mut self, stdout: &mut StdoutLock) -> io::Result<()> { + if self.term_height == 0 { + return Ok(()); + } + + stdout.queue(BeginSynchronizedUpdate)?.queue(MoveTo(0, 0))?; + + // +1 for padding. + const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; + stdout.write_all(b" Current State Name")?; + stdout.write_all(&SPACE[..self.name_col_width - 2])?; + stdout.write_all(b"Path")?; + next_ln::(stdout)?; + + let narrow = self.term_width < 96; + let show_footer = self.term_height > 6; + let max_n_rows_to_display = + (self.term_height - 1 - u16::from(show_footer) * (4 + u16::from(narrow))) as usize; + + let displayed_exercises = self + .app_state + .exercises() + .iter() + .enumerate() + .filter(|(_, exercise)| match self.filter { + Filter::Done => exercise.done, + Filter::Pending => !exercise.done, + Filter::None => true, + }) + .skip(self.offset) + .take(max_n_rows_to_display); + + let current_exercise_ind = self.app_state.current_exercise_ind(); + let mut n_displayed_rows = 0; + for (exercise_ind, exercise) in displayed_exercises { + if self.selected == Some(n_displayed_rows) { + stdout.write_all("🦀".as_bytes())?; + } else { + stdout.write_all(b" ")?; + } + + if exercise_ind == current_exercise_ind { + stdout.queue(SetForegroundColor(Color::Red))?; + stdout.write_all(b">>>>>>> ")?; + } else { + stdout.write_all(b" ")?; + } + + if exercise.done { + stdout.queue(SetForegroundColor(Color::Yellow))?; + stdout.write_all(b"DONE ")?; + } else { + stdout.queue(SetForegroundColor(Color::Green))?; + stdout.write_all(b"PENDING ")?; + } + + stdout.queue(ResetColor)?; + + stdout.write_all(exercise.name.as_bytes())?; + stdout.write_all(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?; + + stdout.write_all(exercise.path.as_bytes())?; + + next_ln::(stdout)?; + n_displayed_rows += 1; + } + + for _ in 0..max_n_rows_to_display - n_displayed_rows { + next_ln::(stdout)?; + } + + if show_footer { + stdout.write_all(&self.separator)?; + next_ln::(stdout)?; + + progress_bar( + stdout, + self.app_state.n_done(), + self.app_state.exercises().len() as u16, + self.term_width, + )?; + next_ln::(stdout)?; + + stdout.write_all(&self.separator)?; + next_ln::(stdout)?; + + if self.message.is_empty() { + // Help footer. + stdout.write_all( + "↓/j ↑/k home/g end/G │ ontinue at │ eset exercise │".as_bytes(), + )?; + if narrow { + next_ln::(stdout)?; + stdout.write_all(b"filter ")?; + } else { + stdout.write_all(b" filter ")?; + } + + match self.filter { + Filter::Done => { + stdout + .queue(SetForegroundColor(Color::Magenta))? + .queue(SetAttribute(Attribute::Underlined))?; + stdout.write_all(b"one")?; + stdout.queue(ResetColor)?; + stdout.write_all(b"/

ending")?; + } + Filter::Pending => { + stdout.write_all(b"one/")?; + stdout + .queue(SetForegroundColor(Color::Magenta))? + .queue(SetAttribute(Attribute::Underlined))?; + stdout.write_all(b"

ending")?; + stdout.queue(ResetColor)?; + } + Filter::None => stdout.write_all(b"one/

ending")?, + } + stdout.write_all(" │ uit list".as_bytes())?; + next_ln::(stdout)?; + } else { + stdout.queue(SetForegroundColor(Color::Magenta))?; + stdout.write_all(self.message.as_bytes())?; + stdout.queue(ResetColor)?; + next_ln::(stdout)?; + if narrow { + next_ln::(stdout)?; + } + } + } + + stdout.queue(EndSynchronizedUpdate)?.flush() + } + + pub fn set_term_size(&mut self, width: u16, height: u16) { + self.term_width = width; + self.term_height = height; + self.separator = "─".as_bytes().repeat(width as usize); + } + #[inline] pub fn filter(&self) -> Filter { self.filter @@ -76,13 +230,13 @@ impl<'a> ListState<'a> { .app_state .exercises() .iter() - .filter(|exercise| !exercise.done) + .filter(|exercise| exercise.done) .count(), Filter::Pending => self .app_state .exercises() .iter() - .filter(|exercise| exercise.done) + .filter(|exercise| !exercise.done) .count(), Filter::None => self.app_state.exercises().len(), }; @@ -127,124 +281,38 @@ impl<'a> ListState<'a> { } } - pub fn redraw(&mut self, stdout: &mut StdoutLock) -> io::Result<()> { - stdout.queue(BeginSynchronizedUpdate)?; - stdout.queue(MoveTo(0, 1))?; - let (width, height) = terminal::size()?; - let narrow = width < 95; - let narrow_u16 = u16::from(narrow); - let max_n_rows_to_display = height.saturating_sub(narrow_u16 + 4); - - let displayed_exercises = self - .app_state - .exercises() - .iter() - .enumerate() - .filter(|(_, exercise)| match self.filter { - Filter::Done => exercise.done, - Filter::Pending => !exercise.done, - Filter::None => true, - }) - .skip(self.offset) - .take(max_n_rows_to_display as usize); - - let mut n_displayed_rows: u16 = 0; - let current_exercise_ind = self.app_state.current_exercise_ind(); - for (ind, exercise) in displayed_exercises { - if self.selected == Some(n_displayed_rows as usize) { - write!(stdout, "🦀")?; - } else { - stdout.write_all(b" ")?; - } - - if ind == current_exercise_ind { - stdout.queue(SetForegroundColor(Color::Red))?; - stdout.write_all(b">>>>>>> ")?; - } else { - stdout.write_all(b" ")?; - } - - if exercise.done { - stdout.queue(SetForegroundColor(Color::Yellow))?; - stdout.write_all(b"DONE ")?; - } else { - stdout.queue(SetForegroundColor(Color::Green))?; - stdout.write_all(b"PENDING ")?; - } - - stdout.queue(ResetColor)?; - - stdout.write_all(exercise.name.as_bytes())?; - stdout.write_all(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?; - - stdout.write_all(exercise.path.as_bytes())?; - stdout.write_all(b"\r\n")?; - - n_displayed_rows += 1; + fn selected_to_exercise_ind(&self, selected: usize) -> Result { + match self.filter { + Filter::Done => self + .app_state + .exercises() + .iter() + .enumerate() + .filter(|(_, exercise)| exercise.done) + .nth(selected) + .context("Invalid selection index") + .map(|(ind, _)| ind), + Filter::Pending => self + .app_state + .exercises() + .iter() + .enumerate() + .filter(|(_, exercise)| !exercise.done) + .nth(selected) + .context("Invalid selection index") + .map(|(ind, _)| ind), + Filter::None => Ok(selected), } - - stdout.queue(MoveDown(max_n_rows_to_display - n_displayed_rows))?; - - // TODO - // let message = if self.message.is_empty() { - // // Help footer. - // let mut text = Text::default(); - // let mut spans = Vec::with_capacity(4); - // spans.push(Span::raw( - // "↓/j ↑/k home/g end/G │ ontinue at │ eset exercise │", - // )); - - // if narrow { - // text.push_line(mem::take(&mut spans)); - // spans.push(Span::raw("filter ")); - // } else { - // spans.push(Span::raw(" filter ")); - // } - - // match self.filter { - // Filter::Done => { - // spans.push("one".underlined().magenta()); - // spans.push(Span::raw("/

ending")); - // } - // Filter::Pending => { - // spans.push(Span::raw("one/")); - // spans.push("

ending".underlined().magenta()); - // } - // Filter::None => spans.push(Span::raw("one/

ending")), - // } - - // spans.push(Span::raw(" │ uit list")); - // text.push_line(spans); - // text - // } else { - // Text::from(self.message.as_str().light_blue()) - // }; - - stdout.queue(EndSynchronizedUpdate)?; - stdout.flush()?; - - Ok(()) } pub fn reset_selected(&mut self) -> Result<()> { let Some(selected) = self.selected else { + self.message.push_str("Nothing selected to reset!"); return Ok(()); }; - let ind = self - .app_state - .exercises() - .iter() - .enumerate() - .filter_map(|(ind, exercise)| match self.filter { - Filter::Done => exercise.done.then_some(ind), - Filter::Pending => (!exercise.done).then_some(ind), - Filter::None => Some(ind), - }) - .nth(selected) - .context("Invalid selection index")?; - - let exercise_path = self.app_state.reset_exercise_by_ind(ind)?; + let exercise_ind = self.selected_to_exercise_ind(selected)?; + let exercise_path = self.app_state.reset_exercise_by_ind(exercise_ind)?; write!(self.message, "The exercise {exercise_path} has been reset")?; Ok(()) @@ -257,20 +325,8 @@ impl<'a> ListState<'a> { return Ok(false); }; - let (ind, _) = self - .app_state - .exercises() - .iter() - .enumerate() - .filter(|(_, exercise)| match self.filter { - Filter::Done => exercise.done, - Filter::Pending => !exercise.done, - Filter::None => true, - }) - .nth(selected) - .context("Invalid selection index")?; - - self.app_state.set_current_exercise_ind(ind)?; + let exercise_ind = self.selected_to_exercise_ind(selected)?; + self.app_state.set_current_exercise_ind(exercise_ind)?; Ok(true) } } diff --git a/src/main.rs b/src/main.rs index 59513671..61dd8ea8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,6 @@ mod exercise; mod info_file; mod init; mod list; -mod progress_bar; mod run; mod term; mod terminal_link; diff --git a/src/progress_bar.rs b/src/progress_bar.rs deleted file mode 100644 index 837c4c78..00000000 --- a/src/progress_bar.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::io::{self, StdoutLock, Write}; - -use crossterm::{ - style::{Color, ResetColor, SetForegroundColor}, - QueueableCommand, -}; - -const PREFIX: &[u8] = b"Progress: ["; -const PREFIX_WIDTH: u16 = PREFIX.len() as u16; -// Leaving the last char empty (_) for `total` > 99. -const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16; -const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; -const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4; - -/// Terminal progress bar to be used when not using Ratataui. -pub fn progress_bar( - stdout: &mut StdoutLock, - progress: u16, - total: u16, - line_width: u16, -) -> io::Result<()> { - debug_assert!(progress <= total); - - if line_width < MIN_LINE_WIDTH { - return write!(stdout, "Progress: {progress}/{total} exercises"); - } - - stdout.write_all(PREFIX)?; - - let width = line_width - WRAPPER_WIDTH; - let filled = (width * progress) / total; - - stdout.queue(SetForegroundColor(Color::Green))?; - for _ in 0..filled { - stdout.write_all(b"#")?; - } - - if filled < width { - stdout.write_all(b">")?; - } - - let width_minus_filled = width - filled; - if width_minus_filled > 1 { - let red_part_width = width_minus_filled - 1; - stdout.queue(SetForegroundColor(Color::Red))?; - for _ in 0..red_part_width { - stdout.write_all(b"-")?; - } - } - - stdout.queue(ResetColor)?; - write!(stdout, "] {progress:>3}/{total} exercises") -} diff --git a/src/term.rs b/src/term.rs index 07edf900..b993108e 100644 --- a/src/term.rs +++ b/src/term.rs @@ -2,10 +2,58 @@ use std::io::{self, BufRead, StdoutLock, Write}; use crossterm::{ cursor::MoveTo, + style::{Color, ResetColor, SetForegroundColor}, terminal::{Clear, ClearType}, QueueableCommand, }; +/// Terminal progress bar to be used when not using Ratataui. +pub fn progress_bar( + stdout: &mut StdoutLock, + progress: u16, + total: u16, + line_width: u16, +) -> io::Result<()> { + debug_assert!(progress <= total); + + const PREFIX: &[u8] = b"Progress: ["; + const PREFIX_WIDTH: u16 = PREFIX.len() as u16; + // Leaving the last char empty (_) for `total` > 99. + const POSTFIX_WIDTH: u16 = "] xxx/xx exercises_".len() as u16; + const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH; + const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4; + + if line_width < MIN_LINE_WIDTH { + return write!(stdout, "Progress: {progress}/{total} exercises"); + } + + stdout.write_all(PREFIX)?; + + let width = line_width - WRAPPER_WIDTH; + let filled = (width * progress) / total; + + stdout.queue(SetForegroundColor(Color::Green))?; + for _ in 0..filled { + stdout.write_all(b"#")?; + } + + if filled < width { + stdout.write_all(b">")?; + } + + let width_minus_filled = width - filled; + if width_minus_filled > 1 { + let red_part_width = width_minus_filled - 1; + stdout.queue(SetForegroundColor(Color::Red))?; + for _ in 0..red_part_width { + stdout.write_all(b"-")?; + } + } + + stdout.queue(ResetColor)?; + write!(stdout, "] {progress:>3}/{total} exercises") +} + pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> { stdout .queue(MoveTo(0, 0))? diff --git a/src/watch/state.rs b/src/watch/state.rs index 26c83d50..40e3d3ec 100644 --- a/src/watch/state.rs +++ b/src/watch/state.rs @@ -9,7 +9,7 @@ use crate::{ app_state::{AppState, ExercisesProgress}, clear_terminal, exercise::{RunnableExercise, OUTPUT_CAPACITY}, - progress_bar::progress_bar, + term::progress_bar, terminal_link::TerminalFileLink, }; From 28d0b0a21ec2d916309733dcce8aecdbdf305d46 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Aug 2024 17:45:02 +0200 Subject: [PATCH 4/8] Highlight selected row --- src/list/state.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/list/state.rs b/src/list/state.rs index d8744352..4ba3d4eb 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use crossterm::{ cursor::{MoveTo, MoveToNextLine}, - style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor}, + style::{Attribute, Color, ResetColor, SetAttribute, SetBackgroundColor, SetForegroundColor}, terminal::{self, BeginSynchronizedUpdate, Clear, ClearType, EndSynchronizedUpdate}, QueueableCommand, }; @@ -115,6 +115,11 @@ impl<'a> ListState<'a> { let mut n_displayed_rows = 0; for (exercise_ind, exercise) in displayed_exercises { if self.selected == Some(n_displayed_rows) { + stdout.queue(SetBackgroundColor(Color::Rgb { + r: 50, + g: 50, + b: 50, + }))?; stdout.write_all("🦀".as_bytes())?; } else { stdout.write_all(b" ")?; @@ -135,7 +140,7 @@ impl<'a> ListState<'a> { stdout.write_all(b"PENDING ")?; } - stdout.queue(ResetColor)?; + stdout.queue(SetForegroundColor(Color::Reset))?; stdout.write_all(exercise.name.as_bytes())?; stdout.write_all(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?; @@ -143,6 +148,7 @@ impl<'a> ListState<'a> { stdout.write_all(exercise.path.as_bytes())?; next_ln::(stdout)?; + stdout.queue(ResetColor)?; n_displayed_rows += 1; } From b6129ad0811e05a256713614db899a98308cb62c Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Aug 2024 17:45:38 +0200 Subject: [PATCH 5/8] Use the full length for the wide footer --- src/list/state.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/list/state.rs b/src/list/state.rs index 4ba3d4eb..d4507416 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -93,7 +93,7 @@ impl<'a> ListState<'a> { stdout.write_all(b"Path")?; next_ln::(stdout)?; - let narrow = self.term_width < 96; + let narrow = self.term_width < 95; let show_footer = self.term_height > 6; let max_n_rows_to_display = (self.term_height - 1 - u16::from(show_footer) * (4 + u16::from(narrow))) as usize; @@ -203,7 +203,11 @@ impl<'a> ListState<'a> { Filter::None => stdout.write_all(b"one/

ending")?, } stdout.write_all(" │ uit list".as_bytes())?; - next_ln::(stdout)?; + if narrow { + next_ln::(stdout)?; + } else { + next_ln::(stdout)?; + } } else { stdout.queue(SetForegroundColor(Color::Magenta))?; stdout.write_all(self.message.as_bytes())?; From fd2a8c01cb35fcff4a5358cce9473ff91272c790 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 24 Aug 2024 19:18:13 +0200 Subject: [PATCH 6/8] Separate drawing rows --- src/list.rs | 2 +- src/list/state.rs | 90 ++++++++++++++++++++++++++++------------------- 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/list.rs b/src/list.rs index a571eeec..e3601825 100644 --- a/src/list.rs +++ b/src/list.rs @@ -84,7 +84,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> Event::FocusGained | Event::FocusLost => continue, } - list_state.redraw(stdout)?; + list_state.draw(stdout)?; } } diff --git a/src/list/state.rs b/src/list/state.rs index d4507416..cbca1d9e 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -10,7 +10,10 @@ use std::{ io::{self, StdoutLock, Write}, }; -use crate::{app_state::AppState, term::progress_bar, MAX_EXERCISE_NAME_LEN}; +use crate::{app_state::AppState, exercise::Exercise, term::progress_bar, MAX_EXERCISE_NAME_LEN}; + +// +1 for padding. +const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> { if CLEAR_LAST_CHAR { @@ -74,46 +77,24 @@ impl<'a> ListState<'a> { separator: "─".as_bytes().repeat(term_width as usize), }; - slf.redraw(stdout)?; + slf.draw(stdout)?; Ok(slf) } - pub fn redraw(&mut self, stdout: &mut StdoutLock) -> io::Result<()> { - if self.term_height == 0 { - return Ok(()); - } - - stdout.queue(BeginSynchronizedUpdate)?.queue(MoveTo(0, 0))?; - - // +1 for padding. - const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; - stdout.write_all(b" Current State Name")?; - stdout.write_all(&SPACE[..self.name_col_width - 2])?; - stdout.write_all(b"Path")?; - next_ln::(stdout)?; - - let narrow = self.term_width < 95; - let show_footer = self.term_height > 6; - let max_n_rows_to_display = - (self.term_height - 1 - u16::from(show_footer) * (4 + u16::from(narrow))) as usize; - - let displayed_exercises = self - .app_state - .exercises() - .iter() - .enumerate() - .filter(|(_, exercise)| match self.filter { - Filter::Done => exercise.done, - Filter::Pending => !exercise.done, - Filter::None => true, - }) - .skip(self.offset) - .take(max_n_rows_to_display); - + fn draw_rows( + &self, + stdout: &mut StdoutLock, + max_n_rows_to_display: usize, + filtered_exercises: impl Iterator, + ) -> io::Result { let current_exercise_ind = self.app_state.current_exercise_ind(); let mut n_displayed_rows = 0; - for (exercise_ind, exercise) in displayed_exercises { + + for (exercise_ind, exercise) in filtered_exercises + .skip(self.offset) + .take(max_n_rows_to_display) + { if self.selected == Some(n_displayed_rows) { stdout.queue(SetBackgroundColor(Color::Rgb { r: 50, @@ -152,6 +133,43 @@ impl<'a> ListState<'a> { n_displayed_rows += 1; } + Ok(n_displayed_rows) + } + + pub fn draw(&mut self, stdout: &mut StdoutLock) -> io::Result<()> { + if self.term_height == 0 { + return Ok(()); + } + + stdout.queue(BeginSynchronizedUpdate)?.queue(MoveTo(0, 0))?; + + // Header + stdout.write_all(b" Current State Name")?; + stdout.write_all(&SPACE[..self.name_col_width - 2])?; + stdout.write_all(b"Path")?; + next_ln::(stdout)?; + + let narrow = self.term_width < 95; + let show_footer = self.term_height > 6; + let max_n_rows_to_display = + (self.term_height - 1 - u16::from(show_footer) * (4 + u16::from(narrow))) as usize; + + // Rows + let iter = self.app_state.exercises().iter().enumerate(); + let n_displayed_rows = match self.filter { + Filter::Done => self.draw_rows( + stdout, + max_n_rows_to_display, + iter.filter(|(_, exercise)| exercise.done), + )?, + Filter::Pending => self.draw_rows( + stdout, + max_n_rows_to_display, + iter.filter(|(_, exercise)| !exercise.done), + )?, + Filter::None => self.draw_rows(stdout, max_n_rows_to_display, iter)?, + }; + for _ in 0..max_n_rows_to_display - n_displayed_rows { next_ln::(stdout)?; } @@ -172,7 +190,7 @@ impl<'a> ListState<'a> { next_ln::(stdout)?; if self.message.is_empty() { - // Help footer. + // Help footer stdout.write_all( "↓/j ↑/k home/g end/G │ ontinue at │ eset exercise │".as_bytes(), )?; From 5f4875e2bae07d3c8ce6505abbc67bbe447b7aa6 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sun, 25 Aug 2024 19:24:12 +0200 Subject: [PATCH 7/8] Almost done with list --- src/list.rs | 8 +- src/list/state.rs | 230 +++++++++++++++++++++++++++------------------- 2 files changed, 135 insertions(+), 103 deletions(-) diff --git a/src/list.rs b/src/list.rs index e3601825..a8e52254 100644 --- a/src/list.rs +++ b/src/list.rs @@ -59,9 +59,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> list_state.message.push_str(message); } - KeyCode::Char('r') => { - list_state.reset_selected()?; - } + KeyCode::Char('r') => list_state.reset_selected()?, KeyCode::Char('c') => { if list_state.selected_to_current_exercise()? { return Ok(()); @@ -77,9 +75,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> MouseEventKind::ScrollUp => list_state.select_previous(), _ => continue, }, - Event::Resize(width, height) => { - list_state.set_term_size(width, height); - } + Event::Resize(width, height) => list_state.set_term_size(width, height), // Ignore Event::FocusGained | Event::FocusLost => continue, } diff --git a/src/list/state.rs b/src/list/state.rs index cbca1d9e..b8fdfcbc 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -15,20 +15,21 @@ use crate::{app_state::AppState, exercise::Exercise, term::progress_bar, MAX_EXE // +1 for padding. const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; -fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> { - if CLEAR_LAST_CHAR { - // Avoids having the last written char as the last displayed one when - // the written width is higher than the terminal width. - // Happens on the Gnome terminal for example. - stdout.write_all(b" ")?; - } - +fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> { stdout .queue(Clear(ClearType::UntilNewLine))? .queue(MoveToNextLine(1))?; Ok(()) } +// Avoids having the last written char as the last displayed one when the +// written width is higher than the terminal width. +// Happens on the Gnome terminal for example. +fn next_ln_overwrite(stdout: &mut StdoutLock) -> io::Result<()> { + stdout.write_all(b" ")?; + next_ln(stdout) +} + #[derive(Copy, Clone, PartialEq, Eq)] pub enum Filter { Done, @@ -37,65 +38,111 @@ pub enum Filter { } pub struct ListState<'a> { + /// Footer message to be displayed if not empty. pub message: String, - filter: Filter, app_state: &'a mut AppState, - n_rows_with_filter: usize, name_col_width: usize, - offset: usize, - selected: Option, + filter: Filter, + n_rows_with_filter: usize, + /// Selected row out of the displayed ones. + selected_row: Option, term_width: u16, term_height: u16, - separator: Vec, + separator_line: Vec, + narrow_term: bool, + show_footer: bool, + max_n_rows_to_display: usize, + scroll_padding: usize, + row_offset: usize, } impl<'a> ListState<'a> { pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result { - let (term_width, term_height) = terminal::size()?; stdout.queue(Clear(ClearType::All))?; + let name_col_title_len = 4; let name_col_width = app_state .exercises() .iter() .map(|exercise| exercise.name.len()) .max() - .map_or(4, |max| max.max(4)); + .map_or(name_col_title_len, |max| max.max(name_col_title_len)); + let filter = Filter::None; let n_rows_with_filter = app_state.exercises().len(); - let selected = app_state.current_exercise_ind(); + let selected = Some(app_state.current_exercise_ind()); let mut slf = Self { message: String::with_capacity(128), - filter: Filter::None, app_state, - n_rows_with_filter, name_col_width, - offset: selected.saturating_sub(10), - selected: Some(selected), - term_width, - term_height, - separator: "─".as_bytes().repeat(term_width as usize), + filter, + n_rows_with_filter, + selected_row: selected, + // Set by `set_term_size` + term_width: 0, + term_height: 0, + separator_line: Vec::new(), + narrow_term: false, + show_footer: true, + max_n_rows_to_display: 0, + scroll_padding: 0, + // Updated by `draw` + row_offset: 0, }; + let (width, height) = terminal::size()?; + slf.set_term_size(width, height); slf.draw(stdout)?; Ok(slf) } + pub fn set_term_size(&mut self, width: u16, height: u16) { + self.term_width = width; + self.term_height = height; + + self.separator_line = "─".as_bytes().repeat(width as usize); + + self.narrow_term = width < 95 && self.selected_row.is_some(); + self.show_footer = height > 6; + self.max_n_rows_to_display = + (height - 1 - u16::from(self.show_footer) * (4 + u16::from(self.narrow_term))) as usize; + self.scroll_padding = (self.max_n_rows_to_display / 4).min(5); + } + + fn update_offset(&mut self) { + let Some(selected) = self.selected_row else { + return; + }; + + let min_offset = (selected + self.scroll_padding) + .saturating_sub(self.max_n_rows_to_display.saturating_sub(1)); + let max_offset = selected.saturating_sub(self.scroll_padding); + let global_max_offset = self + .n_rows_with_filter + .saturating_sub(self.max_n_rows_to_display); + + self.row_offset = self + .row_offset + .max(min_offset) + .min(max_offset) + .min(global_max_offset); + } + fn draw_rows( &self, stdout: &mut StdoutLock, - max_n_rows_to_display: usize, filtered_exercises: impl Iterator, ) -> io::Result { let current_exercise_ind = self.app_state.current_exercise_ind(); let mut n_displayed_rows = 0; for (exercise_ind, exercise) in filtered_exercises - .skip(self.offset) - .take(max_n_rows_to_display) + .skip(self.row_offset) + .take(self.max_n_rows_to_display) { - if self.selected == Some(n_displayed_rows) { + if self.selected_row == Some(self.row_offset + n_displayed_rows) { stdout.queue(SetBackgroundColor(Color::Rgb { r: 50, g: 50, @@ -128,7 +175,7 @@ impl<'a> ListState<'a> { stdout.write_all(exercise.path.as_bytes())?; - next_ln::(stdout)?; + next_ln_overwrite(stdout)?; stdout.queue(ResetColor)?; n_displayed_rows += 1; } @@ -147,36 +194,27 @@ impl<'a> ListState<'a> { stdout.write_all(b" Current State Name")?; stdout.write_all(&SPACE[..self.name_col_width - 2])?; stdout.write_all(b"Path")?; - next_ln::(stdout)?; + next_ln_overwrite(stdout)?; - let narrow = self.term_width < 95; - let show_footer = self.term_height > 6; - let max_n_rows_to_display = - (self.term_height - 1 - u16::from(show_footer) * (4 + u16::from(narrow))) as usize; + self.update_offset(); // Rows let iter = self.app_state.exercises().iter().enumerate(); let n_displayed_rows = match self.filter { - Filter::Done => self.draw_rows( - stdout, - max_n_rows_to_display, - iter.filter(|(_, exercise)| exercise.done), - )?, - Filter::Pending => self.draw_rows( - stdout, - max_n_rows_to_display, - iter.filter(|(_, exercise)| !exercise.done), - )?, - Filter::None => self.draw_rows(stdout, max_n_rows_to_display, iter)?, + Filter::Done => self.draw_rows(stdout, iter.filter(|(_, exercise)| exercise.done))?, + Filter::Pending => { + self.draw_rows(stdout, iter.filter(|(_, exercise)| !exercise.done))? + } + Filter::None => self.draw_rows(stdout, iter)?, }; - for _ in 0..max_n_rows_to_display - n_displayed_rows { - next_ln::(stdout)?; + for _ in 0..self.max_n_rows_to_display - n_displayed_rows { + next_ln(stdout)?; } - if show_footer { - stdout.write_all(&self.separator)?; - next_ln::(stdout)?; + if self.show_footer { + stdout.write_all(&self.separator_line)?; + next_ln(stdout)?; progress_bar( stdout, @@ -184,21 +222,25 @@ impl<'a> ListState<'a> { self.app_state.exercises().len() as u16, self.term_width, )?; - next_ln::(stdout)?; + next_ln(stdout)?; - stdout.write_all(&self.separator)?; - next_ln::(stdout)?; + stdout.write_all(&self.separator_line)?; + next_ln(stdout)?; if self.message.is_empty() { // Help footer - stdout.write_all( - "↓/j ↑/k home/g end/G │ ontinue at │ eset exercise │".as_bytes(), - )?; - if narrow { - next_ln::(stdout)?; - stdout.write_all(b"filter ")?; + if self.selected_row.is_some() { + stdout.write_all( + "↓/j ↑/k home/g end/G | ontinue at | eset exercise".as_bytes(), + )?; + if self.narrow_term { + next_ln_overwrite(stdout)?; + stdout.write_all(b"filter ")?; + } else { + stdout.write_all(b" | filter ")?; + } } else { - stdout.write_all(b" filter ")?; + stdout.write_all(b"filter ")?; } match self.filter { @@ -220,19 +262,19 @@ impl<'a> ListState<'a> { } Filter::None => stdout.write_all(b"one/

ending")?, } - stdout.write_all(" │ uit list".as_bytes())?; - if narrow { - next_ln::(stdout)?; + stdout.write_all(b" | uit list")?; + if self.narrow_term { + next_ln_overwrite(stdout)?; } else { - next_ln::(stdout)?; + next_ln(stdout)?; } } else { stdout.queue(SetForegroundColor(Color::Magenta))?; stdout.write_all(self.message.as_bytes())?; stdout.queue(ResetColor)?; - next_ln::(stdout)?; - if narrow { - next_ln::(stdout)?; + next_ln_overwrite(stdout)?; + if self.narrow_term { + next_ln(stdout)?; } } } @@ -240,20 +282,8 @@ impl<'a> ListState<'a> { stdout.queue(EndSynchronizedUpdate)?.flush() } - pub fn set_term_size(&mut self, width: u16, height: u16) { - self.term_width = width; - self.term_height = height; - self.separator = "─".as_bytes().repeat(width as usize); - } - - #[inline] - pub fn filter(&self) -> Filter { - self.filter - } - - pub fn set_filter(&mut self, filter: Filter) { - self.filter = filter; - self.n_rows_with_filter = match filter { + fn update_rows(&mut self) { + self.n_rows_with_filter = match self.filter { Filter::Done => self .app_state .exercises() @@ -270,42 +300,46 @@ impl<'a> ListState<'a> { }; if self.n_rows_with_filter == 0 { - self.selected = None; + self.selected_row = None; } else { - self.selected = Some( - self.selected + self.selected_row = Some( + self.selected_row .map_or(0, |selected| selected.min(self.n_rows_with_filter - 1)), ); } } + #[inline] + pub fn filter(&self) -> Filter { + self.filter + } + + pub fn set_filter(&mut self, filter: Filter) { + self.filter = filter; + self.update_rows(); + } + pub fn select_next(&mut self) { - if self.n_rows_with_filter > 0 { - let next = self.selected.map_or(0, |selected| { - (selected + 1).min(self.n_rows_with_filter - 1) - }); - self.selected = Some(next); + if let Some(selected) = self.selected_row { + self.selected_row = Some((selected + 1).min(self.n_rows_with_filter - 1)); } } pub fn select_previous(&mut self) { - if self.n_rows_with_filter > 0 { - let previous = self - .selected - .map_or(0, |selected| selected.saturating_sub(1)); - self.selected = Some(previous); + if let Some(selected) = self.selected_row { + self.selected_row = Some(selected.saturating_sub(1)); } } pub fn select_first(&mut self) { if self.n_rows_with_filter > 0 { - self.selected = Some(0); + self.selected_row = Some(0); } } pub fn select_last(&mut self) { if self.n_rows_with_filter > 0 { - self.selected = Some(self.n_rows_with_filter - 1); + self.selected_row = Some(self.n_rows_with_filter - 1); } } @@ -334,13 +368,14 @@ impl<'a> ListState<'a> { } pub fn reset_selected(&mut self) -> Result<()> { - let Some(selected) = self.selected else { + let Some(selected) = self.selected_row else { self.message.push_str("Nothing selected to reset!"); return Ok(()); }; let exercise_ind = self.selected_to_exercise_ind(selected)?; let exercise_path = self.app_state.reset_exercise_by_ind(exercise_ind)?; + self.update_rows(); write!(self.message, "The exercise {exercise_path} has been reset")?; Ok(()) @@ -348,13 +383,14 @@ impl<'a> ListState<'a> { // Return `true` if there was something to select. pub fn selected_to_current_exercise(&mut self) -> Result { - let Some(selected) = self.selected else { + let Some(selected) = self.selected_row else { self.message.push_str("Nothing selected to continue at!"); return Ok(false); }; let exercise_ind = self.selected_to_exercise_ind(selected)?; self.app_state.set_current_exercise_ind(exercise_ind)?; + Ok(true) } } From 64772544fad6788fd3fce5db3f357dba6f2d8d23 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sun, 25 Aug 2024 20:29:54 +0200 Subject: [PATCH 8/8] Final touches :D --- src/app_state.rs | 3 +- src/list/state.rs | 95 ++++++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/app_state.rs b/src/app_state.rs index b72469c4..8fd8f3be 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -271,6 +271,7 @@ impl AppState { Ok(exercise.path) } + // Reset the exercise by index and return its name. pub fn reset_exercise_by_ind(&mut self, exercise_ind: usize) -> Result<&'static str> { if exercise_ind >= self.exercises.len() { bail!(BAD_INDEX_ERR); @@ -280,7 +281,7 @@ impl AppState { let exercise = &self.exercises[exercise_ind]; self.reset(exercise_ind, exercise.path)?; - Ok(exercise.path) + Ok(exercise.name) } // Return the index of the next pending exercise or `None` if all exercises are done. diff --git a/src/list/state.rs b/src/list/state.rs index b8fdfcbc..25ca1ded 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -12,7 +12,8 @@ use std::{ use crate::{app_state::AppState, exercise::Exercise, term::progress_bar, MAX_EXERCISE_NAME_LEN}; -// +1 for padding. +const MAX_SCROLL_PADDING: usize = 8; +// +1 for column padding. const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1]; fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> { @@ -44,8 +45,9 @@ pub struct ListState<'a> { name_col_width: usize, filter: Filter, n_rows_with_filter: usize, - /// Selected row out of the displayed ones. + /// Selected row out of the filtered ones. selected_row: Option, + row_offset: usize, term_width: u16, term_height: u16, separator_line: Vec, @@ -53,7 +55,6 @@ pub struct ListState<'a> { show_footer: bool, max_n_rows_to_display: usize, scroll_padding: usize, - row_offset: usize, } impl<'a> ListState<'a> { @@ -70,7 +71,7 @@ impl<'a> ListState<'a> { let filter = Filter::None; let n_rows_with_filter = app_state.exercises().len(); - let selected = Some(app_state.current_exercise_ind()); + let selected = app_state.current_exercise_ind(); let mut slf = Self { message: String::with_capacity(128), @@ -78,7 +79,8 @@ impl<'a> ListState<'a> { name_col_width, filter, n_rows_with_filter, - selected_row: selected, + selected_row: Some(selected), + row_offset: selected.saturating_sub(MAX_SCROLL_PADDING), // Set by `set_term_size` term_width: 0, term_height: 0, @@ -87,8 +89,6 @@ impl<'a> ListState<'a> { show_footer: true, max_n_rows_to_display: 0, scroll_padding: 0, - // Updated by `draw` - row_offset: 0, }; let (width, height) = terminal::size()?; @@ -98,19 +98,6 @@ impl<'a> ListState<'a> { Ok(slf) } - pub fn set_term_size(&mut self, width: u16, height: u16) { - self.term_width = width; - self.term_height = height; - - self.separator_line = "─".as_bytes().repeat(width as usize); - - self.narrow_term = width < 95 && self.selected_row.is_some(); - self.show_footer = height > 6; - self.max_n_rows_to_display = - (height - 1 - u16::from(self.show_footer) * (4 + u16::from(self.narrow_term))) as usize; - self.scroll_padding = (self.max_n_rows_to_display / 4).min(5); - } - fn update_offset(&mut self) { let Some(selected) = self.selected_row else { return; @@ -130,6 +117,36 @@ impl<'a> ListState<'a> { .min(global_max_offset); } + pub fn set_term_size(&mut self, width: u16, height: u16) { + self.term_width = width; + self.term_height = height; + + if height == 0 { + return; + } + + let wide_help_footer_width = 95; + // The help footer is shorter when nothing is selected. + self.narrow_term = width < wide_help_footer_width && self.selected_row.is_some(); + + let header_height = 1; + // 2 separator, 1 progress bar, 1-2 footer message. + let footer_height = 4 + u16::from(self.narrow_term); + self.show_footer = height > header_height + footer_height; + + if self.show_footer { + self.separator_line = "─".as_bytes().repeat(width as usize); + } + + self.max_n_rows_to_display = height + .saturating_sub(header_height + u16::from(self.show_footer) * footer_height) + as usize; + + self.scroll_padding = (self.max_n_rows_to_display / 4).min(MAX_SCROLL_PADDING); + + self.update_offset(); + } + fn draw_rows( &self, stdout: &mut StdoutLock, @@ -196,8 +213,6 @@ impl<'a> ListState<'a> { stdout.write_all(b"Path")?; next_ln_overwrite(stdout)?; - self.update_offset(); - // Rows let iter = self.app_state.exercises().iter().enumerate(); let n_displayed_rows = match self.filter { @@ -228,7 +243,7 @@ impl<'a> ListState<'a> { next_ln(stdout)?; if self.message.is_empty() { - // Help footer + // Help footer message if self.selected_row.is_some() { stdout.write_all( "↓/j ↑/k home/g end/G | ontinue at | eset exercise".as_bytes(), @@ -240,6 +255,7 @@ impl<'a> ListState<'a> { stdout.write_all(b" | filter ")?; } } else { + // Nothing selected (and nothing shown), so only display filter and quit. stdout.write_all(b"filter ")?; } @@ -262,7 +278,9 @@ impl<'a> ListState<'a> { } Filter::None => stdout.write_all(b"one/

ending")?, } + stdout.write_all(b" | uit list")?; + if self.narrow_term { next_ln_overwrite(stdout)?; } else { @@ -282,6 +300,11 @@ impl<'a> ListState<'a> { stdout.queue(EndSynchronizedUpdate)?.flush() } + fn set_selected(&mut self, selected: usize) { + self.selected_row = Some(selected); + self.update_offset(); + } + fn update_rows(&mut self) { self.n_rows_with_filter = match self.filter { Filter::Done => self @@ -301,12 +324,13 @@ impl<'a> ListState<'a> { if self.n_rows_with_filter == 0 { self.selected_row = None; - } else { - self.selected_row = Some( - self.selected_row - .map_or(0, |selected| selected.min(self.n_rows_with_filter - 1)), - ); + return; } + + self.set_selected( + self.selected_row + .map_or(0, |selected| selected.min(self.n_rows_with_filter - 1)), + ); } #[inline] @@ -321,25 +345,25 @@ impl<'a> ListState<'a> { pub fn select_next(&mut self) { if let Some(selected) = self.selected_row { - self.selected_row = Some((selected + 1).min(self.n_rows_with_filter - 1)); + self.set_selected((selected + 1).min(self.n_rows_with_filter - 1)); } } pub fn select_previous(&mut self) { if let Some(selected) = self.selected_row { - self.selected_row = Some(selected.saturating_sub(1)); + self.set_selected(selected.saturating_sub(1)); } } pub fn select_first(&mut self) { if self.n_rows_with_filter > 0 { - self.selected_row = Some(0); + self.set_selected(0); } } pub fn select_last(&mut self) { if self.n_rows_with_filter > 0 { - self.selected_row = Some(self.n_rows_with_filter - 1); + self.set_selected(self.n_rows_with_filter - 1); } } @@ -374,9 +398,12 @@ impl<'a> ListState<'a> { }; let exercise_ind = self.selected_to_exercise_ind(selected)?; - let exercise_path = self.app_state.reset_exercise_by_ind(exercise_ind)?; + let exercise_name = self.app_state.reset_exercise_by_ind(exercise_ind)?; self.update_rows(); - write!(self.message, "The exercise {exercise_path} has been reset")?; + write!( + self.message, + "The exercise `{exercise_name}` has been reset", + )?; Ok(()) }