diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 39 | ||||
| -rw-r--r-- | src/utils/mod.rs | 11 |
2 files changed, 31 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs index 45d2430..19e6e1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,14 +23,12 @@ use crate::config::{ use crate::function::{eval_tool_calls, need_send_tool_results}; use crate::render::render_error; use crate::repl::Repl; -use crate::utils::{ - create_abort_signal, create_spinner, extract_block, get_env_name, run_command, - run_with_spinner, AbortSignal, Shell, CODE_BLOCK_RE, IS_STDOUT_TERMINAL, SHELL, -}; +use crate::utils::*; use anyhow::{bail, Result}; use clap::Parser; -use inquire::{Select, Text}; +use inquire::validator::Validation; +use inquire::Text; use is_terminal::IsTerminal; use parking_lot::RwLock; use simplelog::{format_description, ConfigBuilder, LevelFilter, SimpleLogger, WriteLogger}; @@ -241,28 +239,41 @@ async fn shell_execute(config: &GlobalConfig, shell: &Shell, mut input: Input) - return Ok(()); } if *IS_STDOUT_TERMINAL { + let options = ["execute", "revise", "describe", "cancel"]; + let command = color_text(eval_str.trim(), nu_ansi_term::Color::Rgb(255, 165, 0)); + let first_letter_color = nu_ansi_term::Color::Cyan; + let prompt_text = options + .iter() + .map(|v| format!("{}{}", color_text(&v[0..1], first_letter_color), &v[1..])) + .collect::<Vec<String>>() + .join(&dimmed_text(" | ")); loop { - let answer = Select::new( - eval_str.trim(), - vec!["✅ Execute", "🔄 Revise", "📖 Explain", "❌ Cancel"], - ) - .prompt()?; + println!("{command}"); + let answer = Text::new(&format!("{prompt_text}:")) + .with_default("e") + .with_validator(|input: &str| match matches!(input, "e" | "r" | "d" | "c") { + true => Ok(Validation::Valid), + false => Ok(Validation::Invalid( + "Invalid option, choice one of e, r, d or c".into(), + )), + }) + .prompt()?; - match answer { - "✅ Execute" => { + match answer.as_str() { + "e" => { debug!("{} {:?}", shell.cmd, &[&shell.arg, &eval_str]); let code = run_command(&shell.cmd, &[&shell.arg, &eval_str], None)?; if code != 0 { process::exit(code); } } - "🔄 Revise" => { + "r" => { let revision = Text::new("Enter your revision:").prompt()?; let text = format!("{}\n{revision}", input.text()); input.set_text(text); return shell_execute(config, shell, input).await; } - "📖 Explain" => { + "d" => { let role = config.read().retrieve_role(EXPLAIN_SHELL_ROLE)?; let input = Input::from_str(config, &eval_str, Some(role)); let abort = create_abort_signal(); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index e937e6b..81ae0ff 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -137,15 +137,16 @@ pub fn pretty_error(err: &anyhow::Error) -> String { } pub fn error_text(input: &str) -> String { - nu_ansi_term::Style::new() - .fg(nu_ansi_term::Color::Red) - .paint(input) - .to_string() + color_text(input, nu_ansi_term::Color::Red) } pub fn warning_text(input: &str) -> String { + color_text(input, nu_ansi_term::Color::Yellow) +} + +pub fn color_text(input: &str, color: nu_ansi_term::Color) -> String { nu_ansi_term::Style::new() - .fg(nu_ansi_term::Color::Yellow) + .fg(color) .paint(input) .to_string() } |
