From 2171412709fa391f499399fe35f6c24805bd45a9 Mon Sep 17 00:00:00 2001 From: jooaf Date: Thu, 10 Apr 2025 22:25:13 -0500 Subject: [PATCH 1/3] adding light mode --- Cargo.lock | 57 ++++++++++++++++++- Cargo.toml | 3 + src/cli.rs | 45 ++++++++++++++- src/config.rs | 78 ++++++++++++++++++++++++++ src/lib.rs | 5 ++ src/main.rs | 10 +++- src/scrollable_textarea.rs | 39 ++++++------- src/theme.rs | 59 ++++++++++++++++++++ src/ui.rs | 110 ++++++++++++++++++++++++++----------- src/ui_handler.rs | 89 ++++++++++++++++++++++++------ 10 files changed, 424 insertions(+), 71 deletions(-) create mode 100644 src/config.rs create mode 100644 src/theme.rs diff --git a/Cargo.lock b/Cargo.lock index 9739106..ef77289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1246,6 +1246,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "signal-hook" version = "0.3.17" @@ -1427,7 +1436,7 @@ dependencies = [ [[package]] name = "thoth-cli" -version = "0.1.79" +version = "0.1.80" dependencies = [ "anyhow", "arboard", @@ -1436,16 +1445,19 @@ dependencies = [ "crossterm", "dirs", "fuzzy-matcher", + "lazy_static", "mockall", "once_cell", "pulldown-cmark", "pulldown-cmark-to-cmark", "rand", "ratatui 0.27.0", + "serde", "serde_json", "syntect", "syntect-tui", "tempfile", + "toml", "tui-textarea", "unicode-width", ] @@ -1502,6 +1514,40 @@ dependencies = [ "time-core", ] +[[package]] +name = "toml" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tui-textarea" version = "0.5.1" @@ -1755,6 +1801,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63d3fcd9bba44b03821e7d699eeee959f3126dcc4aa8e4ae18ec617c2a5cea10" +dependencies = [ + "memchr", +] + [[package]] name = "x11rb" version = "0.13.1" diff --git a/Cargo.toml b/Cargo.toml index c82505a..2c13c8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ lto = true [dependencies] serde_json = "1.0" +serde = { version = "1.0", features = ["derive"] } +toml = "0.8" syntect = "5.1" pulldown-cmark = { version = "0.11.0" } pulldown-cmark-to-cmark = "15.0.1" @@ -36,6 +38,7 @@ rand = "0.8.5" once_cell = "1.19.0" arboard = "3.4.1" fuzzy-matcher = "0.3.7" +lazy_static = "1.5.0" [[bin]] name = "thoth" diff --git a/src/cli.rs b/src/cli.rs index 7206fa0..34196e1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,7 @@ -use crate::{get_save_backup_file_path, load_textareas, save_textareas, EditorClipboard}; +use crate::{ + get_save_backup_file_path, load_textareas, save_textareas, EditorClipboard, ThemeMode, + ThothConfig, +}; use anyhow::{bail, Result}; use std::{ fs::File, @@ -48,6 +51,46 @@ pub enum Commands { /// The name of the block to be used name: String, }, + /// Set the theme to light or dark mode + Theme { + /// The theme to set: 'light' or 'dark' + mode: String, + }, + /// Get the current theme + GetTheme, +} + +pub fn set_theme(mode: &str) -> Result<()> { + let mode_lowercase = mode.to_lowercase(); + + let mut config = ThothConfig::load()?; + + match mode_lowercase.as_str() { + "light" => { + config.set_theme(ThemeMode::Light)?; + println!("Theme set to light mode"); + } + "dark" => { + config.set_theme(ThemeMode::Dark)?; + println!("Theme set to dark mode"); + } + _ => { + bail!("Invalid theme mode. Use 'light' or 'dark'"); + } + } + + Ok(()) +} + +pub fn get_theme() -> Result<()> { + let config = ThothConfig::load()?; + + match config.theme { + ThemeMode::Light => println!("Current theme: light"), + ThemeMode::Dark => println!("Current theme: dark"), + } + + Ok(()) } pub fn read_clipboard_backup() -> Result<()> { diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..36f7cb8 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,78 @@ +use anyhow::Result; +use dirs::home_dir; +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::PathBuf; + +use crate::ThemeColors; +use crate::DARK_MODE_COLORS; +use crate::LIGHT_MODE_COLORS; + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +pub enum ThemeMode { + Light, + Dark, +} + +impl Default for ThemeMode { + fn default() -> Self { + ThemeMode::Dark + } +} + +#[derive(Debug, Serialize, Deserialize, Default)] +pub struct ThothConfig { + pub theme: ThemeMode, +} + +impl ThothConfig { + pub fn load() -> Result { + let config_path = get_config_path(); + + if !config_path.exists() { + let default_config = Self::default(); + default_config.save()?; + return Ok(default_config); + } + + let config_str = fs::read_to_string(config_path)?; + let config: ThothConfig = toml::from_str(&config_str)?; + Ok(config) + } + + pub fn save(&self) -> Result<()> { + let config_path = get_config_path(); + + // Create directory if it doesn't exist + if let Some(parent) = config_path.parent() { + if !parent.exists() { + fs::create_dir_all(parent)?; + } + } + + let config_str = toml::to_string(self)?; + fs::write(config_path, config_str)?; + Ok(()) + } + + pub fn set_theme(&mut self, theme: ThemeMode) -> Result<()> { + self.theme = theme; + self.save()?; + Ok(()) + } + + pub fn get_theme_colors(&self) -> &'static ThemeColors { + match self.theme { + ThemeMode::Light => &LIGHT_MODE_COLORS, + ThemeMode::Dark => &DARK_MODE_COLORS, + } + } +} + +pub fn get_config_path() -> PathBuf { + let mut path = home_dir().unwrap_or_default(); + path.push(".config"); + path.push("thoth"); + path.push("config.toml"); + path +} diff --git a/src/lib.rs b/src/lib.rs index e20271c..2f269bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,10 @@ pub mod cli; pub mod clipboard; +pub mod config; pub mod formatter; pub mod markdown_renderer; pub mod scrollable_textarea; +pub mod theme; pub mod title_popup; pub mod title_select_popup; pub mod ui; @@ -11,11 +13,13 @@ pub mod utils; pub use clipboard::ClipboardTrait; pub use clipboard::EditorClipboard; +pub use config::{ThemeMode, ThothConfig}; use dirs::home_dir; pub use formatter::{format_json, format_markdown}; pub use markdown_renderer::MarkdownRenderer; pub use scrollable_textarea::ScrollableTextArea; use std::path::PathBuf; +pub use theme::{ThemeColors, DARK_MODE_COLORS, LIGHT_MODE_COLORS}; pub use title_popup::TitlePopup; pub use title_select_popup::TitleSelectPopup; pub use utils::{load_textareas, save_textareas}; @@ -30,6 +34,7 @@ pub fn get_clipboard_backup_file_path() -> PathBuf { home_dir().unwrap_or_default().join("thoth_clipboard.txt") } +// The ORANGE constant is kept for backward compatibility pub const ORANGE: ratatui::style::Color = ratatui::style::Color::Rgb(255, 165, 0); pub const DAEMONIZE_ARG: &str = "__thoth_copy_daemonize"; pub const MIN_TEXTAREA_HEIGHT: usize = 3; diff --git a/src/main.rs b/src/main.rs index 2c1e904..01ea0bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ use std::{ }; use thoth_cli::{ cli::{ - add_block, copy_block, delete_block, list_blocks, read_clipboard_backup, - replace_from_backup, view_block, + add_block, copy_block, delete_block, get_theme, list_blocks, read_clipboard_backup, + replace_from_backup, set_theme, view_block, }, get_save_backup_file_path, EditorClipboard, }; @@ -66,6 +66,12 @@ fn main() -> Result<()> { Some(Commands::Copy { name }) => { copy_block(name)?; } + Some(Commands::Theme { mode }) => { + set_theme(mode)?; + } + Some(Commands::GetTheme) => { + get_theme()?; + } None => { run_ui()?; } diff --git a/src/scrollable_textarea.rs b/src/scrollable_textarea.rs index 15bae32..ca36915 100644 --- a/src/scrollable_textarea.rs +++ b/src/scrollable_textarea.rs @@ -5,14 +5,14 @@ use std::{ rc::Rc, }; -use crate::{EditorClipboard, BORDER_PADDING_SIZE, MIN_TEXTAREA_HEIGHT}; -use crate::{MarkdownRenderer, ORANGE}; +use crate::MarkdownRenderer; +use crate::{EditorClipboard, ThemeColors, BORDER_PADDING_SIZE, MIN_TEXTAREA_HEIGHT}; use anyhow; use anyhow::Result; use rand::Rng; use ratatui::{ layout::{Constraint, Direction, Layout, Rect}, - style::{Color, Style}, + style::Style, text::Text, widgets::{Block, Borders, Paragraph, Wrap}, Frame, @@ -297,33 +297,33 @@ impl ScrollableTextArea { Ok(()) } - fn render_full_screen_edit(&mut self, f: &mut Frame, area: Rect) { + fn render_full_screen_edit(&mut self, f: &mut Frame, area: Rect, theme: &ThemeColors) { let textarea = &mut self.textareas[self.focused_index]; let title = &self.titles[self.focused_index]; let block = Block::default() .title(title.clone()) .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)); + .border_style(Style::default().fg(theme.primary)); - let edit_style = Style::default().fg(Color::White).bg(Color::Black); - let cursor_style = Style::default().fg(Color::White).bg(ORANGE); + let edit_style = Style::default().fg(theme.foreground).bg(theme.background); + let cursor_style = Style::default().fg(theme.foreground).bg(theme.accent); textarea.set_block(block); textarea.set_style(edit_style); textarea.set_cursor_style(cursor_style); - textarea.set_selection_style(Style::default().bg(Color::Red)); + textarea.set_selection_style(Style::default().bg(theme.selection)); f.render_widget(textarea.widget(), area); } - pub fn render(&mut self, f: &mut Frame, area: Rect) -> Result<()> { + pub fn render(&mut self, f: &mut Frame, area: Rect, theme: &ThemeColors) -> Result<()> { self.viewport_height = area.height; if self.full_screen_mode { if self.edit_mode { - self.render_full_screen_edit(f, area); + self.render_full_screen_edit(f, area, theme); } else { - self.render_full_screen(f, area)?; + self.render_full_screen(f, area, theme)?; } } else { let mut remaining_height = area.height; @@ -371,24 +371,25 @@ impl ScrollableTextArea { let style = if is_focused { if is_editing { - Style::default().fg(Color::White).bg(Color::Black) + Style::default().fg(theme.foreground).bg(theme.background) } else { - Style::default().fg(Color::Black).bg(Color::DarkGray) + Style::default().fg(theme.background).bg(theme.selection) } } else { - Style::default().fg(Color::White).bg(Color::Reset) + Style::default().fg(theme.foreground).bg(theme.background) }; let block = Block::default() .title(title.to_owned()) .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)) + .border_style(Style::default().fg(theme.primary)) .style(style); if is_editing { textarea.set_block(block); textarea.set_style(style); - textarea.set_cursor_style(Style::default().fg(Color::White).bg(ORANGE)); + textarea + .set_cursor_style(Style::default().fg(theme.foreground).bg(theme.accent)); f.render_widget(textarea.widget(), *chunk); } else { let content = textarea.lines().join("\n"); @@ -445,15 +446,15 @@ impl ScrollableTextArea { } } - fn render_full_screen(&mut self, f: &mut Frame, area: Rect) -> Result<()> { + fn render_full_screen(&mut self, f: &mut Frame, area: Rect, theme: &ThemeColors) -> Result<()> { let textarea = &mut self.textareas[self.focused_index]; - textarea.set_selection_style(Style::default().bg(Color::Red)); + textarea.set_selection_style(Style::default().bg(theme.selection)); let title = &self.titles[self.focused_index]; let block = Block::default() .title(title.clone()) .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)); + .border_style(Style::default().fg(theme.primary)); let content = textarea.lines().join("\n"); let rendered_markdown = self.markdown_cache.borrow_mut().get_or_render( diff --git a/src/theme.rs b/src/theme.rs new file mode 100644 index 0000000..9fa0c0b --- /dev/null +++ b/src/theme.rs @@ -0,0 +1,59 @@ +use lazy_static::lazy_static; +use ratatui::style::Color; + +#[derive(Clone)] +pub struct ThemeColors { + pub primary: Color, + pub background: Color, + pub foreground: Color, + pub selection: Color, + pub accent: Color, + pub error: Color, + pub success: Color, + pub border: Color, + pub header_bg: Color, + pub header_fg: Color, +} + +impl PartialEq for ThemeColors { + fn eq(&self, other: &Self) -> bool { + self.primary == other.primary + && self.background == other.background + && self.foreground == other.foreground + && self.selection == other.selection + && self.accent == other.accent + && self.error == other.error + && self.success == other.success + && self.border == other.border + && self.header_bg == other.header_bg + && self.header_fg == other.header_fg + } +} + +lazy_static! { + pub static ref LIGHT_MODE_COLORS: ThemeColors = ThemeColors { + primary: Color::Rgb(25, 118, 210), // Blue + background: Color::Rgb(248, 248, 248), // Light Grey + foreground: Color::Rgb(33, 33, 33), // Dark Grey (near black) + selection: Color::Rgb(207, 232, 252), // Light Blue + accent: Color::Rgb(230, 81, 0), // Orange + error: Color::Rgb(211, 47, 47), // Red + success: Color::Rgb(56, 142, 60), // Green + border: Color::Rgb(189, 189, 189), // Mid Grey + header_bg: Color::Rgb(232, 232, 232), // Lighter Grey + header_fg: Color::Rgb(66, 66, 66), // Dark Grey + }; + + pub static ref DARK_MODE_COLORS: ThemeColors = ThemeColors { + primary: Color::Rgb(255, 165, 0), // Orange (same as original ORANGE constant) + background: Color::Black, // Black background (original) + foreground: Color::White, // White text (original) + selection: Color::DarkGray, // Dark gray for selection (original) + accent: Color::Rgb(255, 165, 0), // Orange (same as original ORANGE constant) + error: Color::Red, // Red for errors (original) + success: Color::Green, // Green for success (original) + border: Color::Rgb(255, 165, 0), // Orange borders (original) + header_bg: Color::Black, // Black header background (original) + header_fg: Color::Rgb(255, 165, 0), // Orange header text (original) + }; +} diff --git a/src/ui.rs b/src/ui.rs index bf5e64f..8ed8928 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,7 +1,7 @@ -use crate::{TitlePopup, TitleSelectPopup, BORDER_PADDING_SIZE, ORANGE}; +use crate::{ThemeColors, TitlePopup, TitleSelectPopup, BORDER_PADDING_SIZE, DARK_MODE_COLORS}; use ratatui::{ layout::{Constraint, Direction, Layout, Rect}, - style::{Color, Modifier, Style}, + style::{Modifier, Style}, text::{Line, Span}, widgets::{Block, Borders, Cell, Paragraph, Row, Table, Tabs}, Frame, @@ -27,14 +27,18 @@ pub struct UiPopup { pub message: String, pub popup_title: String, pub visible: bool, + pub percent_x: u16, + pub percent_y: u16, } impl UiPopup { - pub fn new(popup_tile: String) -> Self { + pub fn new(popup_title: String, percent_x: u16, percent_y: u16) -> Self { UiPopup { message: String::new(), visible: false, - popup_title: popup_tile, + popup_title, + percent_x, + percent_y, } } @@ -50,22 +54,30 @@ impl UiPopup { impl Default for UiPopup { fn default() -> Self { - Self::new("".to_owned()) + Self::new("".to_owned(), 60, 20) } } -pub fn render_edit_commands_popup(f: &mut Frame) { +pub fn render_edit_commands_popup(f: &mut Frame, theme: &ThemeColors) { let area = centered_rect(80, 80, f.size()); f.render_widget(ratatui::widgets::Clear, area); let block = Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)) - .title("Editing Commands"); + .border_style(Style::default().fg(theme.primary)) + .title("Editing Commands - Esc to exit"); let header = Row::new(vec![ - Cell::from("MAPPINGS").style(Style::default().fg(ORANGE).add_modifier(Modifier::BOLD)), - Cell::from("DESCRIPTIONS").style(Style::default().fg(ORANGE).add_modifier(Modifier::BOLD)), + Cell::from("MAPPINGS").style( + Style::default() + .fg(theme.accent) + .add_modifier(Modifier::BOLD), + ), + Cell::from("DESCRIPTIONS").style( + Style::default() + .fg(theme.accent) + .add_modifier(Modifier::BOLD), + ), ]) .height(BORDER_PADDING_SIZE as u16); @@ -97,6 +109,7 @@ pub fn render_edit_commands_popup(f: &mut Frame) { "Ctrl+A, Home, Ctrl+Alt+B, Ctrl+Alt+←", "Move cursor to the head of line", ]), + Row::new(vec!["Ctrl+L", "Toggle between light and dark mode"]), Row::new(vec!["Ctrl+K", "Format markdown block"]), Row::new(vec!["Ctrl+J", "Format JSON"]), ]; @@ -106,16 +119,17 @@ pub fn render_edit_commands_popup(f: &mut Frame) { .block(block) .widths([Constraint::Percentage(30), Constraint::Percentage(70)]) .column_spacing(BORDER_PADDING_SIZE as u16) - .highlight_style(Style::default().fg(Color::Yellow)) + .highlight_style(Style::default().fg(theme.accent)) .highlight_symbol(">> "); f.render_widget(table, area); } -pub fn render_header(f: &mut Frame, area: Rect, is_edit_mode: bool) { +pub fn render_header(f: &mut Frame, area: Rect, is_edit_mode: bool, theme: &ThemeColors) { let available_width = area.width as usize; let normal_commands = vec![ "q:Quit", + "^h:Help", "^n:Add", "^d:Del", "^y:Copy", @@ -125,6 +139,7 @@ pub fn render_header(f: &mut Frame, area: Rect, is_edit_mode: bool) { "Esc:Exit", "^t:Title", "^s:Select", + "^l:Toggle Theme", "^j:Format JSON", "^k:Format Markdown", ]; @@ -138,6 +153,7 @@ pub fn render_header(f: &mut Frame, area: Rect, is_edit_mode: bool) { "^s:Select", "^e:External Editor", "^h:Help", + "^l:Toggle Theme", ]; let commands = if is_edit_mode { &edit_commands @@ -169,34 +185,61 @@ pub fn render_header(f: &mut Frame, area: Rect, is_edit_mode: bool) { let padding = " ".repeat(available_width - command_width - thoth_width - BORDER_PADDING_SIZE); let header = Line::from(vec![ - Span::styled(command_string, Style::default().fg(ORANGE)), - Span::styled(padding, Style::default().fg(ORANGE)), - Span::styled(format!(" {} ", thoth), Style::default().fg(ORANGE)), + Span::styled(command_string, Style::default().fg(theme.accent)), + Span::styled(padding, Style::default().fg(theme.accent)), + Span::styled(format!(" {} ", thoth), Style::default().fg(theme.accent)), ]); let tabs = Tabs::new(vec![header]) - .style(Style::default().bg(Color::Black)) - .divider(Span::styled("|", Style::default().fg(ORANGE))); + .style(Style::default().bg(theme.header_bg)) + .divider(Span::styled("|", Style::default().fg(theme.accent))); f.render_widget(tabs, area); } -pub fn render_title_popup(f: &mut Frame, popup: &TitlePopup) { +pub fn render_help_popup(f: &mut Frame, popup: &UiPopup, theme: ThemeColors) { + if !popup.visible { + return; + } + + let area = centered_rect(80, 80, f.size()); + f.render_widget(ratatui::widgets::Clear, area); + + let border_color = if theme == *DARK_MODE_COLORS { + theme.accent + } else { + theme.primary + }; + + let text = Paragraph::new(popup.message.as_str()) + .style(Style::default().fg(theme.foreground)) + .block( + Block::default() + .borders(Borders::ALL) + .border_style(Style::default().fg(border_color)) + .title(format!("{} - Esc to exit", popup.popup_title)), + ) + .wrap(ratatui::widgets::Wrap { trim: true }); + + f.render_widget(text, area); +} + +pub fn render_title_popup(f: &mut Frame, popup: &TitlePopup, theme: &ThemeColors) { let area = centered_rect(60, 20, f.size()); f.render_widget(ratatui::widgets::Clear, area); let text = Paragraph::new(popup.title.as_str()) - .style(Style::default().bg(Color::Black)) + .style(Style::default().bg(theme.background).fg(theme.foreground)) .block( Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)) + .border_style(Style::default().fg(theme.primary)) .title("Change Title"), ); f.render_widget(text, area); } -pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) { +pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup, theme: &ThemeColors) { let area = centered_rect(80, 80, f.size()); f.render_widget(ratatui::widgets::Clear, area); @@ -224,7 +267,7 @@ pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) { if absolute_idx == popup.selected_index { Line::from(vec![Span::styled( format!("> {}", title_match.title), - Style::default().fg(Color::Yellow), + Style::default().fg(theme.accent), )]) } else { Line::from(vec![Span::raw(format!(" {}", title_match.title))]) @@ -234,7 +277,7 @@ pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) { let block = Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)) + .border_style(Style::default().fg(theme.primary)) .title("Select Title"); let paragraph = Paragraph::new(items) @@ -245,7 +288,7 @@ pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) { let search_block = Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(ORANGE)) + .border_style(Style::default().fg(theme.primary)) .title("Search"); let search_text = Paragraph::new(popup.search_query.as_str()).block(search_block); @@ -253,20 +296,20 @@ pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup) { f.render_widget(search_text, search_box); } -pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup) { +pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup, theme: &ThemeColors) { if !popup.visible { return; } - let area = centered_rect(60, 20, f.size()); + let area = centered_rect(popup.percent_x, popup.percent_y, f.size()); f.render_widget(ratatui::widgets::Clear, area); let text = Paragraph::new(popup.message.as_str()) - .style(Style::default().fg(Color::Red)) + .style(Style::default().fg(theme.error)) .block( Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(Color::Red)) + .border_style(Style::default().fg(theme.error)) .title(format!("{} - Esc to exit", popup.popup_title)), ) .wrap(ratatui::widgets::Wrap { trim: true }); // Enable text wrapping @@ -304,6 +347,9 @@ pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { mod tests { use ratatui::{backend::TestBackend, Terminal}; + use crate::DARK_MODE_COLORS; + use crate::ORANGE; + use super::*; #[test] @@ -324,7 +370,7 @@ mod tests { terminal .draw(|f| { let area = f.size(); - render_header(f, area, false); + render_header(f, area, false, &DARK_MODE_COLORS); }) .unwrap(); @@ -361,7 +407,7 @@ mod tests { terminal .draw(|f| { - render_title_popup(f, &popup); + render_title_popup(f, &popup, &DARK_MODE_COLORS); }) .unwrap(); @@ -410,7 +456,7 @@ mod tests { terminal .draw(|f| { - render_title_select_popup(f, &popup); + render_title_select_popup(f, &popup, &DARK_MODE_COLORS); }) .unwrap(); @@ -438,7 +484,7 @@ mod tests { terminal .draw(|f| { - render_edit_commands_popup(f); + render_edit_commands_popup(f, &DARK_MODE_COLORS); }) .unwrap(); diff --git a/src/ui_handler.rs b/src/ui_handler.rs index 7e65266..75283d1 100644 --- a/src/ui_handler.rs +++ b/src/ui_handler.rs @@ -1,4 +1,4 @@ -use crate::{get_save_backup_file_path, EditorClipboard}; +use crate::{get_save_backup_file_path, EditorClipboard, ThemeMode, ThothConfig}; use anyhow::{bail, Result}; use crossterm::{ event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode, KeyEventKind, KeyModifiers}, @@ -31,10 +31,12 @@ pub struct UIState { pub title_popup: TitlePopup, pub title_select_popup: TitleSelectPopup, pub error_popup: UiPopup, + pub help_popup: UiPopup, pub copy_popup: UiPopup, pub edit_commands_popup: EditCommandsPopup, pub clipboard: Option, pub last_draw: Instant, + pub config: ThothConfig, } impl UIState { @@ -51,15 +53,19 @@ impl UIState { } scrollable_textarea.initialize_scroll(); + let config = ThothConfig::load()?; + Ok(UIState { scrollable_textarea, title_popup: TitlePopup::new(), title_select_popup: TitleSelectPopup::new(), - error_popup: UiPopup::new("Error".to_string()), - copy_popup: UiPopup::new("Block Copied".to_string()), + error_popup: UiPopup::new("Error".to_string(), 60, 20), + copy_popup: UiPopup::new("Block Copied".to_string(), 60, 20), + help_popup: UiPopup::new("Keyboard Shortcuts".to_string(), 60, 80), edit_commands_popup: EditCommandsPopup::new(), clipboard: EditorClipboard::try_new(), last_draw: Instant::now(), + config, }) } } @@ -69,6 +75,8 @@ pub fn draw_ui( state: &mut UIState, ) -> Result<()> { terminal.draw(|f| { + let theme = state.config.get_theme_colors(); + let chunks = ratatui::layout::Layout::default() .direction(ratatui::layout::Direction::Vertical) .constraints( @@ -80,29 +88,42 @@ pub fn draw_ui( ) .split(f.size()); - render_header(f, chunks[0], state.scrollable_textarea.edit_mode); + render_header(f, chunks[0], state.scrollable_textarea.edit_mode, theme); if state.scrollable_textarea.full_screen_mode { - state.scrollable_textarea.render(f, f.size()).unwrap(); + state + .scrollable_textarea + .render(f, f.size(), theme) + .unwrap(); } else { - state.scrollable_textarea.render(f, chunks[1]).unwrap(); + state + .scrollable_textarea + .render(f, chunks[1], theme) + .unwrap(); } if state.title_popup.visible { - render_title_popup(f, &state.title_popup); + render_title_popup(f, &state.title_popup, theme); } else if state.title_select_popup.visible { - render_title_select_popup(f, &state.title_select_popup); + render_title_select_popup(f, &state.title_select_popup, theme); } if state.edit_commands_popup.visible { - render_edit_commands_popup(f); + render_edit_commands_popup(f, theme); } if state.error_popup.visible { - render_ui_popup(f, &state.error_popup); + render_ui_popup(f, &state.error_popup, theme); + } + if state.help_popup.visible { + render_ui_popup(f, &state.help_popup, theme); } if state.copy_popup.visible { - render_ui_popup(f, &state.copy_popup); + render_ui_popup(f, &state.copy_popup, theme); + } + + if state.help_popup.visible { + render_ui_popup(f, &state.help_popup, theme); } })?; Ok(()) @@ -240,11 +261,6 @@ fn handle_title_popup_input(state: &mut UIState, key: event::KeyEvent) -> Result } fn handle_title_select_popup_input(state: &mut UIState, key: event::KeyEvent) -> Result { - // Subtract 2 from viewport height to account for the top and bottom borders - // drawn by Block::default().borders(Borders::ALL) in ui.rs render_title_select_popup. - // The borders are rendered using unicode box-drawing characters: - // top border : ┌───┐ - // bottom border : └───┘ let visible_items = (state.scrollable_textarea.viewport_height as f32 * 0.8).floor() as usize - 10; @@ -326,6 +342,13 @@ fn handle_normal_input( } } } + KeyCode::Char('l') if key.modifiers.contains(KeyModifiers::CONTROL) => { + let new_theme = match state.config.theme { + ThemeMode::Light => ThemeMode::Dark, + ThemeMode::Dark => ThemeMode::Light, + }; + state.config.set_theme(new_theme.clone())?; + } KeyCode::Char('y') if key.modifiers.contains(KeyModifiers::CONTROL) => { match state.scrollable_textarea.copy_focused_textarea_contents() { Ok(_) => { @@ -361,6 +384,37 @@ fn handle_normal_input( KeyCode::Char('v') if key.modifiers.contains(KeyModifiers::CONTROL) => { handle_paste(state)?; } + KeyCode::Char('h') if key.modifiers.contains(KeyModifiers::CONTROL) => { + let help_message = "\ +NAVIGATION: + • ↑/↓ or j/k: Navigate between blocks + • Enter: Enter edit mode + • Esc: Exit current mode + +BLOCKS: + • ^n: Add a new block + • ^d: Delete current block + • ^t: Change block title + • ^s: Select block by title + • ^f: Toggle fullscreen mode + +CLIPBOARD: + • ^y: Copy current block + • ^v: Paste from clipboard + • ^b: Copy selection (in edit mode) + +FORMATTING: + • ^j: Format as JSON + • ^k: Format as Markdown + +OTHER: + • ^l: Toggle light/dark theme + • ^e: Edit with external editor (in edit mode) + • q: Quit application + • ^h: Show this help"; + + state.help_popup.show(help_message.to_string()); + } KeyCode::Char('f') if key.modifiers.contains(KeyModifiers::CONTROL) => { if !state.scrollable_textarea.edit_mode { state.scrollable_textarea.toggle_full_screen(); @@ -446,6 +500,9 @@ fn handle_normal_input( if state.error_popup.visible { state.error_popup.hide(); } + if state.help_popup.visible { + state.help_popup.hide(); + } if state.copy_popup.visible { state.copy_popup.hide(); } From e75f2f50e7f7780e9d55911f4e73780e860cf4d0 Mon Sep 17 00:00:00 2001 From: jooaf Date: Thu, 10 Apr 2025 22:29:42 -0500 Subject: [PATCH 2/3] updating readme --- README.md | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 09d1f70..6bb8b76 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,25 @@ The binary lives in `thoth/target/release/thoth`. You can add the binary to your ## Usage This will show how to use the scratchpad via the CLI or the TUI. +### Theme +You can set the theme via the CLI or in the TUI. +```bash +# Set to light mode +thoth theme light + +# Set to dark mode +thoth theme dark + +# Check current theme +thoth get_theme +```` +#### UI Shortcuts +Users can toggle between light and dark modes while using the application: +- Ctrl+L: Toggle between light and dark modes + +#### Persistent Configuration +Theme preferences are stored in ~/.config/thoth/config.toml and persist between TUI sessions. + ### TUI To start the TUI, simply type `thoth`. Since it is a persistent buffer, thoth will save when you hit quit using `q`. @@ -172,14 +191,16 @@ A terminal scratchpad akin to Heynote Usage: thoth [COMMAND] Commands: - add Add a new block to the scratchpad - list List all of the blocks within your thoth scratchpad - load_backup Load backup file as the main thoth markdown file - read_clipboard Read the contents of the clipboard backup file - delete Delete a block by name - view View (STDOUT) the contents of the block by name - copy Copy the contents of a block to the system clipboard - help Print this message or the help of the given subcommand(s) + add Add a new block to the scratchpad + list List all of the blocks within your thoth scratchpad + load_backup Load backup file as the main thoth markdown file + read_clipboard Read the contents of the clipboard backup file + delete Delete a block by name + view View (STDOUT) the contents of the block by name + copy Copy the contents of a block to the system clipboard + theme Set the theme to light or dark mode + get_theme Get the current theme + help Print this message or the help of the given subcommand(s) Options: -h, --help Print help @@ -216,6 +237,6 @@ This ensures your content is always accessible, even when the system clipboard i Contributions are always welcomed :) !!! Please take a look at this [doc](https://github.com/jooaf/thoth/blob/main/CONTRIBUTING.md) for more information. ## TODO -- Inlcude light mode +- Inlcude light mode: Done! - Automatically saving backup `thoth_notes.md` files: Done! - Add fuzzy finder for selecting blocks in TUI: Done! From 259d572e63d9d46f6de48e5a9634025941852559 Mon Sep 17 00:00:00 2001 From: jooaf Date: Thu, 10 Apr 2025 22:32:14 -0500 Subject: [PATCH 3/3] linting --- src/config.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 36f7cb8..b0fcd9a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,18 +8,13 @@ use crate::ThemeColors; use crate::DARK_MODE_COLORS; use crate::LIGHT_MODE_COLORS; -#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)] pub enum ThemeMode { Light, + #[default] Dark, } -impl Default for ThemeMode { - fn default() -> Self { - ThemeMode::Dark - } -} - #[derive(Debug, Serialize, Deserialize, Default)] pub struct ThothConfig { pub theme: ThemeMode,