Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions codex-rs/tui/src/transcript_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod layout;
mod mutations;
mod prompt_header;
mod search;
mod select_mode;
mod selection;
mod snapshot;
mod text;
Expand All @@ -38,6 +39,7 @@ use ratatui::widgets::Widget;

use layout::LayoutCache;
use search::Search;
use select_mode::SelectMode;
use selection::Selection;
use snapshot::ViewSnapshot;
use text::TextLayout;
Expand Down Expand Up @@ -126,6 +128,7 @@ pub(crate) struct TranscriptView {
suppressed_prompt_header: Option<prompt_header::SuppressedHeader>,
visible: Vec<VisibleRow>,
selection: Option<Selection>,
select_mode: Option<SelectMode>,
copy_mode: Option<copy_mode::CopyMode>,
held_reading: Option<ViewSnapshot>,
search: Search,
Expand Down Expand Up @@ -164,6 +167,7 @@ impl Default for TranscriptView {
suppressed_prompt_header: None,
visible: Vec::new(),
selection: None,
select_mode: None,
copy_mode: None,
held_reading: None,
search: Search::default(),
Expand Down Expand Up @@ -313,6 +317,7 @@ impl TranscriptView {
self.unseen_activity = false;
}
self.render_selection(buf);
self.render_select_cursor(buf);
if self.history == TranscriptHistoryState::LoadingBeginning && self.is_following() {
self.scroll(current_cells, /*rows*/ 0);
}
Expand Down Expand Up @@ -380,6 +385,7 @@ impl TranscriptView {
return;
}
self.selection = None;
self.select_mode = None;
self.copy_mode = None;
self.release_live_reading();
self.cache.clear();
Expand All @@ -397,15 +403,18 @@ impl TranscriptView {
}

pub(crate) fn has_active_interaction(&self) -> bool {
self.selection.is_some() || self.search.is_active() || self.is_activity_focused()
self.select_mode.is_some()
|| self.selection.is_some()
|| self.search.is_active()
|| self.is_activity_focused()
}

pub(crate) fn is_detailed(&self) -> bool {
self.detailed
}

pub(crate) fn is_following(&self) -> bool {
self.selection.is_none() && self.position == Position::Latest
self.select_mode.is_none() && self.selection.is_none() && self.position == Position::Latest
}

/// Hold the current reading position until every older page has arrived.
Expand All @@ -426,6 +435,7 @@ impl TranscriptView {
self.cancel_beginning();
self.position = Position::Latest;
self.selection = None;
self.select_mode = None;
self.copy_mode = None;
self.release_live_reading();
self.unseen_activity = false;
Expand Down
13 changes: 13 additions & 0 deletions codex-rs/tui/src/transcript_view/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::*;

pub(crate) enum ViewAction {
Changed,
ExitSelectMode,
Copy(String),
CopyOnSelect(String),
PrimarySelection(String),
Expand Down Expand Up @@ -96,6 +97,7 @@ impl TranscriptView {
clear_selection: self.copy_on_select_clear_selection,
},
ViewAction::Changed
| ViewAction::ExitSelectMode
| ViewAction::PrimarySelection(_)
| ViewAction::Copy(_)
| ViewAction::CopyAndFollow(_)
Expand Down Expand Up @@ -159,6 +161,9 @@ impl TranscriptView {
if self.copy_mode.is_some() {
return true;
}
if self.select_mode.is_some() {
return true;
}
if self.selection.is_some() {
return crate::text_selection::is_copy_key(key)
|| code == KeyCode::Esc
Expand Down Expand Up @@ -192,6 +197,11 @@ impl TranscriptView {
if let Some(action) = self.handle_copy_mode_key(key, cells) {
return Some(action);
}
if self.select_mode.is_some() {
return self
.handle_select_mode_key(key, cells)
.or(Some(ViewAction::Changed));
}
if let Some(action) = self.handle_disclosure_key(key, cells) {
return Some(action);
}
Expand Down Expand Up @@ -221,6 +231,9 @@ impl TranscriptView {
event: MouseEvent,
cells: &[Arc<dyn HistoryCell>],
) -> Option<ViewAction> {
if self.select_mode.is_some() {
return self.handle_selection_mouse(event, cells);
}
self.handle_mouse_with_mode(event, cells, MouseMode::Interactive)
}

Expand Down
266 changes: 266 additions & 0 deletions codex-rs/tui/src/transcript_view/select_mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
//! Keyboard-owned transcript cursor and mark state over the existing selection snapshot.
//!
//! An empty `Selection` is the caret. Marking only records its fixed and active anchors here;
//! copying, retained revisions, serialization, and asynchronous completion remain owned by
//! `Selection`.

use super::*;
use crate::text_selection::SelectionUnit;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyEventKind;
use crossterm::event::KeyModifiers;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;

mod motion;

#[derive(Clone, Copy, Default)]
pub(super) struct SelectMode {
pub(super) mark: Option<Mark>,
pub(super) preferred_column: Option<u16>,
}

#[derive(Clone, Copy)]
pub(super) struct Mark {
anchor: Anchor,
active: Anchor,
}

#[derive(Clone, Copy)]
enum CursorMotion {
Character(KeyCode),
Rows(isize),
}

impl SelectMode {
pub(super) fn cursor(self, selection: &Selection) -> Anchor {
self.mark.map_or(selection.end, |mark| mark.active)
}
}

impl TranscriptView {
/// Enter at the bottommost visible selectable position.
#[cfg_attr(not(test), expect(dead_code, reason = "used by a later stack layer"))]
pub(crate) fn begin_select_mode(&mut self, cells: &[Arc<dyn HistoryCell>]) -> bool {
if self.select_mode.is_some() {
return true;
}
self.copy_mode = None;
self.cancel_beginning();
if self.selection.is_none() {
let cursor = self
.visible
.iter()
.rev()
.find_map(|visible| {
let offset = visible.layout.first_cursor_position(visible.row)?;
Some(Anchor {
key: visible.key,
index: visible.index,
offset,
row_bias: visible.layout.row_for_offset(offset) as isize
- visible.row as isize,
})
})
.or_else(|| self.select_boundary(cells, /*end*/ true));
let Some(cursor) = cursor else {
return false;
};
let snapshot = self.capture_snapshot(cells);
self.selection = Some(Selection::keyboard(snapshot, cursor));
}
if self.position == Position::Latest {
self.hold_position();
}
self.select_mode = Some(SelectMode::default());
self.reveal_select_cursor(cells);
true
}

/// Leave the mode while retaining its displayed revision at the paused reading position.
pub(crate) fn end_select_mode(&mut self) {
if self.select_mode.take().is_none() {
return;
}
self.cancel_beginning();
if self.position == Position::Latest {
self.hold_position();
}
if let Some(selection) = self.selection.take() {
self.held_reading = Some(selection.snapshot);
}
}

pub(crate) fn handle_select_mode_key(
&mut self,
key: KeyEvent,
cells: &[Arc<dyn HistoryCell>],
) -> Option<ViewAction> {
if self.select_mode.is_none() || key.kind == KeyEventKind::Release {
return None;
}
let (code, modifiers) = crate::key_hint::normalize_key_parts(key.code, key.modifiers);
let pressed = key.kind == KeyEventKind::Press;
if crate::text_selection::is_copy_key(key)
|| (code == KeyCode::Char('y') && modifiers == KeyModifiers::NONE)
|| (code == KeyCode::Enter
&& modifiers == KeyModifiers::NONE
&& key.kind == KeyEventKind::Press)
{
let Some(text) = self.selected_text(cells).filter(|text| !text.is_empty()) else {
if code == KeyCode::Enter {
return Some(self.exit_select_mode());
}
return Some(ViewAction::Changed);
};
return Some(if code == KeyCode::Enter {
ViewAction::CopyAndFollow(text)
} else {
ViewAction::Copy(text)
});
}
if pressed && code == KeyCode::Esc && modifiers == KeyModifiers::NONE {
if self.select_mode.is_some_and(|mode| mode.mark.is_some()) {
self.clear_select_mark();
return Some(ViewAction::Changed);
}
return Some(self.exit_select_mode());
}
if pressed && code == KeyCode::Char('q') && modifiers == KeyModifiers::NONE {
return Some(self.exit_select_mode());
}
if pressed
&& matches!(code, KeyCode::Char('v') | KeyCode::Char(' '))
&& modifiers == KeyModifiers::NONE
{
self.start_select_mark(cells);
return Some(ViewAction::Changed);
}

let shift_arrow = modifiers == KeyModifiers::SHIFT
&& matches!(
code,
KeyCode::Left | KeyCode::Right | KeyCode::Up | KeyCode::Down
);
if shift_arrow && self.select_mode.is_some_and(|mode| mode.mark.is_none()) {
self.start_select_mark(cells);
}
let motion = match (code, modifiers) {
(
KeyCode::Left | KeyCode::Right | KeyCode::Up | KeyCode::Down,
KeyModifiers::NONE | KeyModifiers::SHIFT,
) => {
if matches!(code, KeyCode::Up | KeyCode::Down) {
CursorMotion::Rows(if code == KeyCode::Up { -1 } else { 1 })
} else {
CursorMotion::Character(code)
}
}
(KeyCode::Char('h'), KeyModifiers::NONE) => CursorMotion::Character(KeyCode::Left),
(KeyCode::Char('l'), KeyModifiers::NONE) => CursorMotion::Character(KeyCode::Right),
(KeyCode::Char('k'), KeyModifiers::NONE) => CursorMotion::Rows(-1),
(KeyCode::Char('j'), KeyModifiers::NONE) => CursorMotion::Rows(1),
_ => return None,
};
self.move_select_cursor(cells, motion);
Some(ViewAction::Changed)
}

fn exit_select_mode(&mut self) -> ViewAction {
self.end_select_mode();
ViewAction::ExitSelectMode
}

fn start_select_mark(&mut self, cells: &[Arc<dyn HistoryCell>]) {
let Some(mut mode) = self.select_mode else {
return;
};
let Some(selection) = self.selection.as_ref() else {
return;
};
let cursor = mode.cursor(selection);
mode.mark = match mode.mark {
Some(_) => {
self.clear_select_mark();
return;
}
None => Some(Mark {
anchor: cursor,
active: cursor,
}),
};
mode.preferred_column = None;
self.select_mode = Some(mode);
self.sync_select_range(cells);
}

fn clear_select_mark(&mut self) {
let Some(mode) = &mut self.select_mode else {
return;
};
let Some(selection) = &mut self.selection else {
return;
};
let cursor = mode.cursor(selection);
mode.mark = None;
mode.preferred_column = None;
selection.set_keyboard_range(cursor, cursor);
}

fn reveal_select_cursor(&mut self, cells: &[Arc<dyn HistoryCell>]) {
let Some(mode) = self.select_mode else {
return;
};
let Some(selection) = self.selection.as_ref() else {
return;
};
let cursor = mode.cursor(selection);
let index = self.resolve(cells, cursor);
let row = self.layout(cells, index).map_or(/*default*/ 0, |layout| {
layout
.row_for_offset(cursor.offset)
.saturating_add_signed(cursor.row_bias.saturating_neg())
});
if !self
.visible
.iter()
.any(|visible| visible.index == index && visible.row == row)
{
self.position = Position::Reading(Anchor { index, ..cursor });
}
}

pub(super) fn render_select_cursor(&self, buf: &mut Buffer) {
let (Some(mode), Some(selection)) = (self.select_mode, self.selection.as_ref()) else {
return;
};
let cursor = mode.cursor(selection);
let Some((y, visible)) = self.visible.iter().enumerate().find(|(_, visible)| {
visible.key == cursor.key
&& visible.row
== visible
.layout
.row_for_offset(cursor.offset)
.saturating_add_signed(cursor.row_bias.saturating_neg())
}) else {
return;
};
let column = visible
.layout
.column_for_offset_on_row(visible.row, cursor.offset);
if column < self.area.width {
let area = Rect::new(
self.area.x + column,
self.area.y + y as u16,
/*width*/ 1,
/*height*/ 1,
);
buf.set_style(area, crate::style::selection_style().underlined());
}
}
}

#[cfg(test)]
#[path = "select_mode_tests.rs"]
mod tests;
Loading
Loading