use crate::ansi::hyperlink::parse_hyperlink;
use crate::ansi::params::Params;
use crate::ansi::text::{Token, string_width, tokenize};
use crate::buffer::{Bounded, Surface, SurfaceMut};
use crate::cell::Cell;
use crate::layout::{Position, Rect};
use crate::style::{Style, read_style};
use super::{TextSurface, WidthMode, WrapMode};
pub struct Painter<'s, S: TextSurface + ?Sized> {
target: &'s mut S,
mode: WidthMode,
eaw_wide: bool,
}
impl<'s, S: TextSurface + ?Sized> Painter<'s, S> {
pub fn new(target: &'s mut S) -> Self {
let mode = target.width_mode();
let eaw_wide = target.eaw_wide();
Self {
target,
mode,
eaw_wide,
}
}
fn paint_truncate(
&mut self,
start: Position,
clip: Rect,
s: &str,
tail_text: &str,
tail_style: Style,
style: Style,
) -> Position {
if clip.is_empty() {
return start;
}
let tail_w = string_width(tail_text.as_bytes(), self.mode, self.eaw_wide) as u16;
let tail = if tail_w == 0 || tail_w > clip.width {
None
} else {
Some(Tail {
text: tail_text,
style: &tail_style,
width: tail_w,
})
};
self.paint_inner(start, clip, s, WrapMode::Truncate, tail, style)
}
fn paint_tail(&mut self, tail: Tail<'_>, clip: Rect, y: u16) {
let tail_x = clip.right().saturating_sub(tail.width);
let sub = Rect::new(tail_x, y, tail.width, 1).intersection(clip);
self.paint_inner(
Position::new(tail_x, y),
sub,
tail.text,
WrapMode::Truncate,
None,
tail.style.clone(),
);
}
fn paint(
&mut self,
start: Position,
clip: Rect,
s: &str,
wrap: WrapMode,
style: Style,
) -> Position {
self.paint_inner(start, clip, s, wrap, None, style)
}
fn paint_inner(
&mut self,
start: Position,
clip: Rect,
s: &str,
wrap: WrapMode,
tail: Option<Tail<'_>>,
base: Style,
) -> Position {
if clip.is_empty() {
return start;
}
if start.y >= clip.bottom() {
return start;
}
let mut x = start.x;
let mut y = start.y;
let mut pen = Style::default();
let mut pending: Option<(u16, u16, String, u8)> = None;
let mut truncated = false;
for tok in tokenize(s.as_bytes(), self.mode, self.eaw_wide) {
if !matches!(tok, Token::Text { width: 0, .. })
&& let Some((px, py, content, w)) = pending.take()
&& clip.contains(Position::new(px, py))
{
let cell = if w == 2 {
Cell::wide(&content)
} else {
Cell::narrow(&content)
};
self.target
.set_cell(Position::new(px, py), &cell.style(pen.inherit(&base)));
}
match tok {
Token::Text { text, width: 0 } => {
if let Some((_, _, ref mut content, _)) = pending {
content.push_str(unsafe { std::str::from_utf8_unchecked(text) });
}
}
Token::Text { text, width } => {
if truncated {
continue;
}
let g = unsafe { std::str::from_utf8_unchecked(text) };
let cw = width as u8;
if x + cw as u16 > clip.right() {
match wrap {
WrapMode::Truncate => {
if let Some(tail) = tail {
self.paint_tail(tail, clip, y);
x = clip.right();
}
truncated = true;
continue;
}
WrapMode::Wrap => {
y = y.saturating_add(1);
x = clip.left();
if y >= clip.bottom() {
return Position::new(x, y);
}
if x + cw as u16 > clip.right() {
return Position::new(x, y);
}
}
}
}
pending = Some((x, y, g.to_string(), cw));
x += cw as u16;
}
Token::Escape(seq) => {
if seq.last() == Some(&b'm')
&& let Some(body) = csi_body(seq)
{
read_style(Params::from_raw(body), &mut pen);
} else if let Some(body) = osc_body(seq)
&& let Some((params, url)) = parse_hyperlink(body)
{
pen = pen.link(url, params);
}
}
Token::Control(0x0A) => {
y = y.saturating_add(1);
x = clip.left();
truncated = false;
if y >= clip.bottom() {
return Position::new(x, y);
}
}
Token::Control(0x0D) => {
x = clip.left();
truncated = false;
}
Token::Control(_) => {}
}
}
if let Some((px, py, content, w)) = pending.take()
&& clip.contains(Position::new(px, py))
{
let cell = if w == 2 {
Cell::wide(&content)
} else {
Cell::narrow(&content)
};
self.target
.set_cell(Position::new(px, py), &cell.style(pen.inherit(&base)));
}
Position::new(x, y)
}
}
impl<'s, S: TextSurface + ?Sized> Bounded for Painter<'s, S> {
fn bounds(&self) -> Rect {
self.target.bounds()
}
}
impl<'s, S: TextSurface + ?Sized> Surface for Painter<'s, S> {
fn cell(&self, pos: Position) -> Option<&Cell> {
self.target.cell(pos)
}
}
impl<'s, S: TextSurface + ?Sized> SurfaceMut for Painter<'s, S> {
fn set_cell(&mut self, pos: Position, cell: &Cell) {
self.target.set_cell(pos, cell);
}
fn cell_mut(&mut self, pos: Position) -> Option<&mut Cell> {
self.target.cell_mut(pos)
}
fn insert_lines(&mut self, y: u16, count: u16, bounds_bottom: u16, fill: &Cell) {
self.target.insert_lines(y, count, bounds_bottom, fill);
}
fn delete_lines(&mut self, y: u16, count: u16, bounds_bottom: u16, fill: &Cell) {
self.target.delete_lines(y, count, bounds_bottom, fill);
}
fn insert_cells(&mut self, pos: Position, count: u16, bounds_right: u16, fill: &Cell) {
self.target.insert_cells(pos, count, bounds_right, fill);
}
fn delete_cells(&mut self, pos: Position, count: u16, bounds_right: u16, fill: &Cell) {
self.target.delete_cells(pos, count, bounds_right, fill);
}
}
impl<'s, S: TextSurface + ?Sized> TextSurface for Painter<'s, S> {
fn width_mode(&self) -> WidthMode {
self.mode
}
fn eaw_wide(&self) -> bool {
self.eaw_wide
}
fn str_width(&self, s: &str) -> u16 {
string_width(s.as_bytes(), self.mode, self.eaw_wide).min(u16::MAX as usize) as u16
}
fn set_str(&mut self, pos: impl Into<Position>, s: &str, style: impl Into<Style>) -> Position {
let clip = self.target.bounds();
self.paint(pos.into(), clip, s, WrapMode::default(), style.into())
}
fn set_str_wrap(
&mut self,
pos: impl Into<Position>,
s: &str,
wrap: WrapMode,
style: impl Into<Style>,
) -> Position {
let clip = self.target.bounds();
self.paint(pos.into(), clip, s, wrap, style.into())
}
fn set_str_rect(
&mut self,
rect: impl Into<Rect>,
s: &str,
style: impl Into<Style>,
) -> Position {
let rect = rect.into();
let clip = rect.intersection(self.target.bounds());
self.paint(rect.position(), clip, s, WrapMode::default(), style.into())
}
fn set_str_rect_wrap(
&mut self,
rect: impl Into<Rect>,
s: &str,
wrap: WrapMode,
style: impl Into<Style>,
) -> Position {
let rect = rect.into();
let clip = rect.intersection(self.target.bounds());
self.paint(rect.position(), clip, s, wrap, style.into())
}
fn set_str_truncate(
&mut self,
pos: impl Into<Position>,
s: &str,
tail: &str,
tail_style: impl Into<Style>,
) -> Position {
let clip = self.target.bounds();
self.paint_truncate(
pos.into(),
clip,
s,
tail,
tail_style.into(),
Style::default(),
)
}
fn set_str_rect_truncate(
&mut self,
rect: impl Into<Rect>,
s: &str,
tail: &str,
tail_style: impl Into<Style>,
) -> Position {
let rect = rect.into();
let clip = rect.intersection(self.target.bounds());
self.paint_truncate(
rect.position(),
clip,
s,
tail,
tail_style.into(),
Style::default(),
)
}
}
#[derive(Clone, Copy)]
struct Tail<'a> {
text: &'a str,
style: &'a Style,
width: u16,
}
fn csi_body(seq: &[u8]) -> Option<&[u8]> {
let body_start = if seq.len() >= 2 && seq[0] == 0x1b && seq[1] == b'[' {
2
} else if !seq.is_empty() && seq[0] == 0x9b {
1
} else {
return None;
};
let last = *seq.last()?;
if !(0x40..=0x7e).contains(&last) || seq.len() <= body_start {
return None;
}
Some(&seq[body_start..seq.len() - 1])
}
fn osc_body(seq: &[u8]) -> Option<&[u8]> {
let body_start = if seq.len() >= 2 && seq[0] == 0x1b && seq[1] == b']' {
2
} else if !seq.is_empty() && seq[0] == 0x9d {
1
} else {
return None;
};
if seq.len() <= body_start {
return Some(&[]);
}
let end = if seq.ends_with(b"\x1b\\") {
seq.len() - 2
} else if matches!(seq.last(), Some(0x07 | 0x9c)) {
seq.len() - 1
} else {
seq.len()
};
if end < body_start {
return Some(&[]);
}
Some(&seq[body_start..end])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::buffer::{Surface, TextBuffer};
use crate::color::Color;
use crate::style::AttrFlags;
fn buf(width: u16, height: u16) -> TextBuffer {
TextBuffer::new(width, height)
}
fn cell_at(b: &TextBuffer, x: u16, y: u16) -> Cell {
b.cell(Position::new(x, y)).cloned().unwrap()
}
fn link_of(s: &crate::style::Style) -> Option<(&str, &str)> {
s.link
.as_deref()
.map(|l| (l.url.as_str(), l.params.as_str()))
}
#[test]
fn plain_text() {
let mut b = buf(10, 1);
let end =
Painter::new(&mut b).set_str_wrap((0, 0), "abc", WrapMode::Truncate, Style::default());
assert_eq!(end, Position::new(3, 0));
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 2, 0).content(), "c");
}
#[test]
fn sgr_updates_style_mid_stream() {
let mut b = buf(10, 1);
let mut p = Painter::new(&mut b);
let end = p.set_str_wrap(
(0, 0),
"a\x1b[1mb\x1b[mc",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(end, Position::new(3, 0));
let c0 = cell_at(&b, 0, 0);
let c1 = cell_at(&b, 1, 0);
let c2 = cell_at(&b, 2, 0);
assert!(!c0.style.attrs.contains(AttrFlags::BOLD));
assert!(c1.style.attrs.contains(AttrFlags::BOLD));
assert!(!c2.style.attrs.contains(AttrFlags::BOLD));
}
#[test]
fn sgr_color() {
let mut b = buf(5, 1);
Painter::new(&mut b).set_str_wrap(
(0, 0),
"\x1b[31mr",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 0, 0).style.fg, Some(Color::Red));
}
#[test]
fn osc8_toggles_link() {
let mut b = buf(10, 1);
Painter::new(&mut b).set_str_wrap(
(0, 0),
"\x1b]8;;https://x\x1b\\a\x1b]8;;\x1b\\b",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(link_of(&cell_at(&b, 0, 0).style), Some(("https://x", "")));
assert!(cell_at(&b, 1, 0).style.link.is_none());
}
#[test]
fn osc8_malformed_ignored() {
let mut b = buf(10, 1);
let mut p = Painter::new(&mut b);
p.set_str_wrap(
(0, 0),
"\x1b]8;;https://x\x1b\\a\x1b]8;garbage\x1b\\b",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(link_of(&cell_at(&b, 0, 0).style), Some(("https://x", "")));
assert_eq!(link_of(&cell_at(&b, 1, 0).style), Some(("https://x", "")));
}
#[test]
fn newline_advances_row() {
let mut b = buf(5, 3);
let end = Painter::new(&mut b).set_str_wrap(
(0, 0),
"ab\ncd",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 1, 0).content(), "b");
assert_eq!(cell_at(&b, 0, 1).content(), "c");
assert_eq!(cell_at(&b, 1, 1).content(), "d");
assert_eq!(end, Position::new(2, 1));
}
#[test]
fn cr_returns_to_left() {
let mut b = buf(5, 1);
Painter::new(&mut b).set_str_wrap((0, 0), "abc\rXY", WrapMode::Truncate, Style::default());
assert_eq!(cell_at(&b, 0, 0).content(), "X");
assert_eq!(cell_at(&b, 1, 0).content(), "Y");
assert_eq!(cell_at(&b, 2, 0).content(), "c");
}
#[test]
fn newline_past_bottom_returns() {
let mut b = buf(5, 2);
let end = Painter::new(&mut b).set_str_wrap(
(0, 0),
"a\nb\nc",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(end, Position::new(0, 2));
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 0, 1).content(), "b");
}
#[test]
fn truncate_at_right_edge() {
let mut b = buf(3, 1);
let end = Painter::new(&mut b).set_str_wrap(
(0, 0),
"abcdef",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(end, Position::new(3, 0));
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 2, 0).content(), "c");
}
#[test]
fn truncate_resumes_on_next_row() {
let mut b = buf(3, 2);
let end = Painter::new(&mut b).set_str_wrap(
(0, 0),
"abcdef\nxy",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 2, 0).content(), "c");
assert_eq!(cell_at(&b, 0, 1).content(), "x");
assert_eq!(cell_at(&b, 1, 1).content(), "y");
assert_eq!(end, Position::new(2, 1));
}
#[test]
fn truncate_tail_stamped_per_row() {
let mut b = buf(4, 2);
Painter::new(&mut b).set_str_truncate((0, 0), "abcdef\nghijkl", "…", Style::default());
assert_eq!(cell_at(&b, 3, 0).content(), "…");
assert_eq!(cell_at(&b, 0, 1).content(), "g");
assert_eq!(cell_at(&b, 3, 1).content(), "…");
}
#[test]
fn literal_truncate_resumes_on_next_row() {
let mut b = buf(3, 2);
b.set_str((0, 0), "abcdef\nxy", Style::default());
assert_eq!(cell_at(&b, 2, 0).content(), "c");
assert_eq!(cell_at(&b, 0, 1).content(), "x");
assert_eq!(cell_at(&b, 1, 1).content(), "y");
}
#[test]
fn cr_clears_truncation_for_the_row() {
let mut b = buf(3, 1);
Painter::new(&mut b).set_str_wrap(
(0, 0),
"abcdef\rXY",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 0, 0).content(), "X");
assert_eq!(cell_at(&b, 1, 0).content(), "Y");
assert_eq!(cell_at(&b, 2, 0).content(), "c");
}
#[test]
fn overflow_drops_rest_of_row_without_backfill() {
let mut b = buf(3, 1);
let end = Painter::new(&mut b).set_str_wrap(
(0, 0),
"ab中c",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 1, 0).content(), "b");
assert_eq!(cell_at(&b, 2, 0).content(), " ");
assert_eq!(end, Position::new(2, 0));
}
#[test]
fn literal_crlf_breaks_the_line() {
let mut b = buf(3, 2);
b.set_str((0, 0), "abcdef\r\nxy", Style::default());
assert_eq!(cell_at(&b, 2, 0).content(), "c");
assert_eq!(cell_at(&b, 0, 1).content(), "x");
assert_eq!(cell_at(&b, 1, 1).content(), "y");
}
#[test]
fn crlf_breaks_the_line() {
let mut b = buf(3, 2);
Painter::new(&mut b).set_str_wrap(
(0, 0),
"abcdef\r\nxy",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 2, 0).content(), "c");
assert_eq!(cell_at(&b, 0, 1).content(), "x");
assert_eq!(cell_at(&b, 1, 1).content(), "y");
}
#[test]
fn escapes_still_apply_across_a_truncated_row() {
let mut b = buf(2, 2);
Painter::new(&mut b).set_str_wrap(
(0, 0),
"ab\x1b[1mcd\x1b]8;;https://x\x1b\\\nz",
WrapMode::Truncate,
Style::default(),
);
assert!(!cell_at(&b, 0, 0).style.attrs.contains(AttrFlags::BOLD));
let z = cell_at(&b, 0, 1);
assert_eq!(z.content(), "z");
assert!(z.style.attrs.contains(AttrFlags::BOLD));
assert_eq!(link_of(&z.style), Some(("https://x", "")));
}
#[test]
fn start_below_clip_paints_nothing() {
let mut b = buf(3, 2);
let end = Painter::new(&mut b).set_str_wrap(
(0, 5),
"abcdef\nghi",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(end, Position::new(0, 5));
for y in 0..2 {
for x in 0..3 {
assert_eq!(cell_at(&b, x, y).content(), " ");
}
}
}
#[test]
fn literal_start_below_clip_paints_nothing() {
let mut b = buf(3, 2);
let end = b.set_str((0, 5), "abcdef\nghi", Style::default());
assert_eq!(end, Position::new(0, 5));
assert_eq!(cell_at(&b, 0, 0).content(), " ");
assert_eq!(cell_at(&b, 0, 1).content(), " ");
}
#[test]
fn wrap_breaks_on_crlf() {
let mut b = buf(4, 3);
Painter::new(&mut b).set_str_wrap((0, 0), "ab\r\ncd", WrapMode::Wrap, Style::default());
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 1, 0).content(), "b");
assert_eq!(cell_at(&b, 0, 1).content(), "c");
assert_eq!(cell_at(&b, 1, 1).content(), "d");
}
#[test]
fn literal_wrap_breaks_on_crlf() {
let mut b = buf(4, 3);
b.set_str_wrap((0, 0), "ab\r\ncd", WrapMode::Wrap, Style::default());
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 1, 0).content(), "b");
assert_eq!(cell_at(&b, 0, 1).content(), "c");
assert_eq!(cell_at(&b, 1, 1).content(), "d");
}
#[test]
fn wrap_at_right_edge() {
let mut b = buf(3, 3);
let end =
Painter::new(&mut b).set_str_wrap((0, 0), "abcdef", WrapMode::Wrap, Style::default());
assert_eq!(end, Position::new(3, 1));
assert_eq!(cell_at(&b, 0, 0).content(), "a");
assert_eq!(cell_at(&b, 2, 0).content(), "c");
assert_eq!(cell_at(&b, 0, 1).content(), "d");
assert_eq!(cell_at(&b, 2, 1).content(), "f");
}
#[test]
fn rect_clip_and_origin() {
let mut b = buf(10, 5);
let end = Painter::new(&mut b).set_str_rect_wrap(
Rect::new(2, 1, 3, 2),
"abcdef",
WrapMode::Wrap,
Style::default(),
);
assert_eq!(end, Position::new(5, 2));
assert_eq!(cell_at(&b, 2, 1).content(), "a");
assert_eq!(cell_at(&b, 4, 1).content(), "c");
assert_eq!(cell_at(&b, 2, 2).content(), "d");
assert_eq!(cell_at(&b, 4, 2).content(), "f");
assert_eq!(cell_at(&b, 0, 0).content(), " ");
assert_eq!(cell_at(&b, 5, 1).content(), " ");
}
#[test]
fn rect_newline_uses_rect_left() {
let mut b = buf(10, 5);
Painter::new(&mut b).set_str_rect_wrap(
Rect::new(2, 1, 4, 3),
"ab\ncd",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(cell_at(&b, 2, 1).content(), "a");
assert_eq!(cell_at(&b, 3, 1).content(), "b");
assert_eq!(cell_at(&b, 2, 2).content(), "c");
assert_eq!(cell_at(&b, 3, 2).content(), "d");
assert_eq!(cell_at(&b, 0, 2).content(), " ");
}
#[test]
fn with_resets_style_and_link() {
let mut b = buf(10, 1);
Painter::new(&mut b).set_str_wrap(
(0, 0),
"a",
WrapMode::Truncate,
Style::default().bold().link("https://x", ""),
);
assert!(cell_at(&b, 0, 0).style.attrs.contains(AttrFlags::BOLD));
assert_eq!(link_of(&cell_at(&b, 0, 0).style), Some(("https://x", "")));
Painter::new(&mut b).set_str_wrap((1, 0), "b", WrapMode::Truncate, Style::default());
assert!(!cell_at(&b, 1, 0).style.attrs.contains(AttrFlags::BOLD));
assert!(cell_at(&b, 1, 0).style.link.is_none());
}
#[test]
fn calls_start_from_their_own_base() {
let mut b = buf(10, 1);
let mut p = Painter::new(&mut b);
p.set_str_wrap((0, 0), "\x1b[1ma", WrapMode::Truncate, Style::default());
p.set_str_wrap((1, 0), "b", WrapMode::Truncate, Style::default());
assert!(cell_at(&b, 0, 0).style.attrs.contains(AttrFlags::BOLD));
assert!(!cell_at(&b, 1, 0).style.attrs.contains(AttrFlags::BOLD));
}
#[test]
fn base_applies_and_inline_reset_returns_to_base() {
let mut b = buf(10, 1);
let base = Style::default().fg(Color::Red);
Painter::new(&mut b).set_str_wrap((0, 0), "a\x1b[1mb\x1b[0mc", WrapMode::Truncate, base);
let red = Some(Color::Red);
assert_eq!(cell_at(&b, 0, 0).style.fg, red);
assert!(!cell_at(&b, 0, 0).style.attrs.contains(AttrFlags::BOLD));
assert_eq!(cell_at(&b, 1, 0).style.fg, red);
assert!(cell_at(&b, 1, 0).style.attrs.contains(AttrFlags::BOLD));
assert_eq!(cell_at(&b, 2, 0).style.fg, red);
assert!(!cell_at(&b, 2, 0).style.attrs.contains(AttrFlags::BOLD));
}
#[test]
fn position_and_rect_match_when_rect_covers_bounds() {
let mut a = buf(5, 2);
let mut b = buf(5, 2);
let e1 =
Painter::new(&mut a).set_str_wrap((0, 0), "abc", WrapMode::Truncate, Style::default());
let e2 = Painter::new(&mut b).set_str_rect_wrap(
Rect::new(0, 0, 5, 2),
"abc",
WrapMode::Truncate,
Style::default(),
);
assert_eq!(e1, e2);
assert_eq!(cell_at(&a, 2, 0).content(), cell_at(&b, 2, 0).content());
}
fn row(b: &TextBuffer, y: u16) -> String {
(0..b.width())
.map(|x| cell_at(b, x, y).content().to_string())
.collect()
}
#[test]
fn truncate_tail_not_shown_when_text_fits() {
let mut b = buf(5, 1);
let end = Painter::new(&mut b).set_str_truncate((0, 0), "abc", "…", Style::default());
assert_eq!(row(&b, 0), "abc ");
assert_eq!(end, Position::new(3, 0));
}
#[test]
fn truncate_single_cell_tail_on_overflow() {
let mut b = buf(5, 1);
let end = Painter::new(&mut b).set_str_truncate((0, 0), "abcdefgh", "…", Style::default());
assert_eq!(row(&b, 0), "abcd…");
assert_eq!(end, Position::new(5, 0));
}
#[test]
fn truncate_multi_cell_tail_reserves_its_width() {
let mut b = buf(8, 1);
Painter::new(&mut b).set_str_truncate((0, 0), "abcdefghij", " more", Style::default());
assert_eq!(row(&b, 0), "abc more");
}
#[test]
fn truncate_tail_carries_its_style() {
let mut b = buf(5, 1);
Painter::new(&mut b).set_str_truncate(
(0, 0),
"abcdefgh",
"…",
Style::default().fg(Color::Red),
);
assert_eq!(cell_at(&b, 4, 0).style.fg, Some(Color::Red));
}
#[test]
fn truncate_tail_inline_escapes_apply() {
let mut b = buf(5, 1);
Painter::new(&mut b).set_str_truncate((0, 0), "abcdefgh", "\x1b[1m…", Style::default());
assert!(cell_at(&b, 4, 0).style.attrs.contains(AttrFlags::BOLD));
}
#[test]
fn truncate_wide_tail_too_big_hard_truncates() {
let mut b = buf(3, 1);
let end =
Painter::new(&mut b).set_str_truncate((0, 0), "abcdef", " more", Style::default());
assert_eq!(row(&b, 0), "abc");
assert_eq!(end, Position::new(3, 0));
}
#[test]
fn truncate_tail_overwrites_split_wide_cell() {
let mut b = buf(5, 1);
Painter::new(&mut b).set_str_truncate((0, 0), "ab中def", "…", Style::default());
assert_eq!(cell_at(&b, 4, 0).content(), "…");
assert!(!cell_at(&b, 3, 0).is_wide());
}
}