Base: codex-main originale (prima del fork aKode)
This commit is contained in:
151
codex-rs/tui/src/terminal_hyperlinks/paragraph.rs
Normal file
151
codex-rs/tui/src/terminal_hyperlinks/paragraph.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
//! Paragraph and prelaid-out row rendering that keeps visible text and links aligned.
|
||||
|
||||
use super::HyperlinkLine;
|
||||
use super::annotate_web_urls;
|
||||
use super::mark_buffer_hyperlinks;
|
||||
use super::visible_lines_ref;
|
||||
use crate::render::line_utils::line_to_borrowed;
|
||||
use crate::render::renderable::Renderable;
|
||||
use ratatui::buffer::Buffer;
|
||||
use ratatui::buffer::CellWidth;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::style::Style;
|
||||
use ratatui::text::Line;
|
||||
use ratatui::text::Text;
|
||||
use ratatui::widgets::Paragraph;
|
||||
use ratatui::widgets::Widget;
|
||||
use ratatui::widgets::Wrap;
|
||||
|
||||
/// Owns a word-wrapped paragraph whose web destinations survive clipping and wrapping.
|
||||
pub(crate) struct HyperlinkText(Vec<HyperlinkLine>);
|
||||
|
||||
impl HyperlinkText {
|
||||
pub(crate) fn new(lines: Vec<Line<'static>>) -> Self {
|
||||
Self(annotate_web_urls(lines))
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for HyperlinkText {
|
||||
fn render(&self, area: Rect, buf: &mut Buffer) {
|
||||
HyperlinkParagraph::new(&self.0, Style::default()).render(area, buf);
|
||||
}
|
||||
|
||||
fn desired_height(&self, width: u16) -> u16 {
|
||||
HyperlinkParagraph::new(&self.0, Style::default()).line_count(width) as u16
|
||||
}
|
||||
}
|
||||
|
||||
/// Word-wraps without trimming and applies the same vertical scroll to text and links.
|
||||
pub(crate) struct HyperlinkParagraph<'a> {
|
||||
lines: &'a [HyperlinkLine],
|
||||
paragraph: Paragraph<'a>,
|
||||
scroll_rows: u16,
|
||||
}
|
||||
|
||||
impl<'a> HyperlinkParagraph<'a> {
|
||||
pub(crate) fn new(lines: &'a [HyperlinkLine], style: Style) -> Self {
|
||||
Self {
|
||||
lines,
|
||||
paragraph: Paragraph::new(Text::from(visible_lines_ref(lines)))
|
||||
.style(style)
|
||||
.wrap(Wrap { trim: false }),
|
||||
scroll_rows: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn line_count(&self, width: u16) -> usize {
|
||||
self.paragraph.line_count(width)
|
||||
}
|
||||
|
||||
pub(crate) fn scroll(mut self, rows: u16) -> Self {
|
||||
self.scroll_rows = rows;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for HyperlinkParagraph<'_> {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
self.paragraph
|
||||
.scroll((self.scroll_rows, 0))
|
||||
.render(area, buf);
|
||||
mark_buffer_hyperlinks(buf, area, self.lines, usize::from(self.scroll_rows));
|
||||
}
|
||||
}
|
||||
|
||||
/// Owns rows that have already been wrapped or clipped by their layout code.
|
||||
/// Rendering clips to the viewport without changing row count or spacing.
|
||||
pub(crate) struct HyperlinkRows(Vec<HyperlinkLine>);
|
||||
|
||||
impl From<Vec<HyperlinkLine>> for HyperlinkRows {
|
||||
fn from(lines: Vec<HyperlinkLine>) -> Self {
|
||||
Self(lines)
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderable for HyperlinkRows {
|
||||
fn render(&self, area: Rect, buf: &mut Buffer) {
|
||||
for (row, line) in self.0.iter().take(usize::from(area.height)).enumerate() {
|
||||
line.render(Rect::new(area.x, area.y + row as u16, area.width, 1), buf);
|
||||
}
|
||||
}
|
||||
|
||||
fn desired_height(&self, width: u16) -> u16 {
|
||||
if width == 0 {
|
||||
0
|
||||
} else {
|
||||
self.0.len().min(usize::from(u16::MAX)) as u16
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render one already-laid-out row, clipping text and links without wrapping it again.
|
||||
impl Widget for &HyperlinkLine {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
self.line.clone().render(area, buf);
|
||||
self.mark_cells(area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
impl HyperlinkLine {
|
||||
/// Paint a fitted, left-aligned row using the terminal cell widths of its graphemes.
|
||||
pub(crate) fn render_fitted(&self, area: Rect, buf: &mut Buffer) {
|
||||
Paragraph::new(line_to_borrowed(&self.line)).render(area, buf);
|
||||
self.mark_cells(area, buf);
|
||||
}
|
||||
|
||||
fn mark_cells(&self, area: Rect, buf: &mut Buffer) {
|
||||
if area.is_empty() {
|
||||
return;
|
||||
}
|
||||
for link in &self.hyperlinks {
|
||||
let Some(destination) = link.terminal_destination() else {
|
||||
continue;
|
||||
};
|
||||
let start = link.columns.start.min(usize::from(area.width)) as u16;
|
||||
let end = link.columns.end.min(usize::from(area.width)) as u16;
|
||||
mark_hyperlink_cells(
|
||||
buf,
|
||||
Rect::new(area.x + start, area.y, end.saturating_sub(start), 1),
|
||||
&destination,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark the painted graphemes in one row with an already validated terminal destination.
|
||||
pub(crate) fn mark_hyperlink_cells(buf: &mut Buffer, row: Rect, destination: &str) {
|
||||
let mut x = row.left();
|
||||
while x < row.right() {
|
||||
let cell = &mut buf[(x, row.y)];
|
||||
if cell.diff_option == ratatui::buffer::CellDiffOption::Skip {
|
||||
x += 1;
|
||||
continue;
|
||||
}
|
||||
let width =
|
||||
std::num::NonZeroU16::new(cell.cell_width()).unwrap_or(std::num::NonZeroU16::MIN);
|
||||
x = x.saturating_add(width.get());
|
||||
let symbol = format!("\x1b]8;;{destination}\x07{}\x1b]8;;\x07", cell.symbol());
|
||||
cell.set_symbol(&symbol)
|
||||
.set_diff_option(ratatui::buffer::CellDiffOption::ForcedWidth(width));
|
||||
}
|
||||
}
|
||||
124
codex-rs/tui/src/terminal_hyperlinks/source.rs
Normal file
124
codex-rs/tui/src/terminal_hyperlinks/source.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
//! Logical text and margins retained when display wrapping removes whitespace or adds a gutter.
|
||||
|
||||
use ratatui::style::Style;
|
||||
use ratatui::text::Line;
|
||||
use ratatui::text::Span;
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Wrapping policy of an existing display renderer.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub(crate) enum LineWrapPolicy {
|
||||
#[default]
|
||||
Word,
|
||||
/// Keep fitting URL tokens intact and split oversized tokens to fit the viewport.
|
||||
UrlAware,
|
||||
Hard,
|
||||
}
|
||||
|
||||
/// A displayed line's contiguous fragment of one original logical line.
|
||||
///
|
||||
/// Wrapped fragments share `text`. `prefix_bytes` counts synthetic display bytes before
|
||||
/// the fragment, so selection never has to infer whether a gutter belongs to the source.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub(crate) struct LogicalLineSource {
|
||||
pub(crate) text: Arc<str>,
|
||||
pub(crate) copy: Option<Arc<crate::markdown_copy::CopyLine>>,
|
||||
/// Selectable label length and the original body, whose Markdown ranges exclude the label.
|
||||
pub(crate) labelled_body: Option<Arc<(usize, LogicalLineSource)>>,
|
||||
/// Literal user text remains prose when copied alongside rendered Markdown.
|
||||
pub(crate) copy_as_prose: bool,
|
||||
/// Span styles share the text's byte coordinates, including whitespace omitted by wrapping.
|
||||
pub(crate) styles: Arc<[(Range<usize>, Style)]>,
|
||||
/// Row style, applied beneath explicit span styles just like ratatui's `Line`.
|
||||
pub(crate) line_style: Style,
|
||||
/// A later uniform span-style patch, such as the dim/italic reasoning treatment.
|
||||
pub(crate) span_style: Style,
|
||||
pub(crate) range: Range<usize>,
|
||||
pub(crate) prefix_bytes: usize,
|
||||
pub(crate) wrap_policy: LineWrapPolicy,
|
||||
pub(crate) continuation_indent: Line<'static>,
|
||||
/// Blank columns after wrapped content; they remain outside the copied source text.
|
||||
pub(crate) right_reserve: u16,
|
||||
}
|
||||
|
||||
impl LogicalLineSource {
|
||||
pub(crate) fn new(text: String) -> Self {
|
||||
let end = text.len();
|
||||
Self {
|
||||
text: text.into(),
|
||||
copy: None,
|
||||
labelled_body: None,
|
||||
copy_as_prose: false,
|
||||
styles: vec![(0..end, Style::default())].into(),
|
||||
line_style: Style::default(),
|
||||
span_style: Style::default(),
|
||||
range: 0..end,
|
||||
prefix_bytes: 0,
|
||||
wrap_policy: LineWrapPolicy::Word,
|
||||
continuation_indent: Line::default(),
|
||||
right_reserve: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn from_line(line: &Line<'_>) -> Self {
|
||||
let mut source = Self::new(
|
||||
line.spans
|
||||
.iter()
|
||||
.map(|span| span.content.as_ref())
|
||||
.collect(),
|
||||
);
|
||||
let mut offset = 0;
|
||||
source.line_style = line.style;
|
||||
source.styles = line
|
||||
.spans
|
||||
.iter()
|
||||
.map(|span| {
|
||||
let start = offset;
|
||||
offset += span.content.len();
|
||||
(start..offset, span.style)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into();
|
||||
source
|
||||
}
|
||||
|
||||
pub(crate) fn styled_range(&self, range: Range<usize>) -> Line<'static> {
|
||||
Line::from(
|
||||
self.styles
|
||||
.iter()
|
||||
.filter_map(|(style_range, style)| {
|
||||
let start = style_range.start.max(range.start);
|
||||
let end = style_range.end.min(range.end);
|
||||
(start < end).then(|| {
|
||||
Span::styled(
|
||||
self.text[start..end].to_owned(),
|
||||
self.line_style.patch(*style).patch(self.span_style),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn wrapped(&self, displayed: Range<usize>, prefix_bytes: usize) -> Self {
|
||||
let source_start = self.prefix_bytes;
|
||||
let source_end = source_start + self.range.len();
|
||||
let start = displayed.start.clamp(source_start, source_end);
|
||||
let end = displayed.end.clamp(source_start, source_end);
|
||||
Self {
|
||||
text: Arc::clone(&self.text),
|
||||
copy: self.copy.clone(),
|
||||
labelled_body: self.labelled_body.clone(),
|
||||
copy_as_prose: self.copy_as_prose,
|
||||
styles: Arc::clone(&self.styles),
|
||||
line_style: self.line_style,
|
||||
span_style: self.span_style,
|
||||
range: self.range.start + start - source_start..self.range.start + end - source_start,
|
||||
prefix_bytes: prefix_bytes + start.saturating_sub(displayed.start).min(displayed.len()),
|
||||
wrap_policy: self.wrap_policy,
|
||||
continuation_indent: self.continuation_indent.clone(),
|
||||
right_reserve: self.right_reserve,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user