Debian Codegraph @

src/utils/formatting/content_split/custom_styling.rs

raw symbols SCIP
1 use ansi_strrust-analyzer cargo ansi-str 0.9.0 crate/::AnsiStrrust-analyzer cargo ansi-str 0.9.0 AnsiStr#;
2 use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#;
3 use unicode_widthrust-analyzer cargo unicode-width 0.2.2 crate/::{UnicodeWidthCharrust-analyzer cargo unicode-width 0.2.2 UnicodeWidthChar#, UnicodeWidthStrrust-analyzer cargo unicode-width 0.2.2 UnicodeWidthStr#};
4
5 const ANSI_RESETrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/ANSI_RESET.: &str = "\u{1b}[0m";
6
7 /// Returns printed length of string, takes into account escape codes
8 #[inline(always)]
9 pub fn measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().(s: &str) -> usize {
10 s.ansi_striprust-analyzer cargo ansi-str 0.9.0 impl#[str][AnsiStr]ansi_strip().().widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().()
11 }
12
13 /// Split the line by the given deliminator without breaking ansi codes that contain the delimiter
14 pub fn split_line_by_delimiterrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_line_by_delimiter().(line: &str, delimiter: char) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
15 let mut lines: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
16 let mut current_line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::defaultrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Default]default().();
17
18 // Iterate over line, splitting text with delimiter
19 let iter = consolerust-analyzer cargo console 0.16.1 crate/::AnsiCodeIteratorrust-analyzer cargo console 0.16.1 ansi/AnsiCodeIterator#::newrust-analyzer cargo console 0.16.1 ansi/impl#[`AnsiCodeIterator<'a>`]new().(line);
20 for (str_slice, is_esc) in iter {
21 if is_esc {
22 current_line.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(str_slice);
23 } else {
24 let mut split = str_slice.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(delimiter);
25
26 // Text before first delimiter (if any) belongs to previous line
27 let first = split
28 .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().()
29 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("split always produces at least one value");
30 current_line.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(first);
31
32 // Text after each delimiter goes to new line.
33 for text in split {
34 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
35 current_line = text.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
36 }
37 }
38 }
39 lines.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(current_line);
40 fix_style_in_split_strrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/fix_style_in_split_str().(lines.as_mutrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`AsMut<[T]>`]as_mut().());
41 lines
42 }
43
44 /// Splits a long word at a given character width. Inserting the needed ansi codes to preserve
45 /// style.
46 pub fn split_long_wordrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_long_word().(allowed_width: usize, word: &str) -> (Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
47 // A buffer for the first half of the split str, which will take up at most `allowed_len`
48 // characters when printed to the terminal.
49 let mut head = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(word.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
50 // A buffer for the second half of the split str
51 let mut tail = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(word.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
52 // Tracks the len() of head
53 let mut head_len = 0;
54 // Tracks the len() of head, sans trailing ansi escape codes
55 let mut head_len_last = 0;
56 // Count of *non-trailing* escape codes in the buffer.
57 let mut escape_count_last = 0;
58 // A buffer for the escape codes that exist in the str before the split.
59 let mut escapes = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
60
61 // Iterate over segments of the input string, each segment is either a singe escape code or
62 // block of text containing no escape codes. Add text and escape codes to the head buffer,
63 // keeping track of printable length and what ansi codes are active, until there is no more room
64 // in allowed_width. If the str was split at a point with active escape-codes, add the ansi
65 // reset code to the end of head, and the list of active escape codes to the beginning of tail.
66 let mut iter = consolerust-analyzer cargo console 0.16.1 crate/::AnsiCodeIteratorrust-analyzer cargo console 0.16.1 ansi/AnsiCodeIterator#::newrust-analyzer cargo console 0.16.1 ansi/impl#[`AnsiCodeIterator<'a>`]new().(word);
67 for (str_slice, is_esc) in iter.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().() {
68 if is_esc {
69 escapes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(str_slice);
70 // If the code is reset, that means all current codes in the buffer can be ignored.
71 if str_slice rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().ANSI_RESETrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/ANSI_RESET. {
72 escapes.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().();
73 }
74 }
75
76 let slice_len = match is_esc {
77 true => 0,
78 false => str_slice.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().(),
79 };
80
81 if head_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().slice_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().allowed_width {
82 head.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(str_slice);
83 head_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().slice_len;
84
85 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_esc {
86 // allows popping unneeded escape codes later
87 head_len_last = head.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().();
88 escape_count_last = escapes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
89 }
90 } else {
91 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_esc);
92 let mut graphmes = str_slice.graphemesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]graphemes().(true).peekablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#peekable().();
93 while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c) = graphmes.peekrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`]peek().() {
94 let character_width = c.widthrust-analyzer cargo unicode-width 0.2.2 impl#[str][UnicodeWidthStr]width().();
95 if allowed_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().head_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().character_width {
96 break;
97 }
98
99 head_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().character_width;
100 let c = graphmes.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/peekable/impl#[`Peekable<I>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
101 head.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(c);
102
103 // c is not escape code
104 head_len_last = head.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().();
105 escape_count_last = escapes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
106 }
107
108 // cut off dangling escape codes since they should have no effect
109 head.truncaterust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]truncate().(head_len_last);
110 if escape_count_last rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 {
111 head.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(ANSI_RESETrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/ANSI_RESET.);
112 }
113
114 for esc in escapes {
115 tail.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(esc);
116 }
117 let remaining: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = graphmes.collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
118 tail.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&remaining);
119 break;
120 }
121 }
122
123 iter.for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|s| tail.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(s.0));
124 (head, tail)
125 }
126
127 /// Fixes ansi escape codes in a split string
128 /// 1. Adds reset code to the end of each substring if needed.
129 /// 2. Keeps track of previous substring's escape codes and inserts them in later substrings to
130 /// continue style
131 pub fn fix_style_in_split_strrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/fix_style_in_split_str().(words: &mut [Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#]) {
132 let mut escapes: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
133
134 for word in words {
135 // before we modify the escape list, make a copy
136 let prepend = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().escapes.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
137 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(escapes.joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(""))
138 } else {
139 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
140 };
141
142 // add escapes in word to escape list
143 let iter = consolerust-analyzer cargo console 0.16.1 crate/::AnsiCodeIteratorrust-analyzer cargo console 0.16.1 ansi/AnsiCodeIterator#::newrust-analyzer cargo console 0.16.1 ansi/impl#[`AnsiCodeIterator<'a>`]new().(word)
144 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|(_, is_esc)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().is_esc)
145 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| v.0);
146 for esc in iter {
147 if esc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().ANSI_RESETrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/ANSI_RESET. {
148 escapes.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]clear().()
149 } else {
150 escapes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(esc.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())
151 }
152 }
153
154 // insert previous esc sequences at the beginning of the segment
155 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(prepend) = prepend {
156 word.insert_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]insert_str().(0, &prepend);
157 }
158
159 // if there are active escape sequences, we need to append reset
160 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().escapes.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
161 word.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(ANSI_RESETrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/ANSI_RESET.);
162 }
163 }
164 }
165
166 #[cfg(test)]
167 mod testrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/test/ {
168 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
169 fn ansi_aware_split_testrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/test/ansi_aware_split_test().() {
170 use superrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/::split_line_by_delimiterrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_line_by_delimiter().;
171
172 let text = "\u{1b}[1m head [ middle [ tail \u{1b}[0m[ after";
173 let split = split_line_by_delimiterrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/split_line_by_delimiter().(text, '[');
174
175 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
176 split,
177 [
178 "\u{1b}[1m head \u{1b}[0m",
179 "\u{1b}[1m middle \u{1b}[0m",
180 "\u{1b}[1m tail \u{1b}[0m",
181 " after"
182 ]
183 )
184 }
185
186 // TODO: Figure out why this fails with the custom_styling feature enabled.
187 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
188 #[cfg(not(feature = "custom_styling"))]
189 fn measure_text_width_osc8_test() {
190 use unicode_widthrust-analyzer cargo unicode-width 0.2.2 crate/::UnicodeWidthStrrust-analyzer cargo unicode-width 0.2.2 UnicodeWidthStr#;
191
192 use superrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/::measure_text_widthrust-analyzer cargo comfy-table 7.2.2 utils/formatting/content_split/custom_styling/measure_text_width().;
193
194 let text = "\x1b]8;;https://github.com\x1b\\This is a link\x1b]8;;\x1b";
195 let width = measure_text_width(text);
196
197 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(text.width(), 41);
198 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(width, 14);
199 }
200 }