//! Shared history-cell building blocks reused across transcript concerns. //! Wrapped prefixes remain display-only while annotated rows retain their logical source. use super::*; #[derive(Debug)] pub(crate) struct PlainHistoryCell { pub(super) lines: Vec>, } impl PlainHistoryCell { pub(crate) fn new(lines: Vec>) -> Self { Self { lines } } } impl HistoryCell for PlainHistoryCell { fn display_lines(&self, _width: u16) -> Vec> { self.lines.clone() } fn raw_lines(&self) -> Vec> { plain_lines(self.lines.clone()) } } #[derive(Debug)] pub(crate) struct WebHyperlinkHistoryCell { lines: Vec, } impl WebHyperlinkHistoryCell { pub(crate) fn new(lines: Vec>) -> Self { Self { lines: crate::terminal_hyperlinks::annotate_web_urls(lines), } } pub(crate) fn new_hyperlink_lines(lines: Vec) -> Self { Self { lines } } } impl HistoryCell for WebHyperlinkHistoryCell { fn display_lines(&self, _width: u16) -> Vec> { self.lines.iter().map(|line| line.line.clone()).collect() } fn display_hyperlink_lines(&self, _width: u16) -> Vec { self.lines.clone() } fn transcript_hyperlink_lines(&self, width: u16) -> Vec { self.display_hyperlink_lines(width) } fn raw_lines(&self) -> Vec> { plain_lines(self.lines.iter().map(|line| line.line.clone())) } } #[derive(Debug)] pub(crate) struct PrefixedWrappedHistoryCell { text: Text<'static>, initial_prefix: Line<'static>, subsequent_prefix: Line<'static>, } impl PrefixedWrappedHistoryCell { pub(crate) fn new( text: impl Into>, initial_prefix: impl Into>, subsequent_prefix: impl Into>, ) -> Self { Self { text: text.into(), initial_prefix: initial_prefix.into(), subsequent_prefix: subsequent_prefix.into(), } } } impl HistoryCell for PrefixedWrappedHistoryCell { fn display_lines(&self, width: u16) -> Vec> { visible_lines(self.display_hyperlink_lines(width)) } fn display_hyperlink_lines(&self, width: u16) -> Vec { if width == 0 { return Vec::new(); } let opts = RtOptions::new(usize::from(width)) .initial_indent(self.initial_prefix.clone()) .subsequent_indent(self.subsequent_prefix.clone()); crate::terminal_hyperlinks::adaptive_wrap_hyperlink_lines( &plain_hyperlink_lines(self.text.lines.clone()), opts, ) } fn transcript_hyperlink_lines(&self, width: u16) -> Vec { self.display_hyperlink_lines(width) } fn raw_lines(&self) -> Vec> { plain_lines(self.text.clone().lines) } } #[derive(Debug)] pub(crate) struct CompositeHistoryCell { pub(super) parts: Vec>, } impl CompositeHistoryCell { pub(crate) fn new(parts: Vec>) -> Self { Self { parts } } } impl HistoryCell for CompositeHistoryCell { fn warning_entries(&self) -> Vec { self.parts .iter() .flat_map(|part| part.warning_entries()) .collect() } fn live_raw_lines(&self) -> Vec> { let mut lines = Vec::new(); for part in &self.parts { let part = part.live_raw_lines(); if !part.is_empty() { if !lines.is_empty() { lines.push(Line::default()); } lines.extend(part); } } lines } fn warning_keys(&self) -> Vec> { self.parts .iter() .flat_map(|part| part.warning_keys()) .collect() } fn compact_hyperlink_lines(&self, width: u16) -> Vec { let mut out = Vec::new(); for part in &self.parts { let lines = part.compact_hyperlink_lines(width); if !lines.is_empty() { if !out.is_empty() { out.push(HyperlinkLine::from("")); } out.extend(lines); } } out } fn display_lines(&self, width: u16) -> Vec> { let mut out: Vec> = Vec::new(); let mut first = true; for part in &self.parts { let mut lines = part.display_lines(width); if !lines.is_empty() { if !first { out.push(Line::from("")); } out.append(&mut lines); first = false; } } out } fn display_hyperlink_lines(&self, width: u16) -> Vec { let mut out = Vec::new(); let mut first = true; for part in &self.parts { let mut lines = part.display_hyperlink_lines(width); if !lines.is_empty() { if !first { out.push(HyperlinkLine::from("")); } out.append(&mut lines); first = false; } } out } fn transcript_hyperlink_lines(&self, width: u16) -> Vec { let mut out = Vec::new(); let mut first = true; for part in &self.parts { let mut lines = part.transcript_hyperlink_lines(width); if !lines.is_empty() { if !first { out.push(HyperlinkLine::from("")); } out.append(&mut lines); first = false; } } out } fn raw_lines(&self) -> Vec> { let mut out: Vec> = Vec::new(); let mut first = true; for part in &self.parts { let mut lines = part.raw_lines(); if !lines.is_empty() { if !first { out.push(Line::from("")); } out.append(&mut lines); first = false; } } out } fn has_stable_transcript_height(&self) -> bool { false } } #[cfg(test)] #[path = "base_tests.rs"] mod tests;