Adds command history to rustyline cli (#461)

This commit is contained in:
Jarrod Sibbison
2024-12-13 13:22:03 +11:00
committed by GitHub
parent 8d2a888894
commit 0c6ffa4cbe
+14 -3
View File
@@ -9,6 +9,7 @@ use super::{
use anyhow::Result;
use cliclack::spinner;
use goose::models::message::Message;
use rustyline::DefaultEditor;
const PROMPT: &str = "\x1b[1m\x1b[38;5;30m( O)> \x1b[0m";
@@ -16,6 +17,7 @@ pub struct RustylinePrompt {
spinner: cliclack::ProgressBar,
theme: Theme,
renderers: HashMap<String, Box<dyn ToolRenderer>>,
editor: DefaultEditor,
}
impl RustylinePrompt {
@@ -29,6 +31,8 @@ impl RustylinePrompt {
Box::new(bash_dev_system_renderer),
);
let editor = DefaultEditor::new().expect("Failed to create editor");
RustylinePrompt {
spinner: spinner(),
theme: std::env::var("GOOSE_CLI_THEME")
@@ -42,6 +46,7 @@ impl RustylinePrompt {
})
.unwrap_or(Theme::Dark),
renderers,
editor,
}
}
}
@@ -62,10 +67,15 @@ impl Prompt for RustylinePrompt {
}
fn get_input(&mut self) -> Result<Input> {
let mut editor = rustyline::DefaultEditor::new()?;
let input = editor.readline(PROMPT);
let input = self.editor.readline(PROMPT);
let mut message_text = match input {
Ok(text) => text,
Ok(text) => {
// Add valid input to history
if let Err(e) = self.editor.add_history_entry(text.as_str()) {
eprintln!("Failed to add to history: {}", e);
}
text
}
Err(e) => {
match e {
rustyline::error::ReadlineError::Interrupted => (),
@@ -108,6 +118,7 @@ impl Prompt for RustylinePrompt {
println!("/t - Toggle Light/Dark theme");
println!("/? | /help - Display this help message");
println!("Ctrl+C - Interrupt goose (resets the interaction to before the interrupted user request)");
println!("Use Up/Down arrow keys to navigate through command history");
return Ok(Input {
input_type: InputType::AskAgain,
content: None,