Skip to main content

crate_cli/fmt/text/
fn.rs

1use super::*;
2
3/// Normalizes CRLF and lone CR line endings to LF (`\n`).
4///
5/// # Arguments
6///
7/// - `&str` - Raw input text with arbitrary line endings.
8///
9/// # Returns
10///
11/// - `String` - Text with LF-only line endings.
12pub fn normalize_line_endings(input: &str) -> String {
13    if !input.contains(TEXT_CR) {
14        return input.to_string();
15    }
16    let replaced_crlf: String = input.replace("\r\n", "\n");
17    replaced_crlf.replace(TEXT_CR, "\n")
18}
19
20/// Formats generic plain text files with LF line endings, trimmed trailing whitespace, and a single trailing newline.
21///
22/// # Arguments
23///
24/// - `&str` - Raw file content.
25/// - `bool` - When `true`, preserves leading tabs (for example in `Makefile`) while stripping trailing whitespace.
26///
27/// # Returns
28///
29/// - `String` - Normalized text content.
30pub fn format_plain_text(input: &str, preserve_tabs: bool) -> String {
31    let normalized: String = normalize_line_endings(input);
32    let mut lines: Vec<String> = Vec::new();
33    for raw_line in normalized.split(TEXT_LF) {
34        let trimmed: &str = raw_line.trim_end_matches([TEXT_SPACE, TEXT_TAB]);
35        if preserve_tabs {
36            lines.push(trimmed.to_string());
37        } else {
38            lines.push(expand_leading_tabs(trimmed));
39        }
40    }
41    while let Some(last) = lines.last() {
42        if last.is_empty() {
43            let _: Option<String> = lines.pop();
44        } else {
45            break;
46        }
47    }
48    if lines.is_empty() {
49        return String::new();
50    }
51    let mut out: String = lines.join("\n");
52    out.push(TEXT_LF);
53    out
54}
55
56/// Expands leading tab characters into `PRETTIER_TAB_WIDTH` spaces while keeping interior characters intact.
57///
58/// # Arguments
59///
60/// - `&str` - Single line without trailing whitespace.
61///
62/// # Returns
63///
64/// - `String` - Line with leading tabs converted to spaces.
65fn expand_leading_tabs(line: &str) -> String {
66    if !line.starts_with(TEXT_TAB) {
67        return line.to_string();
68    }
69    let mut out: String = String::with_capacity(line.len() + 8);
70    let mut in_leading: bool = true;
71    for ch in line.chars() {
72        if in_leading && ch == TEXT_TAB {
73            push_spaces(&mut out, PRETTIER_TAB_WIDTH);
74        } else {
75            in_leading = false;
76            out.push(ch);
77        }
78    }
79    out
80}
81
82/// Computes the terminal/Prettier column display width of a single Unicode scalar value.
83///
84/// # Arguments
85///
86/// - `char` - Unicode character to measure.
87///
88/// # Returns
89///
90/// - `usize` - Display width (`0` for combining marks, `2` for East Asian wide/fullwidth/emoji characters, `1` otherwise).
91pub fn char_display_width(ch: char) -> usize {
92    let code: u32 = ch as u32;
93    if code < 0x20 || (0x7F..0xA0).contains(&code) {
94        return 0;
95    }
96    if (0x0300..=0x036F).contains(&code)
97        || (0x1AB0..=0x1AFF).contains(&code)
98        || (0x1DC0..=0x1DFF).contains(&code)
99        || (0x20D0..=0x20FF).contains(&code)
100        || (0xFE00..=0xFE0F).contains(&code)
101        || (0xFE20..=0xFE2F).contains(&code)
102        || code == 0x200D
103    {
104        return 0;
105    }
106    if is_wide_codepoint(code) {
107        return 2;
108    }
109    1
110}
111
112/// Computes the Prettier column display width of a string (CJK and emoji wide characters count as 2).
113///
114/// # Arguments
115///
116/// - `&str` - String slice to measure.
117///
118/// # Returns
119///
120/// - `usize` - Total column display width.
121pub fn str_display_width(text: &str) -> usize {
122    let mut width: usize = 0;
123    for ch in text.chars() {
124        width += char_display_width(ch);
125    }
126    width
127}
128
129/// Returns `true` when `code` falls inside East Asian Wide, Fullwidth, or wide Emoji Unicode ranges.
130///
131/// # Arguments
132///
133/// - `u32` - Unicode code point.
134///
135/// # Returns
136///
137/// - `bool` - Whether the character occupies two display columns.
138fn is_wide_codepoint(code: u32) -> bool {
139    (0x1100..=0x115F).contains(&code)
140        || (0x2329..=0x232A).contains(&code)
141        || (0x2E80..=0x303E).contains(&code)
142        || (0x3040..=0xA4CF).contains(&code)
143        || (0xAC00..=0xD7A3).contains(&code)
144        || (0xF900..=0xFAFF).contains(&code)
145        || (0xFE10..=0xFE19).contains(&code)
146        || (0xFE30..=0xFE6F).contains(&code)
147        || (0xFF00..=0xFF60).contains(&code)
148        || (0xFFE0..=0xFFE6).contains(&code)
149        || (0x1F300..=0x1FAFF).contains(&code)
150        || (0x20000..=0x2FFFD).contains(&code)
151        || (0x30000..=0x3FFFD).contains(&code)
152}
153
154/// Repeats spaces `count` times into `out`.
155///
156/// # Arguments
157///
158/// - `&mut String` - Destination string buffer.
159/// - `usize` - Number of ASCII space characters to append.
160pub(crate) fn push_spaces(out: &mut String, count: usize) {
161    for _ in 0..count {
162        out.push(TEXT_SPACE);
163    }
164}