2025-05-14 10:13:29 -07:00
|
|
|
//! Bottom pane: shows the ChatComposer or a BottomPaneView, if one is active.
|
|
|
|
|
|
|
|
|
|
use bottom_pane_view::BottomPaneView;
|
|
|
|
|
use bottom_pane_view::ConditionalUpdate;
|
|
|
|
|
use crossterm::event::KeyEvent;
|
|
|
|
|
use ratatui::buffer::Buffer;
|
|
|
|
|
use ratatui::layout::Rect;
|
|
|
|
|
use ratatui::widgets::WidgetRef;
|
|
|
|
|
use std::sync::mpsc::SendError;
|
|
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
|
|
|
|
|
|
use crate::app_event::AppEvent;
|
|
|
|
|
use crate::user_approval_widget::ApprovalRequest;
|
|
|
|
|
|
|
|
|
|
mod approval_modal_view;
|
|
|
|
|
mod bottom_pane_view;
|
|
|
|
|
mod chat_composer;
|
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!
2025-05-14 12:55:49 -07:00
|
|
|
mod command_popup;
|
2025-05-14 10:13:29 -07:00
|
|
|
mod status_indicator_view;
|
|
|
|
|
|
|
|
|
|
pub(crate) use chat_composer::ChatComposer;
|
|
|
|
|
pub(crate) use chat_composer::InputResult;
|
|
|
|
|
|
|
|
|
|
use approval_modal_view::ApprovalModalView;
|
|
|
|
|
use status_indicator_view::StatusIndicatorView;
|
|
|
|
|
|
|
|
|
|
/// Pane displayed in the lower half of the chat UI.
|
|
|
|
|
pub(crate) struct BottomPane<'a> {
|
|
|
|
|
/// Composer is retained even when a BottomPaneView is displayed so the
|
|
|
|
|
/// input state is retained when the view is closed.
|
|
|
|
|
composer: ChatComposer<'a>,
|
|
|
|
|
|
|
|
|
|
/// If present, this is displayed instead of the `composer`.
|
|
|
|
|
active_view: Option<Box<dyn BottomPaneView<'a> + 'a>>,
|
|
|
|
|
|
|
|
|
|
app_event_tx: Sender<AppEvent>,
|
|
|
|
|
has_input_focus: bool,
|
|
|
|
|
is_task_running: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct BottomPaneParams {
|
|
|
|
|
pub(crate) app_event_tx: Sender<AppEvent>,
|
|
|
|
|
pub(crate) has_input_focus: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BottomPane<'_> {
|
|
|
|
|
pub fn new(params: BottomPaneParams) -> Self {
|
|
|
|
|
Self {
|
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!
2025-05-14 12:55:49 -07:00
|
|
|
composer: ChatComposer::new(params.has_input_focus, params.app_event_tx.clone()),
|
2025-05-14 10:13:29 -07:00
|
|
|
active_view: None,
|
|
|
|
|
app_event_tx: params.app_event_tx,
|
|
|
|
|
has_input_focus: params.has_input_focus,
|
|
|
|
|
is_task_running: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Forward a key event to the active view or the composer.
|
|
|
|
|
pub fn handle_key_event(
|
|
|
|
|
&mut self,
|
|
|
|
|
key_event: KeyEvent,
|
|
|
|
|
) -> Result<InputResult, SendError<AppEvent>> {
|
|
|
|
|
if let Some(mut view) = self.active_view.take() {
|
|
|
|
|
view.handle_key_event(self, key_event)?;
|
|
|
|
|
if !view.is_complete() {
|
|
|
|
|
self.active_view = Some(view);
|
|
|
|
|
} else if self.is_task_running {
|
|
|
|
|
let height = self.composer.calculate_required_height(&Rect::default());
|
|
|
|
|
self.active_view = Some(Box::new(StatusIndicatorView::new(
|
|
|
|
|
self.app_event_tx.clone(),
|
|
|
|
|
height,
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
self.request_redraw()?;
|
|
|
|
|
Ok(InputResult::None)
|
|
|
|
|
} else {
|
|
|
|
|
let (input_result, needs_redraw) = self.composer.handle_key_event(key_event);
|
|
|
|
|
if needs_redraw {
|
|
|
|
|
self.request_redraw()?;
|
|
|
|
|
}
|
|
|
|
|
Ok(input_result)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update the status indicator text (only when the `StatusIndicatorView` is
|
|
|
|
|
/// active).
|
|
|
|
|
pub(crate) fn update_status_text(&mut self, text: String) -> Result<(), SendError<AppEvent>> {
|
|
|
|
|
if let Some(view) = &mut self.active_view {
|
|
|
|
|
match view.update_status_text(text) {
|
|
|
|
|
ConditionalUpdate::NeedsRedraw => {
|
|
|
|
|
self.request_redraw()?;
|
|
|
|
|
}
|
|
|
|
|
ConditionalUpdate::NoRedraw => {
|
|
|
|
|
// No redraw needed.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update the UI to reflect whether this `BottomPane` has input focus.
|
|
|
|
|
pub(crate) fn set_input_focus(&mut self, has_focus: bool) {
|
|
|
|
|
self.has_input_focus = has_focus;
|
|
|
|
|
self.composer.set_input_focus(has_focus);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_task_running(&mut self, running: bool) -> Result<(), SendError<AppEvent>> {
|
|
|
|
|
self.is_task_running = running;
|
|
|
|
|
|
|
|
|
|
match (running, self.active_view.is_some()) {
|
|
|
|
|
(true, false) => {
|
|
|
|
|
// Show status indicator overlay.
|
|
|
|
|
let height = self.composer.calculate_required_height(&Rect::default());
|
|
|
|
|
self.active_view = Some(Box::new(StatusIndicatorView::new(
|
|
|
|
|
self.app_event_tx.clone(),
|
|
|
|
|
height,
|
|
|
|
|
)));
|
|
|
|
|
self.request_redraw()?;
|
|
|
|
|
}
|
|
|
|
|
(false, true) => {
|
|
|
|
|
if let Some(mut view) = self.active_view.take() {
|
|
|
|
|
if view.should_hide_when_task_is_done() {
|
|
|
|
|
// Leave self.active_view as None.
|
|
|
|
|
self.request_redraw()?;
|
|
|
|
|
} else {
|
|
|
|
|
// Preserve the view.
|
|
|
|
|
self.active_view = Some(view);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
// No change.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Called when the agent requests user approval.
|
|
|
|
|
pub fn push_approval_request(
|
|
|
|
|
&mut self,
|
|
|
|
|
request: ApprovalRequest,
|
|
|
|
|
) -> Result<(), SendError<AppEvent>> {
|
|
|
|
|
let request = if let Some(view) = self.active_view.as_mut() {
|
|
|
|
|
match view.try_consume_approval_request(request) {
|
|
|
|
|
Some(request) => request,
|
|
|
|
|
None => {
|
|
|
|
|
self.request_redraw()?;
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
request
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Otherwise create a new approval modal overlay.
|
|
|
|
|
let modal = ApprovalModalView::new(request, self.app_event_tx.clone());
|
|
|
|
|
self.active_view = Some(Box::new(modal));
|
|
|
|
|
self.request_redraw()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Height (terminal rows) required by the current bottom pane.
|
|
|
|
|
pub fn calculate_required_height(&self, area: &Rect) -> u16 {
|
|
|
|
|
if let Some(view) = &self.active_view {
|
|
|
|
|
view.calculate_required_height(area)
|
|
|
|
|
} else {
|
|
|
|
|
self.composer.calculate_required_height(area)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn request_redraw(&self) -> Result<(), SendError<AppEvent>> {
|
|
|
|
|
self.app_event_tx.send(AppEvent::Redraw)
|
|
|
|
|
}
|
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!
2025-05-14 12:55:49 -07:00
|
|
|
|
|
|
|
|
/// Returns true when the slash-command popup inside the composer is visible.
|
|
|
|
|
pub(crate) fn is_command_popup_visible(&self) -> bool {
|
|
|
|
|
self.active_view.is_none() && self.composer.is_command_popup_visible()
|
|
|
|
|
}
|
2025-05-14 10:13:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WidgetRef for &BottomPane<'_> {
|
|
|
|
|
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
|
|
|
|
|
// Show BottomPaneView if present.
|
|
|
|
|
if let Some(ov) = &self.active_view {
|
|
|
|
|
ov.render(area, buf);
|
|
|
|
|
} else {
|
|
|
|
|
(&self.composer).render_ref(area, buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|