diff --git a/src/config.rs b/src/config.rs index 1ae2b2e..a624fc1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use crossterm::{ event::{KeyCode, KeyModifiers}, style::{Attribute, Attributes, Color, ContentStyle}, }; -use promkit::style::StyleBuilder; +use promkit::{style::StyleBuilder, text_editor::Mode}; use serde::{Deserialize, Serialize}; use tokio::time::Duration; @@ -14,11 +14,15 @@ mod duration; use duration::duration_serde; pub mod event; use event::{EventDefSet, KeyEventDef}; +mod text_editor; +use text_editor::text_editor_mode_serde; #[derive(Serialize, Deserialize)] pub(crate) struct EditorConfig { pub theme_on_focus: EditorTheme, pub theme_on_defocus: EditorTheme, + #[serde(with = "text_editor_mode_serde")] + pub mode: Mode, pub word_break_chars: HashSet, } @@ -58,6 +62,7 @@ impl Default for EditorConfig { .attrs(Attributes::from(Attribute::Dim)) .build(), }, + mode: Mode::Insert, word_break_chars: HashSet::from(['.', '|', '(', ')', '[', ']']), } } diff --git a/src/config/text_editor.rs b/src/config/text_editor.rs new file mode 100644 index 0000000..318e372 --- /dev/null +++ b/src/config/text_editor.rs @@ -0,0 +1,34 @@ +use promkit::text_editor::Mode; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +pub mod text_editor_mode_serde { + use super::*; + + pub fn serialize(mode: &Mode, serializer: S) -> Result + where + S: Serializer, + { + let mode_str = match mode { + Mode::Insert => "Insert", + Mode::Overwrite => "Overwrite", + // Add other variants if they exist + }; + mode_str.serialize(serializer) + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let mode_str = String::deserialize(deserializer)?; + match mode_str.as_str() { + "Insert" => Ok(Mode::Insert), + "Overwrite" => Ok(Mode::Overwrite), + // Add other variants if they exist + _ => Err(serde::de::Error::custom(format!( + "Unknown Mode variant: {}", + mode_str + ))), + } + } +} diff --git a/src/main.rs b/src/main.rs index ba2242c..7e6a909 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,21 +60,6 @@ pub struct Args { /// reads from standard input. pub input: Option, - #[arg( - short = 'e', - long = "edit-mode", - default_value = "insert", - value_parser = edit_mode_validator, - help = "Edit mode for the interface ('insert' or 'overwrite').", - long_help = r#" - Specifies the edit mode for the interface. - Acceptable values are "insert" or "overwrite". - - "insert" inserts a new input at the cursor's position. - - "overwrite" mode replaces existing characters with new input at the cursor's position. - "#, - )] - pub edit_mode: text_editor::Mode, - #[arg( short = 'n', long = "no-hint", @@ -108,14 +93,6 @@ pub struct Args { pub max_streams: Option, } -fn edit_mode_validator(val: &str) -> anyhow::Result { - match val { - "insert" | "" => Ok(text_editor::Mode::Insert), - "overwrite" => Ok(text_editor::Mode::Overwrite), - _ => Err(anyhow!("edit-mode must be 'insert' or 'overwrite'")), - } -} - /// Parses the input based on the provided arguments. /// /// This function reads input data from either a specified file or standard input. @@ -221,7 +198,7 @@ async fn main() -> anyhow::Result<()> { prefix_style: config.editor.theme_on_focus.prefix_style, active_char_style: config.editor.theme_on_focus.active_char_style, inactive_char_style: config.editor.theme_on_focus.inactive_char_style, - edit_mode: args.edit_mode, + edit_mode: config.editor.mode, word_break_chars: config.editor.word_break_chars, lines: Default::default(), };