From 4619d0ea76bf4d9fcb0b645c57d5bf5d6ab93edf Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 6 Apr 2025 16:22:12 +0300 Subject: [PATCH] stub help modal; cleanup --- src/keys.rs | 18 +++++++++-------- src/keys/edit.rs | 2 +- src/keys/help.rs | 13 +++++++++++++ src/keys/quit.rs | 24 +++++++++++------------ src/keys/save.rs | 18 ++++++++--------- src/model.rs | 15 ++++----------- src/model/metadata.rs | 7 +++---- src/view.rs | 4 ++-- src/view/dialog.rs | 45 +++++++++++++++++++++++-------------------- 9 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 src/keys/help.rs diff --git a/src/keys.rs b/src/keys.rs index 2f687e4..ccbee6c 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -20,9 +20,10 @@ macro_rules! press { }; } -mod edit; pub use self::edit::*; -mod save; pub use self::save::*; -mod quit; pub use self::quit::*; +mod edit; +mod save; +mod quit; +mod help; impl Handle for Taggart { fn handle (&mut self, input: &TuiIn) -> Perhaps { @@ -30,10 +31,12 @@ impl Handle for Taggart { let max = self.offset + self.display.h().saturating_sub(1); let event = &*input.event(); match &self.mode { - Some(Mode::Edit { .. }) => self.mode_edit(event), - Some(Mode::Save { value }) => self.mode_save(event, *value), - Some(Mode::Quit { value }) => self.mode_quit(input, event, *value), + Some(Mode::Edit { .. }) => self.handle_edit(event), + Some(Mode::Help { page }) => self.handle_help(event, *page), + Some(Mode::Save { choice }) => self.handle_save(event, *choice), + Some(Mode::Quit { choice }) => self.handle_quit(input, *choice), None => match event { + press!(F(1)) => { self.help_begin() }, press!(Char('q')) => { self.quit_begin(&input) }, press!(Char('w')) => { self.save_begin() }, press!(Enter) => { self.edit_begin() }, @@ -48,8 +51,7 @@ impl Handle for Taggart { press!(Char('[')) => { self.columns.0[self.column].width = self.columns.0[self.column].width.saturating_sub(1).max(5); } _ => {} - } - _ => todo!("{:?}", self.mode) + }, } self.clamp(min, max); Ok(None) diff --git a/src/keys/edit.rs b/src/keys/edit.rs index 17eaa2e..00c4b08 100644 --- a/src/keys/edit.rs +++ b/src/keys/edit.rs @@ -1,7 +1,7 @@ use crate::*; impl Taggart { - pub fn mode_edit (&mut self, event: &Event) { + pub fn handle_edit (&mut self, event: &Event) { match event { press!(Char(c)) => self.edit_insert(*c), press!(Shift-Char(c)) => self.edit_insert(c.to_uppercase().next().unwrap()), diff --git a/src/keys/help.rs b/src/keys/help.rs new file mode 100644 index 0000000..65fa90d --- /dev/null +++ b/src/keys/help.rs @@ -0,0 +1,13 @@ +use crate::*; + +impl Taggart { + pub fn handle_help (&mut self, event: &Event, _page: u8) { + match event { + press!(Esc) => { self.mode = None }, + _ => {} + } + } + pub fn help_begin (&mut self) { + self.mode = Some(Mode::Help { page: 0 }) + } +} diff --git a/src/keys/quit.rs b/src/keys/quit.rs index cade134..ffd680e 100644 --- a/src/keys/quit.rs +++ b/src/keys/quit.rs @@ -1,23 +1,16 @@ use crate::*; impl Taggart { - pub fn quit_begin (&mut self, input: &TuiIn) { - if self.tasks.len() == 0 { - input.done() - } else { - self.mode = Some(Mode::Quit { value: 1 }) - } - } - pub fn mode_quit (&mut self, input: &TuiIn, event: &Event, value: u8) { - match event { + pub fn handle_quit (&mut self, input: &TuiIn, choice: u8) { + match &*input.event() { press!(Esc) => { self.mode = None }, press!(Left) => { self.mode = Some(Mode::Quit { - value: value.overflowing_sub(1).0.min(2) + choice: choice.overflowing_sub(1).0.min(2) }) }, press!(Right) => { self.mode = Some(Mode::Quit { - value: (value + 1) % 3 + choice: (choice + 1) % 3 }) }, - press!(Enter) => match value { + press!(Enter) => match choice { 0 => { input.done() }, 1 => { self.mode = None }, 2 => todo!(), @@ -26,4 +19,11 @@ impl Taggart { _ => {} } } + pub fn quit_begin (&mut self, input: &TuiIn) { + if self.tasks.len() == 0 { + input.done() + } else { + self.mode = Some(Mode::Quit { choice: 1 }) + } + } } diff --git a/src/keys/save.rs b/src/keys/save.rs index e0e5958..fdfeb90 100644 --- a/src/keys/save.rs +++ b/src/keys/save.rs @@ -1,21 +1,16 @@ use crate::*; impl Taggart { - pub fn save_begin (&mut self) { - if self.tasks.len() > 0 { - self.mode = Some(Mode::Save { value: 1 }) - } - } - pub fn mode_save (&mut self, event: &Event, value: u8) { + pub fn handle_save (&mut self, event: &Event, choice: u8) { match event { press!(Esc) => { self.mode = None } press!(Left) => { self.mode = Some(Mode::Save { - value: value.overflowing_sub(1).0.min(2) + choice: choice.overflowing_sub(1).0.min(2) }) }, press!(Right) => { self.mode = Some(Mode::Save { - value: (value + 1) % 3 + choice: (choice + 1) % 3 }) }, - press!(Enter) => match value { + press!(Enter) => match choice { 0 => { self.tasks = vec![]; self.mode = None; }, 1 => { self.mode = None }, 2 => todo!(), @@ -24,4 +19,9 @@ impl Taggart { _ => {} } } + pub fn save_begin (&mut self) { + if self.tasks.len() > 0 { + self.mode = Some(Mode::Save { choice: 1 }) + } + } } diff --git a/src/model.rs b/src/model.rs index cca1b67..e1c695a 100644 --- a/src/model.rs +++ b/src/model.rs @@ -20,17 +20,10 @@ pub struct Taggart { #[derive(Debug)] pub enum Mode { - Help, - Edit { - value: Arc, - index: usize, - }, - Save { - value: u8, - }, - Quit { - value: u8, - }, + Help { page: u8 }, + Edit { value: Arc, index: usize, }, + Save { choice: u8, }, + Quit { choice: u8, }, } impl Taggart { diff --git a/src/model/metadata.rs b/src/model/metadata.rs index 1980c80..a76cc13 100644 --- a/src/model/metadata.rs +++ b/src/model/metadata.rs @@ -1,13 +1,12 @@ use crate::*; use std::fs::File; use std::io::{BufReader, Read}; -use std::borrow::Borrow; use byte_unit::{Byte, Unit::MB}; use lofty::{ probe::Probe, file::TaggedFileExt, config::{ParseOptions, ParsingMode}, - tag::{Accessor, Tag, TagItem, ItemKey, ItemValue, TagType} + tag::{Accessor, Tag, TagItem, TagType} }; pub enum Metadata { @@ -139,7 +138,7 @@ macro_rules! music_tag_field { } pub fn $set (&mut self, value: &impl AsRef) -> Option { let value = value.as_ref().trim(); - if let &mut Metadata::Music { ref original_tag, ref mut modified_tag, .. } = self { + if let &mut Metadata::Music { ref mut modified_tag, .. } = self { match (value.len(), &modified_tag) { (0, Some(new_tag)) => { if new_tag.read().unwrap().item_count() <= 1 { @@ -184,7 +183,7 @@ macro_rules! music_tag_field { } pub fn $set (&mut self, value: &impl AsRef) -> Option { let value = value.as_ref().trim(); - if let &mut Metadata::Music { ref original_tag, ref mut modified_tag, .. } = self + if let &mut Metadata::Music { ref mut modified_tag, .. } = self && let Ok(numeric_value) = value.parse::() { match (value.len(), &modified_tag) { diff --git a/src/view.rs b/src/view.rs index ecd37f7..d6a082b 100644 --- a/src/view.rs +++ b/src/view.rs @@ -3,8 +3,8 @@ pub(crate) use tengri::tui::ratatui::style::{Color, Modifier}; pub(crate) use pad::PadStr; mod table; pub use self::table::*; -mod dialog; pub use self::dialog::*; -mod status; pub use self::status::*; +mod dialog; +mod status; impl Content for Taggart { fn content (&self) -> impl Render { diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 4ddb3be..ba10821 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -10,40 +10,43 @@ impl Taggart { let bg = |x|Bsp::a(x, Repeat(" ")); pos(style(border(bg(content)))) } - fn dialog_help (&self) { + fn dialog_help (&self, _page: u8) -> impl Content { + Self::dialog(Bsp::s( + "Help", + "TODO", + )) } - fn dialog_save (&self, value: u8) { + fn dialog_save (&self, choice: u8) -> impl Content { let choices = [ - if value == 0 { "[ Clear changes ]" } else { " Clear changes " }, - if value == 1 { "[ Continue editing ]" } else { " Continue editing " }, - if value == 2 { "[ Write and continue ]" } else { " Write and continue " }, + if choice == 0 { "[ Clear changes ]" } else { " Clear changes " }, + if choice == 1 { "[ Continue editing ]" } else { " Continue editing " }, + if choice == 2 { "[ Write and continue ]" } else { " Write and continue " }, ]; Self::dialog(Bsp::s( format!("Save {} change(s)?", self.tasks.len()), - Bsp::s("", Bsp::e(choices[0], Bsp::e(choices[1], choices[2]))))); + Bsp::s("", Bsp::e(choices[0], Bsp::e(choices[1], choices[2]))))) } - fn dialog_quit (&self, value: u8) { + fn dialog_quit (&self, choice: u8) -> impl Content { let choices = [ - if value == 0 { "[ Exit without saving ]" } else { " Exit without saving " }, - if value == 1 { "[ Cancel ]" } else { " Cancel " }, - if value == 2 { "[ Write and exit ]" } else { " Write and exit " }, + if choice == 0 { "[ Exit without saving ]" } else { " Exit without saving " }, + if choice == 1 { "[ Cancel ]" } else { " Cancel " }, + if choice == 2 { "[ Write and exit ]" } else { " Write and exit " }, ]; Self::dialog(Bsp::s( format!("Save {} change(s) before exiting?", self.tasks.len()), - Bsp::s("", Bsp::e(choices[0], Bsp::e(choices[1], choices[2]))))); + Bsp::s("", Bsp::e(choices[0], Bsp::e(choices[1], choices[2]))))) } pub fn render_dialog (&self, to: &mut TuiOut) { match self.mode { - Some(Mode::Save { value }) => { - to.tint_all(Color::Rgb(96,96,96), Color::Rgb(48,48,48), Modifier::DIM); - Content::render(&self.dialog_save(value), to) - }, - Some(Mode::Quit { value }) => { - to.tint_all(Color::Rgb(96,96,96), Color::Rgb(48,48,48), Modifier::DIM); - Content::render(&self.dialog_quit(value), to) - }, - Some(Mode::Help { .. }) => { - }, + Some(Mode::Edit { .. }) => {}, + Some(_) => to.tint_all(Color::Rgb(96,96,96), Color::Rgb(48,48,48), Modifier::DIM), + _ => {} + } + match self.mode { + Some(Mode::Edit { .. }) => {}, + Some(Mode::Save { choice }) => Content::render(&self.dialog_save(choice), to), + Some(Mode::Quit { choice }) => Content::render(&self.dialog_quit(choice), to), + Some(Mode::Help { page }) => Content::render(&self.dialog_help(page), to), _ => {}, } }