From 632977a0dc4c01d3a291d3f595d7f7fe911d748b Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 13 May 2025 20:25:44 +0300 Subject: [PATCH 1/5] dsl: implement Display for Value --- dsl/src/dsl_parse.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/dsl/src/dsl_parse.rs b/dsl/src/dsl_parse.rs index ee06792..d81c54b 100644 --- a/dsl/src/dsl_parse.rs +++ b/dsl/src/dsl_parse.rs @@ -189,6 +189,21 @@ pub const fn to_digit (c: char) -> DslResult { Exp(usize, TokenIter<'source>), } +impl<'source> std::fmt::Display for Value<'source> { + fn fmt (&self, out: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(out, "{}", match self { + Nil => String::new(), + Err(e) => format!("[error: {e}]"), + Num(n) => format!("{n}"), + Sym(s) => format!("{s}"), + Key(s) => format!("{s}"), + Str(s) => format!("{s}"), + Exp(_, e) => format!("{e:?}"), + }); + Ok(()) + } +} + impl<'source> Token<'source> { pub const fn new ( source: &'source str, start: usize, length: usize, value: Value<'source> From b45ac8f417b2f4e83e116a9ee5fe4bf3ad57a726 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 13 May 2025 20:25:55 +0300 Subject: [PATCH 2/5] tui: remove FileBrowser --- tui/src/tui_content/tui_file.rs | 87 --------------------------------- 1 file changed, 87 deletions(-) diff --git a/tui/src/tui_content/tui_file.rs b/tui/src/tui_content/tui_file.rs index 401b8ae..e69de29 100644 --- a/tui/src/tui_content/tui_file.rs +++ b/tui/src/tui_content/tui_file.rs @@ -1,87 +0,0 @@ -use crate::*; -/// Browses for phrase to import/export -#[derive(Debug, Clone)] -pub struct FileBrowser { - pub cwd: PathBuf, - pub dirs: Vec<(OsString, String)>, - pub files: Vec<(OsString, String)>, - pub filter: String, - pub index: usize, - pub scroll: usize, - pub size: Measure -} -/// Commands supported by [FileBrowser] -#[derive(Debug, Clone, PartialEq)] -pub enum FileBrowserCommand { - Begin, - Cancel, - Confirm, - Select(usize), - Chdir(PathBuf), - Filter(Arc), -} -content!(TuiOut: |self: FileBrowser| /*Stack::down(|add|{ - let mut i = 0; - for (_, name) in self.dirs.iter() { - if i >= self.scroll { - add(&Tui::bold(i == self.index, name.as_str()))?; - } - i += 1; - } - for (_, name) in self.files.iter() { - if i >= self.scroll { - add(&Tui::bold(i == self.index, name.as_str()))?; - } - i += 1; - } - add(&format!("{}/{i}", self.index))?; - Ok(()) -})*/"todo"); -impl FileBrowser { - pub fn new (cwd: Option) -> Usually { - let cwd = if let Some(cwd) = cwd { cwd } else { std::env::current_dir()? }; - let mut dirs = vec![]; - let mut files = vec![]; - for entry in std::fs::read_dir(&cwd)? { - let entry = entry?; - let name = entry.file_name(); - let decoded = name.clone().into_string().unwrap_or_else(|_|"".to_string()); - let meta = entry.metadata()?; - if meta.is_dir() { - dirs.push((name, format!("📁 {decoded}"))); - } else if meta.is_file() { - files.push((name, format!("📄 {decoded}"))); - } - } - Ok(Self { - cwd, - dirs, - files, - filter: "".to_string(), - index: 0, - scroll: 0, - size: Measure::new(), - }) - } - pub fn len (&self) -> usize { - self.dirs.len() + self.files.len() - } - pub fn is_dir (&self) -> bool { - self.index < self.dirs.len() - } - pub fn is_file (&self) -> bool { - self.index >= self.dirs.len() - } - pub fn path (&self) -> PathBuf { - self.cwd.join(if self.is_dir() { - &self.dirs[self.index].0 - } else if self.is_file() { - &self.files[self.index - self.dirs.len()].0 - } else { - unreachable!() - }) - } - pub fn chdir (&self) -> Usually { - Self::new(Some(self.path())) - } -} From 663efede640135fbb3182b586ea193b34ffd72f0 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 13 May 2025 20:27:12 +0300 Subject: [PATCH 3/5] dsl: don't eat error --- dsl/src/dsl_parse.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dsl/src/dsl_parse.rs b/dsl/src/dsl_parse.rs index d81c54b..81a0336 100644 --- a/dsl/src/dsl_parse.rs +++ b/dsl/src/dsl_parse.rs @@ -199,8 +199,7 @@ impl<'source> std::fmt::Display for Value<'source> { Key(s) => format!("{s}"), Str(s) => format!("{s}"), Exp(_, e) => format!("{e:?}"), - }); - Ok(()) + }) } } From 5d546affed5c643e5812000297321ee60fce8fde Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 13 May 2025 20:27:41 +0300 Subject: [PATCH 4/5] tui: remove tui_file --- tui/src/tui_content.rs | 1 - tui/src/tui_content/tui_file.rs | 0 2 files changed, 1 deletion(-) delete mode 100644 tui/src/tui_content/tui_file.rs diff --git a/tui/src/tui_content.rs b/tui/src/tui_content.rs index 0ea658c..6b668c5 100644 --- a/tui/src/tui_content.rs +++ b/tui/src/tui_content.rs @@ -24,7 +24,6 @@ impl> Content for std::sync::Arc { mod tui_border; pub use self::tui_border::*; mod tui_color; pub use self::tui_color::*; mod tui_field; pub use self::tui_field::*; -mod tui_file; pub use self::tui_file::*; mod tui_phat; pub use self::tui_phat::*; mod tui_repeat; pub use self::tui_repeat::*; mod tui_scroll; pub use self::tui_scroll::*; diff --git a/tui/src/tui_content/tui_file.rs b/tui/src/tui_content/tui_file.rs deleted file mode 100644 index e69de29..0000000 From bad20f5037dc22d572a8381840fab871ce65f565 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Wed, 14 May 2025 00:46:09 +0300 Subject: [PATCH 5/5] tui: add button_2, button_3 --- tui/src/tui_content.rs | 1 + tui/src/tui_content/tui_button.rs | 40 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tui/src/tui_content/tui_button.rs diff --git a/tui/src/tui_content.rs b/tui/src/tui_content.rs index 6b668c5..38960b9 100644 --- a/tui/src/tui_content.rs +++ b/tui/src/tui_content.rs @@ -22,6 +22,7 @@ impl> Content for std::sync::Arc { } mod tui_border; pub use self::tui_border::*; +mod tui_button; pub use self::tui_button::*; mod tui_color; pub use self::tui_color::*; mod tui_field; pub use self::tui_field::*; mod tui_phat; pub use self::tui_phat::*; diff --git a/tui/src/tui_content/tui_button.rs b/tui/src/tui_content/tui_button.rs new file mode 100644 index 0000000..2a297da --- /dev/null +++ b/tui/src/tui_content/tui_button.rs @@ -0,0 +1,40 @@ +use crate::{*, Color::*}; + +pub fn button_2 <'a> ( + key: impl Content + 'a, label: impl Content + 'a, editing: bool, +) -> impl Content + 'a { + let key = Tui::fg_bg(Tui::g(0), Tui::orange(), Bsp::e( + Tui::fg_bg(Tui::orange(), Reset, "▐"), + Bsp::e(key, Tui::fg(Tui::g(96), "▐")) + )); + let label = When::new(!editing, Tui::fg_bg(Tui::g(255), Tui::g(96), label)); + Tui::bold(true, Bsp::e(key, label)) +} + +pub fn button_3 <'a, K, L, V> ( + key: K, + label: L, + value: V, + editing: bool, +) -> impl Content + 'a where + K: Content + 'a, + L: Content + 'a, + V: Content + 'a, +{ + let key = Tui::fg_bg(Tui::g(0), Tui::orange(), + Bsp::e(Tui::fg_bg(Tui::orange(), Reset, "▐"), Bsp::e(key, Tui::fg(if editing { + Tui::g(128) + } else { + Tui::g(96) + }, "▐")))); + let label = Bsp::e( + When::new(!editing, Bsp::e( + Tui::fg_bg(Tui::g(255), Tui::g(96), label), + Tui::fg_bg(Tui::g(128), Tui::g(96), "▐"), + )), + Bsp::e( + Tui::fg_bg(Tui::g(224), Tui::g(128), value), + Tui::fg_bg(Tui::g(128), Reset, "▌"), + )); + Tui::bold(true, Bsp::e(key, label)) +}