rustlings/src/list/state.rs

277 lines
8.3 KiB
Rust
Raw Normal View History

use anyhow::{Context, Result};
2024-08-24 01:14:12 +03:00
use crossterm::{
cursor::{MoveDown, MoveTo},
style::{Color, ResetColor, SetForegroundColor},
terminal::{self, BeginSynchronizedUpdate, EndSynchronizedUpdate},
QueueableCommand,
2024-04-07 20:05:29 +03:00
};
2024-08-24 01:14:12 +03:00
use std::{
fmt::Write as _,
io::{self, StdoutLock, Write as _},
};
use crate::{app_state::AppState, term::clear_terminal, MAX_EXERCISE_NAME_LEN};
2024-04-07 20:05:29 +03:00
2024-08-24 01:14:12 +03:00
// +1 for padding.
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
2024-04-07 20:05:29 +03:00
2024-04-08 03:41:48 +03:00
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Filter {
Done,
Pending,
None,
}
2024-08-24 01:14:12 +03:00
pub struct ListState<'a> {
2024-04-08 02:33:11 +03:00
pub message: String,
2024-08-24 01:14:12 +03:00
filter: Filter,
app_state: &'a mut AppState,
2024-08-24 01:14:12 +03:00
n_rows_with_filter: usize,
name_col_width: usize,
offset: usize,
selected: Option<usize>,
2024-04-07 20:05:29 +03:00
}
2024-08-24 01:14:12 +03:00
impl<'a> ListState<'a> {
pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result<Self> {
let name_col_width = app_state
.exercises()
2024-04-07 20:05:29 +03:00
.iter()
.map(|exercise| exercise.name.len())
.max()
2024-08-24 01:14:12 +03:00
.map_or(4, |max| max.max(4));
2024-04-07 20:05:29 +03:00
2024-08-24 01:14:12 +03:00
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")?;
2024-04-07 20:05:29 +03:00
2024-08-24 01:14:12 +03:00
let selected = app_state.current_exercise_ind();
let n_rows_with_filter = app_state.exercises().len();
2024-04-11 15:35:30 +03:00
2024-08-24 01:14:12 +03:00
let mut slf = Self {
2024-04-08 03:41:48 +03:00
message: String::with_capacity(128),
2024-08-24 01:14:12 +03:00
filter: Filter::None,
app_state,
2024-08-24 01:14:12 +03:00
n_rows_with_filter,
name_col_width,
offset: selected.saturating_sub(10),
selected: Some(selected),
};
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(),
};
2024-08-24 01:14:12 +03:00
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)),
);
}
2024-04-07 20:05:29 +03:00
}
pub fn select_next(&mut self) {
2024-08-24 01:14:12 +03:00
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);
2024-04-11 15:35:30 +03:00
}
2024-04-07 20:05:29 +03:00
}
pub fn select_previous(&mut self) {
2024-08-24 01:14:12 +03:00
if self.n_rows_with_filter > 0 {
2024-04-11 15:35:30 +03:00
let previous = self
2024-08-24 01:14:12 +03:00
.selected
2024-04-11 15:35:30 +03:00
.map_or(0, |selected| selected.saturating_sub(1));
2024-08-24 01:14:12 +03:00
self.selected = Some(previous);
2024-04-11 15:35:30 +03:00
}
2024-04-07 20:05:29 +03:00
}
pub fn select_first(&mut self) {
2024-08-24 01:14:12 +03:00
if self.n_rows_with_filter > 0 {
self.selected = Some(0);
2024-04-11 15:35:30 +03:00
}
2024-04-07 20:05:29 +03:00
}
pub fn select_last(&mut self) {
2024-08-24 01:14:12 +03:00
if self.n_rows_with_filter > 0 {
self.selected = Some(self.n_rows_with_filter - 1);
2024-04-11 15:35:30 +03:00
}
2024-04-07 20:05:29 +03:00
}
2024-08-24 01:14:12 +03:00
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;
2024-08-17 17:54:44 +03:00
let narrow_u16 = u16::from(narrow);
2024-08-24 01:14:12 +03:00
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, "🦀")?;
2024-08-17 17:54:44 +03:00
} else {
2024-08-24 01:14:12 +03:00
stdout.write_all(b" ")?;
2024-08-17 17:54:44 +03:00
}
2024-08-24 01:14:12 +03:00
if ind == current_exercise_ind {
stdout.queue(SetForegroundColor(Color::Red))?;
stdout.write_all(b">>>>>>> ")?;
} else {
stdout.write_all(b" ")?;
2024-04-29 00:21:13 +03:00
}
2024-08-17 17:54:44 +03:00
2024-08-24 01:14:12 +03:00
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 │ <c>ontinue at │ <r>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("<d>one".underlined().magenta());
// spans.push(Span::raw("/<p>ending"));
// }
// Filter::Pending => {
// spans.push(Span::raw("<d>one/"));
// spans.push("<p>ending".underlined().magenta());
// }
// Filter::None => spans.push(Span::raw("<d>one/<p>ending")),
// }
// spans.push(Span::raw(" │ <q>uit list"));
// text.push_line(spans);
// text
// } else {
// Text::from(self.message.as_str().light_blue())
// };
stdout.queue(EndSynchronizedUpdate)?;
stdout.flush()?;
2024-04-09 20:37:39 +03:00
Ok(())
2024-04-07 20:05:29 +03:00
}
2024-08-24 01:14:12 +03:00
pub fn reset_selected(&mut self) -> Result<()> {
let Some(selected) = self.selected else {
return Ok(());
2024-04-11 15:35:30 +03:00
};
2024-04-18 13:41:17 +03:00
let ind = self
.app_state
.exercises()
.iter()
.enumerate()
2024-04-14 02:15:43 +03:00
.filter_map(|(ind, exercise)| match self.filter {
2024-04-18 13:41:17 +03:00
Filter::Done => exercise.done.then_some(ind),
Filter::Pending => (!exercise.done).then_some(ind),
Filter::None => Some(ind),
})
.nth(selected)
.context("Invalid selection index")?;
2024-04-18 13:41:17 +03:00
let exercise_path = self.app_state.reset_exercise_by_ind(ind)?;
2024-04-25 03:03:26 +03:00
write!(self.message, "The exercise {exercise_path} has been reset")?;
2024-08-24 01:14:12 +03:00
Ok(())
}
// Return `true` if there was something to select.
pub fn selected_to_current_exercise(&mut self) -> Result<bool> {
2024-08-24 01:14:12 +03:00
let Some(selected) = self.selected else {
self.message.push_str("Nothing selected to continue at!");
return Ok(false);
2024-04-11 15:35:30 +03:00
};
let (ind, _) = self
.app_state
2024-04-14 02:15:43 +03:00
.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)?;
Ok(true)
}
2024-04-07 20:05:29 +03:00
}