diff --git a/src/chat/commands/save_files.rs b/src/chat/commands/save_files.rs index 4596e3b..46f3144 100644 --- a/src/chat/commands/save_files.rs +++ b/src/chat/commands/save_files.rs @@ -5,7 +5,7 @@ use crate::chat::{ interface::{ChatBackend, ChatStorage, MessageRole}, service::ChatService, }; -use crate::cli::utils::get_user_input; +use crate::cli::editor::get_user_input; use crate::openai_api::openai_interface::OpenAIInterface; use regex::Regex; use std::error::Error; diff --git a/src/cli/chat/run.rs b/src/cli/chat/run.rs index 16c38db..00c7a08 100644 --- a/src/cli/chat/run.rs +++ b/src/cli/chat/run.rs @@ -4,9 +4,9 @@ use crate::chat::file_storage::DirectoryChatStorage; use crate::chat::interface::{ChatStorage, MessageRole}; use crate::chat::service::ChatService; use crate::cli::chat::ChatArgs; +use crate::cli::editor::{get_multiline_input, get_user_input}; use crate::cli::spinner::{start_spinner, stop_spinner}; use crate::cli::style::configure_mad_skin; -use crate::cli::utils::{get_multiline_input, get_user_input}; use crate::config; use crate::config::get_chat_sessions_dir; use crate::openai_api::openai_interface::OpenAIInterface; diff --git a/src/cli/createbackground/run.rs b/src/cli/createbackground/run.rs index d591057..c44057c 100644 --- a/src/cli/createbackground/run.rs +++ b/src/cli/createbackground/run.rs @@ -1,6 +1,6 @@ use crate::cli::createbackground::Orientation; +use crate::cli::editor::get_multiline_input; use crate::cli::spinner::{start_spinner, stop_spinner}; -use crate::cli::utils::get_multiline_input; use async_openai::config::OpenAIConfig; use async_openai::types::{ CreateImageRequestArgs, Image, ImageModel, ImageQuality, ImageResponseFormat, ImageSize, diff --git a/src/cli/createicon/run.rs b/src/cli/createicon/run.rs index 8dfa02f..3995787 100644 --- a/src/cli/createicon/run.rs +++ b/src/cli/createicon/run.rs @@ -1,5 +1,5 @@ +use crate::cli::editor::get_multiline_input; use crate::cli::spinner::{start_spinner, stop_spinner}; -use crate::cli::utils::get_multiline_input; use async_openai::config::OpenAIConfig; use async_openai::types::{ CreateImageRequestArgs, Image, ImageModel, ImageQuality, ImageResponseFormat, ImageSize, diff --git a/src/cli/editor/mod.rs b/src/cli/editor/mod.rs new file mode 100644 index 0000000..2ce4099 --- /dev/null +++ b/src/cli/editor/mod.rs @@ -0,0 +1,6 @@ +mod multiline_editor; +mod user_input_editor; + +pub use multiline_editor::get_multiline_input; + +pub use user_input_editor::get_user_input; diff --git a/src/cli/utils.rs b/src/cli/editor/multiline_editor.rs similarity index 58% rename from src/cli/utils.rs rename to src/cli/editor/multiline_editor.rs index 316e5c0..2a2532e 100644 --- a/src/cli/utils.rs +++ b/src/cli/editor/multiline_editor.rs @@ -1,12 +1,13 @@ use crate::cli::slash_completer::SlashCommandCompleter; use crate::cli::style::configure_mad_skin; -use rustyline::error::ReadlineError; -use rustyline::highlight::{Highlighter, MatchingBracketHighlighter}; -use rustyline::hint::HistoryHinter; -use rustyline::history::DefaultHistory; -use rustyline::validate::MatchingBracketValidator; -use rustyline::EditMode::Emacs; -use rustyline::{Completer, Config, DefaultEditor, Editor, Helper, Hinter, Validator}; +use rustyline::{ + error::ReadlineError, + highlight::{Highlighter, MatchingBracketHighlighter}, + hint::HistoryHinter, + history::DefaultHistory, + validate::MatchingBracketValidator, + Completer, Editor, Helper, Hinter, Validator, +}; use std::borrow::Cow; use std::borrow::Cow::{Borrowed, Owned}; use std::error::Error; @@ -49,7 +50,6 @@ impl Highlighter for MyHelper { } } -// Function to capture user input using rustyline with multiline support. pub fn get_multiline_input( prompt: &str, slash_completions: Vec, @@ -64,11 +64,9 @@ pub fn get_multiline_input( validator: MatchingBracketValidator::new(), }; rl.set_helper(Some(h)); - let mut buffer = String::new(); - // Create a MadSkin for styling the prompt - let skin = configure_mad_skin(); - // Use termimad to print a horizontal line and a colored prompt + let mut buffer: String = String::new(); + let skin = configure_mad_skin(); // Configure for styling the prompt skin.print_text("---\n"); skin.print_text(&format!("**{}**", prompt)); // Make the prompt bold and colored @@ -76,42 +74,15 @@ pub fn get_multiline_input( match rl.readline("") { Ok(line) => { if line.trim_start().starts_with('/') { - // Exit the loop if a slash command is entered buffer.push_str(&line); // Keep the command in the buffer if needed break; } buffer.push_str(&line); buffer.push('\n'); } - Err(ReadlineError::Eof) => { - break; - } + Err(ReadlineError::Eof) => break, Err(err) => return Err(Box::new(err)), } } Ok(buffer) } -pub fn get_user_input(prompt: &str) -> Result> { - let config = Config::builder().edit_mode(Emacs).build(); - let mut rl = DefaultEditor::with_config(config)?; - - // Print a styled prompt - let skin = configure_mad_skin(); - skin.print_text("---\n"); - skin.print_text(&format!("**{}**", prompt)); // Make the prompt bold and colored - - // Read a single line of input - match rl.readline("") { - Ok(input) => { - // Valid input or cancel action if empty - Ok(input.trim().to_string()) - } - Err(ReadlineError::Interrupted) => { - Ok(String::new()) // Return empty, indicating cancel/use default - } - Err(ReadlineError::Eof) => { - Ok(String::new()) // Return empty for end-of-file signal - } - Err(err) => Err(Box::new(err)), - } -} diff --git a/src/cli/editor/user_input_editor.rs b/src/cli/editor/user_input_editor.rs new file mode 100644 index 0000000..9648b87 --- /dev/null +++ b/src/cli/editor/user_input_editor.rs @@ -0,0 +1,20 @@ +use crate::cli::style::configure_mad_skin; +use rustyline::{error::ReadlineError, Config, DefaultEditor, EditMode::Emacs}; + +pub fn get_user_input(prompt: &str) -> Result> { + let config = Config::builder().edit_mode(Emacs).build(); + let mut rl = DefaultEditor::with_config(config)?; + + // Print a styled prompt + let skin = configure_mad_skin(); + skin.print_text("---\n"); + skin.print_text(&format!("**{}**", prompt)); // Make the prompt bold and colored + + // Read a single line of input + match rl.readline("") { + Ok(input) => Ok(input.trim().to_string()), + Err(ReadlineError::Interrupted) => Ok(String::new()), // Return empty string as cancelation + Err(ReadlineError::Eof) => Ok(String::new()), + Err(err) => Err(Box::new(err)), + } +} diff --git a/src/cli/mod.rs b/src/cli/mod.rs index f93b32c..a475223 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -2,9 +2,9 @@ pub mod chat; pub mod commitmessage; pub mod createbackground; pub mod createicon; +pub mod editor; pub mod init; mod slash_completer; mod spinner; mod style; -pub mod utils; pub mod wish; diff --git a/src/cli/wish/run.rs b/src/cli/wish/run.rs index dc274fb..0803e56 100644 --- a/src/cli/wish/run.rs +++ b/src/cli/wish/run.rs @@ -1,6 +1,6 @@ use crate::chat::file_storage::NilChatStorage; use crate::chat::service::ChatService; -use crate::cli::utils::get_multiline_input; +use crate::cli::editor::get_multiline_input; use crate::config; use crate::openai_api::openai_interface::OpenAIInterface; use crate::persona::get_persona;