euv_core/render/terminal/
fn.rs1use super::*;
2
3#[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#[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#[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#[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
115pub(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
128pub(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}