Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion crates/goose-cli/src/session/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ pub struct PlanCommandOptions {
pub message_text: String,
}

struct CtrlCHandler;

impl rustyline::ConditionalEventHandler for CtrlCHandler {
/// Handle Ctrl+C to clear the line if text is entered, otherwise exit the session.
fn handle(
&self,
_event: &rustyline::Event,
_n: usize,
_positive: bool,
ctx: &rustyline::EventContext,
) -> Option<rustyline::Cmd> {
if !ctx.line().is_empty() {
Some(rustyline::Cmd::Kill(rustyline::Movement::WholeBuffer))
} else {
Some(rustyline::Cmd::Interrupt)
}
}
}

pub fn get_input(
editor: &mut Editor<GooseCompleter, rustyline::history::DefaultHistory>,
) -> Result<InputResult> {
Expand All @@ -44,7 +63,13 @@ pub fn get_input(
rustyline::EventHandler::Simple(rustyline::Cmd::Newline),
);

editor.bind_sequence(
rustyline::KeyEvent(rustyline::KeyCode::Char('c'), rustyline::Modifiers::CTRL),
rustyline::EventHandler::Conditional(Box::new(CtrlCHandler)),
);

let prompt = format!("{} ", console::style("( O)>").cyan().bold());

let input = match editor.readline(&prompt) {
Ok(text) => text,
Err(e) => match e {
Expand Down Expand Up @@ -270,7 +295,7 @@ fn print_help() {
/clear - Clears the current chat history

Navigation:
Ctrl+C - Interrupt goose (resets the interaction to before the interrupted user request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it still interrupt when running with this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, like it says, it only interrupt it if the text buffer is empty. the sequence should be:

  • if goose is running, ctl-c stops it
  • if you have something in the buffer, ctrl-c clears it
  • if nothing, exit goose

Ctrl+C - Clear current line if text is entered, otherwise exit the session
Ctrl+J - Add a newline
Up/Down arrows - Navigate through command history"
);
Expand Down
Loading