mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Preserve URL destinations in wrapped hook details (#51473)
## Why The hooks browser rendered detail URLs as plain text. Long URLs in hook commands can wrap or be truncated, so their visible text may omit part of the destination. ## What changed Render web URLs in hook details as terminal hyperlinks, preserving the complete destination across wrapped lines and truncated text. Keep truncation ellipses outside the links. ## Testing Add a regression test and snapshot for a wrapped, truncated hook command URL, checking that linked cells retain the complete destination and the ellipsis remains unlinked. GitOrigin-RevId: 488562266bd5ef880c03bf313bc4cfa7d3104c7b
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
//! Responsive hook browsing presentation; trust and toggle policies stay in the view.
|
||||
//! Responsive hook browsing presentation, including URL-preserving wrapped details.
|
||||
//! Trust and toggle policies stay in the view; truncation ellipses stay outside links.
|
||||
|
||||
use super::*;
|
||||
use crate::bottom_pane::picker_style;
|
||||
@@ -6,6 +7,7 @@ use crate::bottom_pane::selection_popup_common::RenderedRows;
|
||||
use crate::render::Insets;
|
||||
use crate::render::RectExt;
|
||||
use ratatui::widgets::Wrap;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
impl HooksBrowserView {
|
||||
// Keep the existing yellow warning treatment for hooks awaiting review.
|
||||
@@ -151,8 +153,11 @@ impl Renderable for HooksBrowserView {
|
||||
Layout::vertical([Constraint::Length(list_height), Constraint::Fill(1)])
|
||||
.areas(body);
|
||||
render_line_rows(list, buf, rows, self.state);
|
||||
Paragraph::new(self.detail_lines(event, usize::from(content.width)))
|
||||
.render(details.inset(Insets::vh(/*v*/ 0, /*h*/ 2)), buf);
|
||||
crate::terminal_hyperlinks::HyperlinkParagraph::new(
|
||||
&self.detail_lines(event, usize::from(content.width)),
|
||||
ratatui::style::Style::default(),
|
||||
)
|
||||
.render(details.inset(Insets::vh(/*v*/ 0, /*h*/ 2)), buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,6 +165,82 @@ impl Renderable for HooksBrowserView {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn detail_line(label: &str, value: &str) -> crate::terminal_hyperlinks::HyperlinkLine {
|
||||
crate::terminal_hyperlinks::annotate_web_urls_in_line(Line::from(vec![
|
||||
format!("{label:<10}").into(),
|
||||
value.to_string().dim(),
|
||||
]))
|
||||
}
|
||||
|
||||
pub(super) fn detail_wrapped_lines(
|
||||
label: &str,
|
||||
value: &str,
|
||||
width: usize,
|
||||
max_lines: Option<usize>,
|
||||
) -> Vec<crate::terminal_hyperlinks::HyperlinkLine> {
|
||||
let label_width = label.width().saturating_add(1).max(10);
|
||||
let available = width.saturating_sub(label_width).max(1);
|
||||
let mut first = true;
|
||||
let mut lines = value
|
||||
.split('\n')
|
||||
.flat_map(|text| {
|
||||
let source =
|
||||
crate::terminal_hyperlinks::annotate_web_urls_in_line(text.to_owned().dim().into());
|
||||
let wrapped = crate::wrapping::wrap_ranges_trim(text, available)
|
||||
.into_iter()
|
||||
.map(|range| {
|
||||
let prefix = if first {
|
||||
first = false;
|
||||
format!("{label:<label_width$}")
|
||||
} else {
|
||||
" ".repeat(label_width)
|
||||
};
|
||||
crate::wrapping::WrappedLine {
|
||||
line: vec![prefix.into(), text[range.clone()].to_owned().dim()].into(),
|
||||
range,
|
||||
prefix_bytes: label_width,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
crate::terminal_hyperlinks::remap_source_wrapped_line(&source, wrapped)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let Some(max_lines) = max_lines else {
|
||||
return lines;
|
||||
};
|
||||
if lines.len() <= max_lines {
|
||||
return lines;
|
||||
}
|
||||
|
||||
lines.truncate(max_lines);
|
||||
if let Some(last_line) = lines.last_mut() {
|
||||
let prefix_width = last_line.line.spans[..last_line.line.spans.len().saturating_sub(1)]
|
||||
.iter()
|
||||
.map(ratatui::prelude::Span::width)
|
||||
.sum::<usize>();
|
||||
let max_width = width.saturating_sub(prefix_width);
|
||||
let Some(last_span) = last_line.line.spans.last_mut() else {
|
||||
return lines;
|
||||
};
|
||||
let truncated = truncate_line_with_ellipsis_if_overflow(
|
||||
Line::from(format!("{}…", last_span.content)),
|
||||
max_width,
|
||||
);
|
||||
let content = truncated
|
||||
.spans
|
||||
.into_iter()
|
||||
.map(|span| span.content.into_owned())
|
||||
.collect::<String>();
|
||||
let visible_end = prefix_width + crate::width::display_width(content.trim_end_matches('…'));
|
||||
last_span.content = content.into();
|
||||
last_line.hyperlinks.retain_mut(|link| {
|
||||
link.columns.end = link.columns.end.min(visible_end);
|
||||
!link.columns.is_empty()
|
||||
});
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "hooks_browser_render_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
//! Wide hook titles preserve the selection fill and per-span index emphasis.
|
||||
//! Regression coverage for wide hook selection styling and URL-preserving detail truncation.
|
||||
|
||||
use super::super::tests::hook;
|
||||
use super::super::tests::render_buffer;
|
||||
use super::super::tests::render_lines;
|
||||
use super::*;
|
||||
use crate::app_event::AppEvent;
|
||||
use crate::app_event_sender::AppEventSender;
|
||||
use crate::bottom_pane::bottom_pane_view::BottomPaneView;
|
||||
use crate::test_support::PathBufExt;
|
||||
use crate::test_support::test_path_buf;
|
||||
use codex_app_server_protocol::HookEventName;
|
||||
use codex_app_server_protocol::HookSource;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use insta::assert_snapshot;
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::style::Color;
|
||||
use ratatui::style::Modifier;
|
||||
use ratatui::style::Style;
|
||||
use tokio::sync::mpsc::unbounded_channel;
|
||||
|
||||
#[test]
|
||||
fn wide_titles_preserve_reverse_video_selection_and_dim_index() {
|
||||
@@ -38,3 +52,52 @@ fn wide_titles_preserve_reverse_video_selection_and_dim_index() {
|
||||
assert!(!buf[(10, 0)].modifier.contains(Modifier::DIM));
|
||||
assert_eq!(buf[(10, 0)].symbol(), "检");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrapped_hook_command_preserves_the_complete_url_destination() {
|
||||
let url = "https://github.com/openai/codex/pull/12345?diff=split&review=complete-destination";
|
||||
let (tx_raw, _rx) = unbounded_channel::<AppEvent>();
|
||||
let mut url_hook = hook(
|
||||
"path:review",
|
||||
HookEventName::PreToolUse,
|
||||
HookSource::User,
|
||||
/*plugin_id*/ None,
|
||||
&format!("curl {url}"),
|
||||
/*enabled*/ true,
|
||||
/*is_managed*/ false,
|
||||
/*display_order*/ 0,
|
||||
);
|
||||
url_hook.source_path = test_path_buf("/tmp/h.json").abs();
|
||||
let mut view = HooksBrowserView::new(
|
||||
vec![url_hook],
|
||||
Vec::new(),
|
||||
Vec::new(),
|
||||
AppEventSender::new(tx_raw),
|
||||
);
|
||||
view.handle_key_event(KeyEvent::from(KeyCode::Enter));
|
||||
let buf = render_buffer(&view, /*width*/ 44);
|
||||
let linked = buf
|
||||
.content
|
||||
.iter()
|
||||
.filter(|cell| cell.symbol().contains("\x1b]8;;"))
|
||||
.map(|cell| {
|
||||
assert!(cell.symbol().starts_with(&format!("\x1b]8;;{url}\x07")));
|
||||
crate::terminal_hyperlinks::strip_osc8(cell.symbol())
|
||||
})
|
||||
.collect::<String>();
|
||||
assert_eq!(
|
||||
linked,
|
||||
"https://github.com/openai/codex/pull/12345?diff=split&review=complete-"
|
||||
);
|
||||
let ellipses = buf
|
||||
.content
|
||||
.iter()
|
||||
.filter(|cell| crate::terminal_hyperlinks::strip_osc8(cell.symbol()) == "…")
|
||||
.map(ratatui::buffer::Cell::symbol)
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(ellipses, vec!["…"]);
|
||||
assert_snapshot!(
|
||||
"hooks_browser_wrapped_url",
|
||||
render_lines(&view, /*width*/ 44)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@ use ratatui::text::Span;
|
||||
use ratatui::widgets::Paragraph;
|
||||
use ratatui::widgets::Widget;
|
||||
use strum::IntoEnumIterator;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use self::render::detail_line;
|
||||
use self::render::detail_wrapped_lines;
|
||||
use super::CancellationEvent;
|
||||
use super::bottom_pane_view::BottomPaneView;
|
||||
use super::picker_style::selection_style;
|
||||
@@ -487,9 +488,15 @@ impl HooksBrowserView {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn detail_lines(&self, event_name: HookEventName, width: usize) -> Vec<Line<'static>> {
|
||||
fn detail_lines(
|
||||
&self,
|
||||
event_name: HookEventName,
|
||||
width: usize,
|
||||
) -> Vec<crate::terminal_hyperlinks::HyperlinkLine> {
|
||||
let Some(hook) = self.selected_hook(event_name) else {
|
||||
return vec!["No hooks installed for this event.".dim().into()];
|
||||
return crate::terminal_hyperlinks::plain_hyperlink_lines(vec![
|
||||
"No hooks installed for this event.".dim().into(),
|
||||
]);
|
||||
};
|
||||
|
||||
let mut lines = vec![detail_line("Event", event_label(event_name))];
|
||||
@@ -775,59 +782,6 @@ fn config_source_label(source: HookSource) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
fn detail_line(label: &str, value: &str) -> Line<'static> {
|
||||
Line::from(vec![format!("{label:<10}").into(), value.to_string().dim()])
|
||||
}
|
||||
|
||||
fn detail_wrapped_lines(
|
||||
label: &str,
|
||||
value: &str,
|
||||
width: usize,
|
||||
max_lines: Option<usize>,
|
||||
) -> Vec<Line<'static>> {
|
||||
let label_width = label.width().saturating_add(1).max(10);
|
||||
let prefix = format!("{label:<label_width$}");
|
||||
let available = width.saturating_sub(prefix.width()).max(1);
|
||||
let mut wrapped = textwrap::wrap(value, available).into_iter();
|
||||
let first = wrapped.next().unwrap_or_default().into_owned();
|
||||
let mut lines = vec![Line::from(vec![prefix.into(), first.dim()])];
|
||||
lines.extend(wrapped.map(|line| {
|
||||
Line::from(vec![
|
||||
" ".repeat(label_width).into(),
|
||||
line.into_owned().dim(),
|
||||
])
|
||||
}));
|
||||
let Some(max_lines) = max_lines else {
|
||||
return lines;
|
||||
};
|
||||
if lines.len() <= max_lines {
|
||||
return lines;
|
||||
}
|
||||
|
||||
lines.truncate(max_lines);
|
||||
if let Some(last_line) = lines.last_mut() {
|
||||
let prefix_width = last_line.spans[..last_line.spans.len().saturating_sub(1)]
|
||||
.iter()
|
||||
.map(ratatui::prelude::Span::width)
|
||||
.sum::<usize>();
|
||||
let max_width = width.saturating_sub(prefix_width);
|
||||
let Some(last_span) = last_line.spans.last_mut() else {
|
||||
return lines;
|
||||
};
|
||||
let truncated = truncate_line_with_ellipsis_if_overflow(
|
||||
Line::from(format!("{}…", last_span.content)),
|
||||
max_width,
|
||||
);
|
||||
let content = truncated
|
||||
.spans
|
||||
.into_iter()
|
||||
.map(|span| span.content.into_owned())
|
||||
.collect::<String>();
|
||||
last_span.content = content.into();
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -854,7 +808,7 @@ mod tests {
|
||||
use ratatui::style::Modifier;
|
||||
use tokio::sync::mpsc::unbounded_channel;
|
||||
|
||||
fn render_lines(view: &HooksBrowserView, width: u16) -> String {
|
||||
pub(super) fn render_lines(view: &HooksBrowserView, width: u16) -> String {
|
||||
let height = view.desired_height(width);
|
||||
let area = Rect::new(0, 0, width, height);
|
||||
let mut buf = Buffer::empty(area);
|
||||
@@ -868,7 +822,7 @@ mod tests {
|
||||
if symbol.is_empty() {
|
||||
" ".to_string()
|
||||
} else {
|
||||
symbol.to_string()
|
||||
crate::terminal_hyperlinks::strip_osc8(symbol)
|
||||
}
|
||||
})
|
||||
.collect::<String>();
|
||||
@@ -881,7 +835,7 @@ mod tests {
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn render_buffer(view: &HooksBrowserView, width: u16) -> Buffer {
|
||||
pub(super) fn render_buffer(view: &HooksBrowserView, width: u16) -> Buffer {
|
||||
let height = view.desired_height(width);
|
||||
let area = Rect::new(0, 0, width, height);
|
||||
let mut buf = Buffer::empty(area);
|
||||
@@ -890,7 +844,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn hook(
|
||||
pub(super) fn hook(
|
||||
key: &str,
|
||||
event_name: HookEventName,
|
||||
source: HookSource,
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
---
|
||||
source: tui/src/bottom_pane/hooks_browser_render_tests.rs
|
||||
expression: "render_lines(&view, 44)"
|
||||
---
|
||||
|
||||
PreToolUse hooks
|
||||
Turn hooks on or off. Your changes are
|
||||
saved automatically.
|
||||
|
||||
› [x] 1 Unnamed hook
|
||||
|
||||
Event PreToolUse
|
||||
Matcher Bash
|
||||
Source User config - /tmp/h.json
|
||||
Command curl https://github.com/
|
||||
openai/codex/pull/12345?
|
||||
diff=split&review=complete-…
|
||||
Mode Sync
|
||||
Timeout 30s
|
||||
Trust Trusted
|
||||
|
||||
|
||||
space/enter toggle · esc back
|
||||
Reference in New Issue
Block a user