Skip to content

Commit

Permalink
Refactor input handling to support one-line input and improve user pr…
Browse files Browse the repository at this point in the history
…ompt styling

- Replaced `get_user_input` with `get_multiline_input` for better one-line support.
- Modified the user prompt styling with simplified line breaks for consistency in terminal output across different parts of the application.
- Minor cleanup of imports and redundant whitespace for improved code readability and organization.
  • Loading branch information
Christian Stolz committed Sep 28, 2024
1 parent 8f2ab58 commit 6a5bd6c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/cli/chat/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::chat::interface::ChatStorage;
use crate::chat::service::ChatService;
use crate::cli::spinner::{start_spinner, stop_spinner};
use crate::cli::style::configure_mad_skin;
use crate::cli::utils::{get_user_input, load_files_into_context};
use crate::cli::utils::{get_multiline_input, get_user_input, load_files_into_context};
use crate::config;
use crate::config::get_chat_sessions_dir;
use crate::openai_api::openai_interface::OpenAIInterface;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub async fn run_chat(
let skin = configure_mad_skin();

loop {
let user_input = get_user_input("User (use Ctrl+D to submit): ")?;
let user_input = get_multiline_input("User (use Ctrl+D to submit): ")?;
let trimmed_input = user_input.trim();

if trimmed_input == "exit" || trimmed_input.is_empty() {
Expand All @@ -92,7 +92,7 @@ pub async fn run_chat(
stop_spinner(spinner);

// Use the skin to print the AI's response with styling
skin.print_text("-----\nAI:");
skin.print_text("---\n# AI:");
skin.print_text(&response);
chat_service.print_statistics();
}
Expand Down
1 change: 0 additions & 1 deletion src/cli/commitmessage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub mod commitmessage;

pub use cm_args::CommitMessageArgs;


pub async fn run(_args: CommitMessageArgs) -> Result<(), Box<dyn std::error::Error>> {
commitmessage::run_commitmessage().await
}
4 changes: 2 additions & 2 deletions src/cli/createicon/createicon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cli::spinner::{start_spinner, stop_spinner};
use crate::cli::utils::get_user_input;
use crate::cli::utils::get_multiline_input;
use async_openai::config::OpenAIConfig;
use async_openai::types::{CreateImageRequestArgs, Image, ImageResponseFormat, ImageSize};
use async_openai::Client;
Expand All @@ -17,7 +17,7 @@ pub async fn run_createicon(output_dir: &str, sizes: Vec<u32>) -> Result<(), Box

// Get user's description
let prompt_message = "Please describe the icon you wish to create. Type 'Ctrl+D' on a new line when you're finished:";
let description = get_user_input(prompt_message)?;
let description = get_multiline_input(prompt_message)?;

if description.trim().is_empty() {
println!("No description provided. Exiting.");
Expand Down
30 changes: 26 additions & 4 deletions src/cli/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use rustyline::{Config, DefaultEditor};
use crate::cli::style::configure_mad_skin;

// Function to capture user input using rustyline with multiline support.
pub fn get_user_input(prompt: &str) -> Result<String, Box<dyn Error>> {
pub fn get_multiline_input(prompt: &str) -> Result<String, Box<dyn Error>> {
let config = Config::builder().edit_mode(Emacs).build();
let mut rl = DefaultEditor::with_config(config)?;
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
skin.print_text("-------------------------------\n");
skin.print_text("---\n");
skin.print_text(&format!("**{}**", prompt)); // Make the prompt bold and colored

loop {
Expand All @@ -33,10 +33,32 @@ pub fn get_user_input(prompt: &str) -> Result<String, Box<dyn Error>> {
Err(err) => return Err(Box::new(err)),
}
}
Ok(buffer)
}

// Add another separation after input is complete
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)?;

Ok(buffer)
// 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)),
}
}

pub fn load_files_into_context(
Expand Down
2 changes: 0 additions & 2 deletions src/cli/wish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ mod wish_args;

pub use wish_args::WishArgs;


pub async fn run(args: WishArgs) -> Result<(), Box<dyn std::error::Error>> {
wish::run_wish(args.directory.as_str(), args.tools).await
}

4 changes: 2 additions & 2 deletions src/cli/wish/wish.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::{add_to_context, get_user_input, load_files_into_context};
use crate::cli::utils::{add_to_context, get_multiline_input, load_files_into_context};
use crate::openai_api::openai_interface::OpenAIInterface;
use std::error::Error;
use std::path::PathBuf;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub async fn run_wish(directory: &str, use_tools: bool) -> Result<(), Box<dyn Er
chat_service.add_system_message(&full_context);

// Get user input for their wish
let user_input = get_user_input("What do you wish? ")
let user_input = get_multiline_input("What do you wish? ")
.map_err(|e| format!("Failed to read user input: {}", e))?;
let wish = format!("Users wish: {}", user_input);

Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ mod config;
mod openai_api;
mod persona;

use clap::{Command, Parser, CommandFactory};
use clap::{Command, CommandFactory, Parser};
use clap_complete::{generate, Generator};
use std::io;
use dotenvy::dotenv;
use std::io;

#[tokio::main]
async fn main() {
Expand Down

0 comments on commit 6a5bd6c

Please sign in to comment.