chore: edit_mode into Config

This commit is contained in:
ynqa
2025-03-25 02:05:06 +09:00
parent a6b6f00c3d
commit 5508a55252
3 changed files with 41 additions and 25 deletions

View File

@@ -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<char>,
}
@@ -58,6 +62,7 @@ impl Default for EditorConfig {
.attrs(Attributes::from(Attribute::Dim))
.build(),
},
mode: Mode::Insert,
word_break_chars: HashSet::from(['.', '|', '(', ')', '[', ']']),
}
}

34
src/config/text_editor.rs Normal file
View File

@@ -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<S>(mode: &Mode, serializer: S) -> Result<S::Ok, S::Error>
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<Mode, D::Error>
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
))),
}
}
}

View File

@@ -60,21 +60,6 @@ pub struct Args {
/// reads from standard input.
pub input: Option<PathBuf>,
#[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<usize>,
}
fn edit_mode_validator(val: &str) -> anyhow::Result<text_editor::Mode> {
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(),
};