Skip to main content

euv_core/render/terminal/
fn.rs

1use super::*;
2
3/// Returns `true` when `ch` is a zero-width combining mark, format control, or variation selector.
4///
5/// # Arguments
6///
7/// - `char` - Unicode scalar value to classify.
8///
9/// # Returns
10///
11/// - `bool` - `true` when the scalar occupies zero terminal columns.
12#[must_use]
13pub const fn is_zero_width_combining(ch: char) -> bool {
14    let code: u32 = ch as u32;
15    matches!(
16        code,
17        0x0300..=0x036F
18            | 0x0483..=0x0489
19            | 0x0591..=0x05BD
20            | 0x05BF
21            | 0x05C1..=0x05C2
22            | 0x05C4..=0x05C5
23            | 0x05C7
24            | 0x0610..=0x061A
25            | 0x064B..=0x065F
26            | 0x0670
27            | 0x06D6..=0x06DC
28            | 0x06DF..=0x06E4
29            | 0x06E7..=0x06E8
30            | 0x06EA..=0x06ED
31            | 0x1AB0..=0x1AFF
32            | 0x1DC0..=0x1DFF
33            | 0x200B..=0x200F
34            | 0x202A..=0x202E
35            | 0x2060..=0x206F
36            | 0x20D0..=0x20FF
37            | 0xFE00..=0xFE0F
38            | 0xFE20..=0xFE2F
39            | 0xFEFF
40            | 0xE0100..=0xE01EF
41    )
42}
43
44/// Returns `true` when `ch` occupies two terminal columns (CJK ideographs, Hangul, fullwidth forms, and wide emoji).
45///
46/// # Arguments
47///
48/// - `char` - Unicode scalar value to classify.
49///
50/// # Returns
51///
52/// - `bool` - `true` when the scalar renders as double-width in standard terminals.
53#[must_use]
54pub const fn is_wide_char(ch: char) -> bool {
55    let code: u32 = ch as u32;
56    matches!(
57        code,
58        0x1100..=0x115F
59            | 0x2329..=0x232A
60            | 0x2E80..=0x303E
61            | 0x3041..=0x33FF
62            | 0x3400..=0x4DBF
63            | 0x4E00..=0xA4CF
64            | 0xA960..=0xA97F
65            | 0xAC00..=0xD7A3
66            | 0xF900..=0xFAFF
67            | 0xFE10..=0xFE19
68            | 0xFE30..=0xFE6F
69            | 0xFF01..=0xFF60
70            | 0xFFE0..=0xFFE6
71            | 0x1F300..=0x1FAFF
72            | 0x20000..=0x3FFFD
73    )
74}
75
76/// Computes the East-Asian / Unicode terminal cell display width of `ch` (`0`, `1`, or `2`).
77///
78/// # Arguments
79///
80/// - `char` - Unicode scalar value to measure.
81///
82/// # Returns
83///
84/// - `u8` - Display column width (`0` for control/combining marks, `2` for wide CJK/fullwidth/emoji, `1` otherwise).
85#[must_use]
86pub const fn terminal_char_width(ch: char) -> u8 {
87    let code: u32 = ch as u32;
88    if code <= 0x1F || (code >= 0x7F && code <= 0x9F) || is_zero_width_combining(ch) {
89        0
90    } else if is_wide_char(ch) {
91        2
92    } else {
93        1
94    }
95}
96
97/// Computes total terminal column width of a UTF-8 string across all scalar values.
98///
99/// # Arguments
100///
101/// - `&str` - UTF-8 text slice to measure.
102///
103/// # Returns
104///
105/// - `usize` - Sum of terminal display column widths.
106#[must_use]
107pub fn terminal_str_width(text: &str) -> usize {
108    let mut total: usize = 0;
109    for ch in text.chars() {
110        total = total.saturating_add(terminal_char_width(ch) as usize);
111    }
112    total
113}
114
115/// Appends an ANSI cursor-positioning CSI sequence (`ESC[{row+1};{col+1}H`) into `out`.
116///
117/// # Arguments
118///
119/// - `&mut String` - Destination ANSI output buffer.
120/// - `u16` - Zero-based column coordinate.
121/// - `u16` - Zero-based row coordinate.
122pub(crate) fn append_cursor_move(out: &mut String, col: u16, row: u16) {
123    let row_one: u32 = (row as u32) + 1;
124    let col_one: u32 = (col as u32) + 1;
125    out.push_str(&format!("{ANSI_CSI_PREFIX}{row_one};{col_one}H"));
126}
127
128/// Appends an ANSI SGR styling sequence for foreground, background, bold, reverse, and underline flags into `out`.
129///
130/// # Arguments
131///
132/// - `&mut String` - Destination ANSI output buffer.
133/// - `Option<Color>` - Optional foreground 24-bit color.
134/// - `Option<Color>` - Optional background 24-bit color.
135/// - `bool` - Bold flag.
136/// - `bool` - Reverse-video flag.
137/// - `bool` - Underline flag.
138pub(crate) fn append_sgr_style(
139    out: &mut String,
140    fg: Option<Color>,
141    bg: Option<Color>,
142    bold: bool,
143    reverse: bool,
144    underline: bool,
145) {
146    out.push_str(ANSI_CSI_PREFIX);
147    out.push('0');
148    if bold {
149        out.push_str(ANSI_BOLD_CODE);
150    }
151    if reverse {
152        out.push_str(ANSI_REVERSE_CODE);
153    }
154    if underline {
155        out.push_str(ANSI_UNDERLINE_CODE);
156    }
157    if let Some(color) = fg {
158        let r: u8 = color.get_r();
159        let g: u8 = color.get_g();
160        let b: u8 = color.get_b();
161        out.push_str(&format!("{ANSI_FG_TRUECOLOR_PREFIX}{r};{g};{b}"));
162    }
163    if let Some(color) = bg {
164        let r: u8 = color.get_r();
165        let g: u8 = color.get_g();
166        let b: u8 = color.get_b();
167        out.push_str(&format!("{ANSI_BG_TRUECOLOR_PREFIX}{r};{g};{b}"));
168    }
169    out.push('m');
170}