impl Default, explicitly use SortMode, autoformatting

This commit is contained in:
kunkankinkan 2024-09-30 17:44:55 +02:00
parent 78f8585d27
commit 7bd97697c2

View File

@ -17,6 +17,7 @@ use crossterm::{
}; };
/// state of the user interface /// state of the user interface
#[derive(Debug)]
pub struct Ui<T> pub struct Ui<T>
where where
T: io::Write, T: io::Write,
@ -35,7 +36,7 @@ where
pub height: usize, pub height: usize,
pub active_entry_idx: usize, pub active_entry_idx: usize,
pub current_scroll_offset: usize, pub current_scroll_offset: usize,
queue_sort_update: bool queue_sort_update: bool,
} }
impl<T> Ui<T> impl<T> Ui<T>
@ -59,7 +60,7 @@ where
inactive_color_pair, inactive_color_pair,
inactive_done_color_pair, inactive_done_color_pair,
header_color_pair, header_color_pair,
current_sort_mode: Default, current_sort_mode: SortMode::Default,
active_entry_idx: 0, active_entry_idx: 0,
scrolloff: 8, scrolloff: 8,
current_scroll_offset: 0, current_scroll_offset: 0,
@ -95,6 +96,7 @@ where
RestorePosition, RestorePosition,
cursor::Show cursor::Show
)?; )?;
writeln!(self.ostream)?;
Ok(()) Ok(())
} }
@ -209,11 +211,15 @@ where
/// update the index of the first *shown* entry using `self.scrolloff` /// update the index of the first *shown* entry using `self.scrolloff`
pub fn update_scroll_offset(&mut self) { pub fn update_scroll_offset(&mut self) {
if self.current_scroll_offset + self.scrolloff >= self.active_entry_idx { if self.current_scroll_offset + self.scrolloff >= self.active_entry_idx {
let diff = (self.current_scroll_offset + self.scrolloff).abs_diff(self.active_entry_idx); let diff =
(self.current_scroll_offset + self.scrolloff).abs_diff(self.active_entry_idx);
self.current_scroll_offset = self.current_scroll_offset.saturating_sub(diff); self.current_scroll_offset = self.current_scroll_offset.saturating_sub(diff);
} else if (self.current_scroll_offset + self.inner_height()).saturating_sub(self.scrolloff)
} else if (self.current_scroll_offset + self.inner_height()).saturating_sub(self.scrolloff) <= self.active_entry_idx { <= self.active_entry_idx
let diff = (self.current_scroll_offset + self.inner_height()).saturating_sub(self.scrolloff).abs_diff(self.active_entry_idx); {
let diff = (self.current_scroll_offset + self.inner_height())
.saturating_sub(self.scrolloff)
.abs_diff(self.active_entry_idx);
self.current_scroll_offset = (self.current_scroll_offset + diff).min( self.current_scroll_offset = (self.current_scroll_offset + diff).min(
self.document self.document
.entries .entries
@ -269,11 +275,11 @@ where
pub fn cycle_sort_mode(&mut self) { pub fn cycle_sort_mode(&mut self) {
self.current_sort_mode = match self.current_sort_mode { self.current_sort_mode = match self.current_sort_mode {
Default => ByDeadlineDescending, SortMode::Default => SortMode::ByDeadlineDescending,
ByDeadlineDescending => ByDeadlineAscending, SortMode::ByDeadlineDescending => SortMode::ByDeadlineAscending,
ByDeadlineAscending => ByTextAscending, SortMode::ByDeadlineAscending => SortMode::ByTextAscending,
ByTextAscending => ByTextDescending, SortMode::ByTextAscending => SortMode::ByTextDescending,
ByTextDescending => Default, SortMode::ByTextDescending => SortMode::Default,
}; };
self.queue_sort_update = true; self.queue_sort_update = true;
} }
@ -281,17 +287,24 @@ where
pub fn apply_sort_mode(&mut self) { pub fn apply_sort_mode(&mut self) {
if self.queue_sort_update { if self.queue_sort_update {
match self.current_sort_mode { match self.current_sort_mode {
Default => self.document = self.original_document.clone(), SortMode::Default => self.document = self.original_document.clone(),
ByDeadlineDescending => { SortMode::ByDeadlineDescending => {
self.document.entries.sort_by_key(|entry| entry.deadline); self.document.entries.sort_by_key(|entry| entry.deadline);
self.document.entries.reverse(); self.document.entries.reverse();
}, }
ByDeadlineAscending => self.document.entries.sort_by_key(|entry| entry.deadline), SortMode::ByDeadlineAscending => {
ByTextAscending => self.document.entries.sort_by_key(|entry| entry.text.to_lowercase()), self.document.entries.sort_by_key(|entry| entry.deadline)
ByTextDescending => { }
self.document.entries.sort_by_key(|entry| entry.text.to_lowercase()); SortMode::ByTextAscending => self
.document
.entries
.sort_by_key(|entry| entry.text.to_lowercase()),
SortMode::ByTextDescending => {
self.document
.entries
.sort_by_key(|entry| entry.text.to_lowercase());
self.document.entries.reverse(); self.document.entries.reverse();
}, }
} }
self.queue_sort_update = false; self.queue_sort_update = false;
} }
@ -303,17 +316,27 @@ where
} }
} }
impl<T> Default for Ui<T>
where
T: Default + io::Write,
{
fn default() -> Self {
Self::init(T::default(), Document::default())
}
}
pub enum MoveDirection { pub enum MoveDirection {
Down, Down,
Up, Up,
} }
pub use MoveDirection::*; pub use MoveDirection::*;
#[derive(Debug, Default)]
pub enum SortMode { pub enum SortMode {
#[default]
Default, Default,
ByDeadlineDescending, ByDeadlineDescending,
ByDeadlineAscending, ByDeadlineAscending,
ByTextAscending, ByTextAscending,
ByTextDescending, ByTextDescending,
} }
pub use SortMode::*;