use crate::ansi::{escape_len, Pieces};
use crate::grapheme::{first_cluster, Graphemes};
use crate::tables::{entry, WIDTH_MASK};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Ambiguous {
#[default]
Narrow,
Wide,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Control {
#[default]
Zero,
Caret,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Clusters {
#[default]
WholeGlyph,
CodePoints,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Width {
ambiguous: Ambiguous,
control: Control,
clusters: Clusters,
tab_stop: usize,
ansi: bool,
}
impl Default for Width {
fn default() -> Self {
Width::DEFAULT
}
}
impl Width {
pub const DEFAULT: Width = Width {
ambiguous: Ambiguous::Narrow,
control: Control::Zero,
clusters: Clusters::WholeGlyph,
tab_stop: 8,
ansi: true,
};
pub const MODERN: Width = Width::DEFAULT;
pub const LEGACY: Width = Width {
clusters: Clusters::CodePoints,
..Width::DEFAULT
};
pub const fn clusters(mut self, c: Clusters) -> Self {
self.clusters = c;
self
}
pub const fn ambiguous(mut self, a: Ambiguous) -> Self {
self.ambiguous = a;
self
}
pub const fn control(mut self, c: Control) -> Self {
self.control = c;
self
}
pub const fn tab_stop(mut self, n: usize) -> Self {
self.tab_stop = n;
self
}
pub const fn ansi(mut self, enabled: bool) -> Self {
self.ansi = enabled;
self
}
pub fn of_char(&self, c: char) -> usize {
let cp = c as u32;
if cp < 0x7F {
return if cp >= 0x20 {
1
} else if c == '\t' {
self.tab_stop
} else {
self.control_width()
};
}
if (0x7F..=0x9F).contains(&cp) {
return self.control_width();
}
if self.clusters == Clusters::CodePoints {
match cp {
0x1F3FB..=0x1F3FF => return 2,
0x1F1E6..=0x1F1FF => return 1,
_ => {}
}
}
match entry(cp) & WIDTH_MASK {
0 => 0,
2 => 2,
3 if self.ambiguous == Ambiguous::Wide => 2,
_ => 1,
}
}
fn control_width(&self) -> usize {
match self.control {
Control::Zero => 0,
Control::Caret => 2,
}
}
pub(crate) fn advance(&self, cluster: &str, col: usize) -> usize {
let bytes = cluster.as_bytes();
if bytes.len() == 1 {
let b = bytes[0];
if (0x20..0x7F).contains(&b) {
return 1;
}
if b == b'\t' {
return self.tab_advance(col);
}
return self.control_width();
}
if self.clusters == Clusters::CodePoints {
return cluster.chars().map(|c| self.of_char(c)).sum();
}
let mut sum = 0;
let mut first = None;
let mut ri = 0;
let mut modifier = false;
let mut vs16 = false;
let mut vs15 = false;
for c in cluster.chars() {
match c {
'\u{FE0F}' => vs16 = true,
'\u{FE0E}' => vs15 = true,
'\u{1F1E6}'..='\u{1F1FF}' => ri += 1,
'\u{1F3FB}'..='\u{1F3FF}' => modifier = true,
_ => {}
}
let w = self.of_char(c);
sum += w;
first.get_or_insert(w);
}
if ri == 2 {
return 2;
}
if vs16 || modifier {
return 2;
}
if vs15 {
return 1;
}
first.filter(|&w| w > 0).unwrap_or(sum)
}
fn tab_advance(&self, col: usize) -> usize {
if self.tab_stop == 0 {
0
} else {
self.tab_stop - (col % self.tab_stop)
}
}
pub fn of(&self, s: &str) -> usize {
self.of_at(s, 0)
}
pub fn of_at(&self, s: &str, start: usize) -> usize {
let mut col = start;
if !self.ansi {
return self.of_text(s, start);
}
for piece in Pieces::new(s) {
if let crate::ansi::Piece::Text(t) = piece {
col += self.of_text(t, col);
}
}
col - start
}
fn of_text(&self, s: &str, start_col: usize) -> usize {
let mut col = start_col;
if s.is_ascii() && s.bytes().all(|b| (0x20..0x7F).contains(&b)) {
return s.len();
}
for cluster in Graphemes::new(s) {
col += self.advance(cluster, col);
}
col - start_col
}
pub fn of_grapheme(&self, cluster: &str) -> usize {
self.advance(cluster, 0)
}
#[cfg(feature = "alloc")]
pub(crate) fn ansi_enabled(&self) -> bool {
self.ansi
}
pub(crate) fn escape_at(&self, s: &str, i: usize) -> Option<usize> {
if self.ansi {
escape_len(s.as_bytes(), i)
} else {
None
}
}
pub fn truncate<'a>(&self, s: &'a str, max: usize) -> &'a str {
let mut i = 0;
let mut col = 0;
while i < s.len() {
if let Some(len) = self.escape_at(s, i) {
i += len;
continue;
}
let cluster = first_cluster(&s[i..]);
let w = self.advance(cluster, col);
if col + w > max {
break;
}
col += w;
i += cluster.len();
}
&s[..i]
}
}