Skip to content

Commit

Permalink
Refactor CLI editors into separate modules
Browse files Browse the repository at this point in the history
- Moved multiline input functionality to 'multiline_editor.rs' for improved modularity.
- Separated user input handling into 'user_input_editor.rs'.
- Cleaned up 'utils.rs' by removing editor-specific logic and redirecting imports.
- Created an 'editor' directory to encapsulate all editor-related components.

This refactor enhances code organization, making the codebase easier to maintain and extend, and
prepares for implementing advanced editor features.
  • Loading branch information
Christian Stolz committed Oct 4, 2024
1 parent 1edc31b commit ebfc303
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/chat/commands/save_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/chat/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/createbackground/run.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/createicon/run.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/cli/editor/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
51 changes: 11 additions & 40 deletions src/cli/utils.rs → src/cli/editor/multiline_editor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String>,
Expand All @@ -64,54 +64,25 @@ 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

loop {
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<String, Box<dyn std::error::Error>> {
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)),
}
}
20 changes: 20 additions & 0 deletions src/cli/editor/user_input_editor.rs
Original file line number Diff line number Diff line change
@@ -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<String, Box<dyn std::error::Error>> {
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)),
}
}
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/cli/wish/run.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit ebfc303

Please sign in to comment.