feat: add support for commands in the Rust TUI (#935)

Introduces support for slash commands like in the TypeScript CLI. We do
not support the full set of commands yet, but the core abstraction is
there now.

In particular, we have a `SlashCommand` enum and due to thoughtful use
of the [strum](https://crates.io/crates/strum) crate, it requires
minimal boilerplate to add a new command to the list.

The key new piece of UI is `CommandPopup`, though the keyboard events
are still handled by `ChatComposer`. The behavior is roughly as follows:

* if the first character in the composer is `/`, the command popup is
displayed (if you really want to send a message to Codex that starts
with a `/`, simply put a space before the `/`)
* while the popup is displayed, up/down can be used to change the
selection of the popup
* if there is a selection, hitting tab completes the command, but does
not send it
* if there is a selection, hitting enter sends the command
* if the prefix of the composer matches a command, the command will be
visible in the popup so the user can see the description (commands could
take arguments, so additional text may appear after the command name
itself)


https://github.com/user-attachments/assets/39c3e6ee-eeb7-4ef7-a911-466d8184975f

Incidentally, Codex wrote almost all the code for this PR!
This commit is contained in:
Michael Bolin
2025-05-14 12:55:49 -07:00
committed by GitHub
parent 0402aef126
commit a12e4b0b31
10 changed files with 452 additions and 27 deletions

View File

@@ -124,8 +124,12 @@ impl ChatWidget<'_> {
&mut self,
key_event: KeyEvent,
) -> std::result::Result<(), SendError<AppEvent>> {
// Special-case <tab>: does not get dispatched to child components.
if matches!(key_event.code, crossterm::event::KeyCode::Tab) {
// Special-case <Tab>: normally toggles focus between history and bottom panes.
// However, when the slash-command popup is visible we forward the key
// to the bottom pane so it can handle auto-completion.
if matches!(key_event.code, crossterm::event::KeyCode::Tab)
&& !self.bottom_pane.is_command_popup_visible()
{
self.input_focus = match self.input_focus {
InputFocus::HistoryPane => InputFocus::BottomPane,
InputFocus::BottomPane => InputFocus::HistoryPane,
@@ -149,18 +153,7 @@ impl ChatWidget<'_> {
InputFocus::BottomPane => {
match self.bottom_pane.handle_key_event(key_event)? {
InputResult::Submitted(text) => {
// Special clientside commands start with a leading slash.
let trimmed = text.trim();
match trimmed {
"/clear" => {
// Clear the current conversation history without exiting.
self.conversation_history.clear();
self.request_redraw()?;
}
_ => {
self.submit_user_message(text)?;
}
}
self.submit_user_message(text)?;
}
InputResult::None => {}
}
@@ -211,6 +204,13 @@ impl ChatWidget<'_> {
Ok(())
}
pub(crate) fn clear_conversation_history(
&mut self,
) -> std::result::Result<(), SendError<AppEvent>> {
self.conversation_history.clear();
self.request_redraw()
}
pub(crate) fn handle_codex_event(
&mut self,
event: Event,