mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-18 06:58:10 -05:00
Almost done with list
This commit is contained in:
parent
fd2a8c01cb
commit
5f4875e2ba
2 changed files with 135 additions and 103 deletions
|
@ -59,9 +59,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
||||||
|
|
||||||
list_state.message.push_str(message);
|
list_state.message.push_str(message);
|
||||||
}
|
}
|
||||||
KeyCode::Char('r') => {
|
KeyCode::Char('r') => list_state.reset_selected()?,
|
||||||
list_state.reset_selected()?;
|
|
||||||
}
|
|
||||||
KeyCode::Char('c') => {
|
KeyCode::Char('c') => {
|
||||||
if list_state.selected_to_current_exercise()? {
|
if list_state.selected_to_current_exercise()? {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -77,9 +75,7 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()>
|
||||||
MouseEventKind::ScrollUp => list_state.select_previous(),
|
MouseEventKind::ScrollUp => list_state.select_previous(),
|
||||||
_ => continue,
|
_ => continue,
|
||||||
},
|
},
|
||||||
Event::Resize(width, height) => {
|
Event::Resize(width, height) => list_state.set_term_size(width, height),
|
||||||
list_state.set_term_size(width, height);
|
|
||||||
}
|
|
||||||
// Ignore
|
// Ignore
|
||||||
Event::FocusGained | Event::FocusLost => continue,
|
Event::FocusGained | Event::FocusLost => continue,
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,20 +15,21 @@ use crate::{app_state::AppState, exercise::Exercise, term::progress_bar, MAX_EXE
|
||||||
// +1 for padding.
|
// +1 for padding.
|
||||||
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
|
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
|
||||||
|
|
||||||
fn next_ln<const CLEAR_LAST_CHAR: bool>(stdout: &mut StdoutLock) -> io::Result<()> {
|
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
|
stdout
|
||||||
.queue(Clear(ClearType::UntilNewLine))?
|
.queue(Clear(ClearType::UntilNewLine))?
|
||||||
.queue(MoveToNextLine(1))?;
|
.queue(MoveToNextLine(1))?;
|
||||||
Ok(())
|
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)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub enum Filter {
|
pub enum Filter {
|
||||||
Done,
|
Done,
|
||||||
|
@ -37,65 +38,111 @@ pub enum Filter {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ListState<'a> {
|
pub struct ListState<'a> {
|
||||||
|
/// Footer message to be displayed if not empty.
|
||||||
pub message: String,
|
pub message: String,
|
||||||
filter: Filter,
|
|
||||||
app_state: &'a mut AppState,
|
app_state: &'a mut AppState,
|
||||||
n_rows_with_filter: usize,
|
|
||||||
name_col_width: usize,
|
name_col_width: usize,
|
||||||
offset: usize,
|
filter: Filter,
|
||||||
selected: Option<usize>,
|
n_rows_with_filter: usize,
|
||||||
|
/// Selected row out of the displayed ones.
|
||||||
|
selected_row: Option<usize>,
|
||||||
term_width: u16,
|
term_width: u16,
|
||||||
term_height: u16,
|
term_height: u16,
|
||||||
separator: Vec<u8>,
|
separator_line: Vec<u8>,
|
||||||
|
narrow_term: bool,
|
||||||
|
show_footer: bool,
|
||||||
|
max_n_rows_to_display: usize,
|
||||||
|
scroll_padding: usize,
|
||||||
|
row_offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ListState<'a> {
|
impl<'a> ListState<'a> {
|
||||||
pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result<Self> {
|
pub fn new(app_state: &'a mut AppState, stdout: &mut StdoutLock) -> io::Result<Self> {
|
||||||
let (term_width, term_height) = terminal::size()?;
|
|
||||||
stdout.queue(Clear(ClearType::All))?;
|
stdout.queue(Clear(ClearType::All))?;
|
||||||
|
|
||||||
|
let name_col_title_len = 4;
|
||||||
let name_col_width = app_state
|
let name_col_width = app_state
|
||||||
.exercises()
|
.exercises()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|exercise| exercise.name.len())
|
.map(|exercise| exercise.name.len())
|
||||||
.max()
|
.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 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 {
|
let mut slf = Self {
|
||||||
message: String::with_capacity(128),
|
message: String::with_capacity(128),
|
||||||
filter: Filter::None,
|
|
||||||
app_state,
|
app_state,
|
||||||
n_rows_with_filter,
|
|
||||||
name_col_width,
|
name_col_width,
|
||||||
offset: selected.saturating_sub(10),
|
filter,
|
||||||
selected: Some(selected),
|
n_rows_with_filter,
|
||||||
term_width,
|
selected_row: selected,
|
||||||
term_height,
|
// Set by `set_term_size`
|
||||||
separator: "─".as_bytes().repeat(term_width as usize),
|
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)?;
|
slf.draw(stdout)?;
|
||||||
|
|
||||||
Ok(slf)
|
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(
|
fn draw_rows(
|
||||||
&self,
|
&self,
|
||||||
stdout: &mut StdoutLock,
|
stdout: &mut StdoutLock,
|
||||||
max_n_rows_to_display: usize,
|
|
||||||
filtered_exercises: impl Iterator<Item = (usize, &'a Exercise)>,
|
filtered_exercises: impl Iterator<Item = (usize, &'a Exercise)>,
|
||||||
) -> io::Result<usize> {
|
) -> io::Result<usize> {
|
||||||
let current_exercise_ind = self.app_state.current_exercise_ind();
|
let current_exercise_ind = self.app_state.current_exercise_ind();
|
||||||
let mut n_displayed_rows = 0;
|
let mut n_displayed_rows = 0;
|
||||||
|
|
||||||
for (exercise_ind, exercise) in filtered_exercises
|
for (exercise_ind, exercise) in filtered_exercises
|
||||||
.skip(self.offset)
|
.skip(self.row_offset)
|
||||||
.take(max_n_rows_to_display)
|
.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 {
|
stdout.queue(SetBackgroundColor(Color::Rgb {
|
||||||
r: 50,
|
r: 50,
|
||||||
g: 50,
|
g: 50,
|
||||||
|
@ -128,7 +175,7 @@ impl<'a> ListState<'a> {
|
||||||
|
|
||||||
stdout.write_all(exercise.path.as_bytes())?;
|
stdout.write_all(exercise.path.as_bytes())?;
|
||||||
|
|
||||||
next_ln::<true>(stdout)?;
|
next_ln_overwrite(stdout)?;
|
||||||
stdout.queue(ResetColor)?;
|
stdout.queue(ResetColor)?;
|
||||||
n_displayed_rows += 1;
|
n_displayed_rows += 1;
|
||||||
}
|
}
|
||||||
|
@ -147,36 +194,27 @@ impl<'a> ListState<'a> {
|
||||||
stdout.write_all(b" Current State Name")?;
|
stdout.write_all(b" Current State Name")?;
|
||||||
stdout.write_all(&SPACE[..self.name_col_width - 2])?;
|
stdout.write_all(&SPACE[..self.name_col_width - 2])?;
|
||||||
stdout.write_all(b"Path")?;
|
stdout.write_all(b"Path")?;
|
||||||
next_ln::<true>(stdout)?;
|
next_ln_overwrite(stdout)?;
|
||||||
|
|
||||||
let narrow = self.term_width < 95;
|
self.update_offset();
|
||||||
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
|
// Rows
|
||||||
let iter = self.app_state.exercises().iter().enumerate();
|
let iter = self.app_state.exercises().iter().enumerate();
|
||||||
let n_displayed_rows = match self.filter {
|
let n_displayed_rows = match self.filter {
|
||||||
Filter::Done => self.draw_rows(
|
Filter::Done => self.draw_rows(stdout, iter.filter(|(_, exercise)| exercise.done))?,
|
||||||
stdout,
|
Filter::Pending => {
|
||||||
max_n_rows_to_display,
|
self.draw_rows(stdout, iter.filter(|(_, exercise)| !exercise.done))?
|
||||||
iter.filter(|(_, exercise)| exercise.done),
|
}
|
||||||
)?,
|
Filter::None => self.draw_rows(stdout, iter)?,
|
||||||
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 {
|
for _ in 0..self.max_n_rows_to_display - n_displayed_rows {
|
||||||
next_ln::<false>(stdout)?;
|
next_ln(stdout)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if show_footer {
|
if self.show_footer {
|
||||||
stdout.write_all(&self.separator)?;
|
stdout.write_all(&self.separator_line)?;
|
||||||
next_ln::<false>(stdout)?;
|
next_ln(stdout)?;
|
||||||
|
|
||||||
progress_bar(
|
progress_bar(
|
||||||
stdout,
|
stdout,
|
||||||
|
@ -184,19 +222,23 @@ impl<'a> ListState<'a> {
|
||||||
self.app_state.exercises().len() as u16,
|
self.app_state.exercises().len() as u16,
|
||||||
self.term_width,
|
self.term_width,
|
||||||
)?;
|
)?;
|
||||||
next_ln::<false>(stdout)?;
|
next_ln(stdout)?;
|
||||||
|
|
||||||
stdout.write_all(&self.separator)?;
|
stdout.write_all(&self.separator_line)?;
|
||||||
next_ln::<false>(stdout)?;
|
next_ln(stdout)?;
|
||||||
|
|
||||||
if self.message.is_empty() {
|
if self.message.is_empty() {
|
||||||
// Help footer
|
// Help footer
|
||||||
|
if self.selected_row.is_some() {
|
||||||
stdout.write_all(
|
stdout.write_all(
|
||||||
"↓/j ↑/k home/g end/G │ <c>ontinue at │ <r>eset exercise │".as_bytes(),
|
"↓/j ↑/k home/g end/G | <c>ontinue at | <r>eset exercise".as_bytes(),
|
||||||
)?;
|
)?;
|
||||||
if narrow {
|
if self.narrow_term {
|
||||||
next_ln::<true>(stdout)?;
|
next_ln_overwrite(stdout)?;
|
||||||
stdout.write_all(b"filter ")?;
|
stdout.write_all(b"filter ")?;
|
||||||
|
} else {
|
||||||
|
stdout.write_all(b" | filter ")?;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stdout.write_all(b"filter ")?;
|
stdout.write_all(b"filter ")?;
|
||||||
}
|
}
|
||||||
|
@ -220,19 +262,19 @@ impl<'a> ListState<'a> {
|
||||||
}
|
}
|
||||||
Filter::None => stdout.write_all(b"<d>one/<p>ending")?,
|
Filter::None => stdout.write_all(b"<d>one/<p>ending")?,
|
||||||
}
|
}
|
||||||
stdout.write_all(" │ <q>uit list".as_bytes())?;
|
stdout.write_all(b" | <q>uit list")?;
|
||||||
if narrow {
|
if self.narrow_term {
|
||||||
next_ln::<true>(stdout)?;
|
next_ln_overwrite(stdout)?;
|
||||||
} else {
|
} else {
|
||||||
next_ln::<false>(stdout)?;
|
next_ln(stdout)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
stdout.queue(SetForegroundColor(Color::Magenta))?;
|
stdout.queue(SetForegroundColor(Color::Magenta))?;
|
||||||
stdout.write_all(self.message.as_bytes())?;
|
stdout.write_all(self.message.as_bytes())?;
|
||||||
stdout.queue(ResetColor)?;
|
stdout.queue(ResetColor)?;
|
||||||
next_ln::<true>(stdout)?;
|
next_ln_overwrite(stdout)?;
|
||||||
if narrow {
|
if self.narrow_term {
|
||||||
next_ln::<false>(stdout)?;
|
next_ln(stdout)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -240,20 +282,8 @@ impl<'a> ListState<'a> {
|
||||||
stdout.queue(EndSynchronizedUpdate)?.flush()
|
stdout.queue(EndSynchronizedUpdate)?.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_term_size(&mut self, width: u16, height: u16) {
|
fn update_rows(&mut self) {
|
||||||
self.term_width = width;
|
self.n_rows_with_filter = match self.filter {
|
||||||
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 {
|
|
||||||
Filter::Done => self
|
Filter::Done => self
|
||||||
.app_state
|
.app_state
|
||||||
.exercises()
|
.exercises()
|
||||||
|
@ -270,42 +300,46 @@ impl<'a> ListState<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.n_rows_with_filter == 0 {
|
if self.n_rows_with_filter == 0 {
|
||||||
self.selected = None;
|
self.selected_row = None;
|
||||||
} else {
|
} else {
|
||||||
self.selected = Some(
|
self.selected_row = Some(
|
||||||
self.selected
|
self.selected_row
|
||||||
.map_or(0, |selected| selected.min(self.n_rows_with_filter - 1)),
|
.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) {
|
pub fn select_next(&mut self) {
|
||||||
if self.n_rows_with_filter > 0 {
|
if let Some(selected) = self.selected_row {
|
||||||
let next = self.selected.map_or(0, |selected| {
|
self.selected_row = Some((selected + 1).min(self.n_rows_with_filter - 1));
|
||||||
(selected + 1).min(self.n_rows_with_filter - 1)
|
|
||||||
});
|
|
||||||
self.selected = Some(next);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_previous(&mut self) {
|
pub fn select_previous(&mut self) {
|
||||||
if self.n_rows_with_filter > 0 {
|
if let Some(selected) = self.selected_row {
|
||||||
let previous = self
|
self.selected_row = Some(selected.saturating_sub(1));
|
||||||
.selected
|
|
||||||
.map_or(0, |selected| selected.saturating_sub(1));
|
|
||||||
self.selected = Some(previous);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_first(&mut self) {
|
pub fn select_first(&mut self) {
|
||||||
if self.n_rows_with_filter > 0 {
|
if self.n_rows_with_filter > 0 {
|
||||||
self.selected = Some(0);
|
self.selected_row = Some(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_last(&mut self) {
|
pub fn select_last(&mut self) {
|
||||||
if self.n_rows_with_filter > 0 {
|
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<()> {
|
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!");
|
self.message.push_str("Nothing selected to reset!");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let exercise_ind = self.selected_to_exercise_ind(selected)?;
|
let exercise_ind = self.selected_to_exercise_ind(selected)?;
|
||||||
let exercise_path = self.app_state.reset_exercise_by_ind(exercise_ind)?;
|
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")?;
|
write!(self.message, "The exercise {exercise_path} has been reset")?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -348,13 +383,14 @@ impl<'a> ListState<'a> {
|
||||||
|
|
||||||
// Return `true` if there was something to select.
|
// Return `true` if there was something to select.
|
||||||
pub fn selected_to_current_exercise(&mut self) -> Result<bool> {
|
pub fn selected_to_current_exercise(&mut self) -> Result<bool> {
|
||||||
let Some(selected) = self.selected else {
|
let Some(selected) = self.selected_row else {
|
||||||
self.message.push_str("Nothing selected to continue at!");
|
self.message.push_str("Nothing selected to continue at!");
|
||||||
return Ok(false);
|
return Ok(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
let exercise_ind = self.selected_to_exercise_ind(selected)?;
|
let exercise_ind = self.selected_to_exercise_ind(selected)?;
|
||||||
self.app_state.set_current_exercise_ind(exercise_ind)?;
|
self.app_state.set_current_exercise_ind(exercise_ind)?;
|
||||||
|
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue