Debian Codegraph @

src/ansi/mod.rs

raw symbols SCIP
1 mod console_testsrust-analyzer cargo git-delta 0.19.2 ansi/console_tests/;
2 mod iteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/;
3
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#;
5
6 use ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#;
7 use itertoolsrust-analyzer cargo itertools 0.14.0 crate/::Itertoolsrust-analyzer cargo itertools 0.14.0 Itertools#;
8 use unicode_segmentationrust-analyzer cargo unicode-segmentation 1.12.0 crate/::UnicodeSegmentationrust-analyzer cargo unicode-segmentation 1.12.0 UnicodeSegmentation#;
9 use unicode_widthrust-analyzer cargo unicode-width 0.1.14 crate/::UnicodeWidthStrrust-analyzer cargo unicode-width 0.1.14 UnicodeWidthStr#;
10
11 use iteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/::{AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#, Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#};
12
13 pub const ANSI_CSI_CLEAR_TO_EOLrust-analyzer cargo git-delta 0.19.2 ansi/ANSI_CSI_CLEAR_TO_EOL.: &str = "\x1b[0K";
14 pub const ANSI_CSI_CLEAR_TO_BOLrust-analyzer cargo git-delta 0.19.2 ansi/ANSI_CSI_CLEAR_TO_BOL.: &str = "\x1b[1K";
15 pub const ANSI_SGR_BOLDrust-analyzer cargo git-delta 0.19.2 ansi/ANSI_SGR_BOLD.: &str = "\x1b[1m";
16 pub const ANSI_SGR_RESETrust-analyzer cargo git-delta 0.19.2 ansi/ANSI_SGR_RESET.: &str = "\x1b[0m";
17 pub const ANSI_SGR_REVERSErust-analyzer cargo git-delta 0.19.2 ansi/ANSI_SGR_REVERSE.: &str = "\x1b[7m";
18 pub const ANSI_SGR_UNDERLINErust-analyzer cargo git-delta 0.19.2 ansi/ANSI_SGR_UNDERLINE.: &str = "\x1b[4m";
19
20 pub fn strip_ansi_codesrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes().(s: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
21 strip_ansi_codes_from_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes_from_strings_iterator().(ansi_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/ansi_strings_iterator().(s))
22 }
23
24 pub fn measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().(s: &str) -> usize {
25 ansi_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/ansi_strings_iterator().(s).foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Iterator]fold().(0, |acc, (element, is_ansi)| {
26 acc 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().if is_ansi { 0 } else { element.widthrust-analyzer cargo unicode-width 0.1.14 impl#[str][UnicodeWidthStr]width().() }
27 })
28 }
29
30 fn truncate_str_implrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_impl().<'a>(
31 s: &'a str,
32 display_width: usize,
33 tail: &str,
34 fill2w: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<char>,
35 ) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str> {
36 let items = ansi_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/ansi_strings_iterator().(s).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(&str, bool)>>();
37 let width = strip_ansi_codes_from_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes_from_strings_iterator().(items.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().copiedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#copied().()).widthrust-analyzer cargo unicode-width 0.1.14 impl#[str][UnicodeWidthStr]width().();
38 if width 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().display_width {
39 return Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'a, str>`][`From<&'a str>`]from().(s);
40 }
41 let result_tail = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().tail.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
42 truncate_str_implrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_impl().(tail, display_width, "", fill2w).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
43 } else {
44 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().()
45 };
46 let mut used = measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().(&result_tail);
47 let mut result = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
48 for (t, is_ansi) in items {
49 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_ansi {
50 for g in t.graphemesrust-analyzer cargo unicode-segmentation 1.12.0 impl#[str][UnicodeSegmentation]graphemes().(true) {
51 let width_of_grapheme = g.widthrust-analyzer cargo unicode-width 0.1.14 impl#[str][UnicodeWidthStr]width().();
52 if used 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().width_of_grapheme rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().display_width {
53 // Handle case "2." mentioned in `truncate_str` docs and fill the
54 // hole left by double-width (2w) truncation.
55 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(fillchar) = fill2w {
56 if width_of_grapheme rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().2 && used 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().display_width {
57 result.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(fillchar);
58 } else if width_of_grapheme rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().2 {
59 // Should not happen, this means either unicode_segmentation
60 // graphemes are too wide, or the unicode_width is calculated wrong.
61 // Fallback:
62 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(width_of_grapheme <= 2, "strange grapheme width");
63 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#display_width.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(used) {
64 result.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(fillchar);
65 }
66 }
67 }
68 break;
69 }
70 result.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(g);
71 used 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().width_of_grapheme;
72 }
73 } else {
74 result.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(t);
75 }
76 }
77
78 result.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&result_tail);
79 Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`Cow<'a, str>`][`From<String>`]from().(result)
80 }
81
82 /// Truncate string such that `tail` is present as a suffix, preceded by as much of `s` as can be
83 /// displayed in the requested width. Even with `tail` empty the result may not be a prefix of `s`.
84 // Return string constructed as follows:
85 // 1. `display_width` characters are available. If the string fits, return it.
86 //
87 // 2. If a double-width (fullwidth) grapheme has to be cut in the following steps, replace the first
88 // half with a space (' '). If this happens the result is no longer a prefix of the input.
89 //
90 // 3. Contribute graphemes and ANSI escape sequences from `tail` until either (1) `tail` is
91 // exhausted, or (2) the display width of the result would exceed `display_width`.
92 //
93 // 4. If tail was exhausted, then contribute graphemes and ANSI escape sequences from `s` until the
94 // display_width of the result would exceed `display_width`.
95 pub fn truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().<'a>(s: &'a str, display_width: usize, tail: &str) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'a, str> {
96 truncate_str_implrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_impl().(s, display_width, tail, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(' '))
97 }
98
99 /// Truncate string `s` so it fits into `display_width`, ignoring any ANSI escape sequences when
100 /// calculating the width. If a double-width ("fullwidth") grapheme has to be cut, it is omitted and
101 /// the resulting string is *shorter* than `display_width`. But this way the result is always a
102 /// prefix of the input `s`.
103 pub fn truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(s: &str, display_width: usize) -> Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<'_, str> {
104 truncate_str_implrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_impl().(s, display_width, "", Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
105 }
106
107 pub fn parse_style_sectionsrust-analyzer cargo git-delta 0.19.2 ansi/parse_style_sections().(s: &str) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#, &str)> {
108 let mut sections = 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().();
109 let mut curr_style = Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#::defaultrust-analyzer cargo nu-ansi-term 0.50.3 style/impl#[Style][Default]default().();
110 for element in AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#::newrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'a>`]new().(s) {
111 match element {
112 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Textrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Text#(start, end) => sections.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((curr_style, &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)),
113 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Sgrrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Sgr#(style, _, _) => curr_style = style,
114 _ => {}
115 }
116 }
117 sections
118 }
119
120 // Return the first CSI element, if any, as an `ansi_term::Style`.
121 pub fn parse_first_stylerust-analyzer cargo git-delta 0.19.2 ansi/parse_first_style().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#> {
122 AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#::newrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'a>`]new().(s).find_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find_map().(|el| match el {
123 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Sgrrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Sgr#(style, _, _) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(style),
124 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
125 })
126 }
127
128 pub fn string_starts_with_ansi_style_sequencerust-analyzer cargo git-delta 0.19.2 ansi/string_starts_with_ansi_style_sequence().(s: &str) -> bool {
129 AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#::newrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'a>`]new().(s)
130 .nextrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'_>`][Iterator]next().()
131 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|el| matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(el, Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Sgrrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Sgr#(_, _, _)))
132 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(false)
133 }
134
135 /// Return string formed from a byte slice starting at byte position `start`, where the index
136 /// counts bytes in non-ANSI-escape-sequence content only. All ANSI escape sequences in the
137 /// original string are preserved.
138 pub fn ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().(s: &str, start: usize) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
139 AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#::newrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'a>`]new().(s)
140 .scanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#scan().(0, |index, element| {
141 // `index` is the index in non-ANSI-escape-sequence content.
142 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(match element {
143 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Sgrrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Sgr#(_, a, b) => &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().a..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#b]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
144 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Csirust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Csi#(a, b) => &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().a..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#b]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
145 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Escrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Esc#(a, b) => &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().a..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#b]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
146 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Oscrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Osc#(a, b) => &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().a..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#b]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,
147 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Textrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Text#(a, b) => {
148 let i = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().index;
149 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().index 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().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().a;
150 if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().index 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().start {
151 // This text segment ends before start, so contributes no bytes.
152 ""
153 } else if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().start {
154 // This section starts after `start`, so contributes all its bytes.
155 &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().a..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#b]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
156 } else {
157 // This section contributes those bytes that are >= start
158 &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().(a 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().start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().i)..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#b]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
159 }
160 }
161 })
162 })
163 .joinrust-analyzer cargo itertools 0.14.0 Itertools#join().("")
164 }
165
166 /// Return the byte index in `s` of the i-th text byte in `s`. I.e. `i` counts
167 /// bytes in non-ANSI-escape-sequence content only.
168 pub fn ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(s: &str, i: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
169 let mut index = 0;
170 for element in AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#::newrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'a>`]new().(s) {
171 if let Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Textrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Text#(a, b) = element {
172 index 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().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().a;
173 if index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().i {
174 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().(index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().i));
175 }
176 }
177 }
178 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
179 }
180
181 fn ansi_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/ansi_strings_iterator().(s: &str) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = (&str, bool)> {
182 AnsiElementIteratorrust-analyzer cargo git-delta 0.19.2 ansi/iterator/AnsiElementIterator#::newrust-analyzer cargo git-delta 0.19.2 ansi/iterator/impl#[`AnsiElementIterator<'a>`]new().(s).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(move |el| match el {
183 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Sgrrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Sgr#(_, i, j) => (&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#j]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., true),
184 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Csirust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Csi#(i, j) => (&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#j]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., true),
185 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Escrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Esc#(i, j) => (&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#j]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., true),
186 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Oscrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Osc#(i, j) => (&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#j]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., true),
187 Elementrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#::Textrust-analyzer cargo git-delta 0.19.2 ansi/iterator/Element#Text#(i, j) => (&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#j]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., false),
188 })
189 }
190
191 fn strip_ansi_codes_from_strings_iteratorrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes_from_strings_iterator().<'a>(
192 strings: impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = (&'a str, bool)>,
193 ) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
194 strings
195 .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|(el, is_ansi)| if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_ansi { Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(el) } else { Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# })
196 .joinrust-analyzer cargo itertools 0.14.0 Itertools#join().("")
197 }
198
199 pub fn explain_ansirust-analyzer cargo git-delta 0.19.2 ansi/explain_ansi().(line: &str, colorful: bool) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
200 use craterust-analyzer cargo git-delta 0.19.2 crate/::stylerust-analyzer cargo git-delta 0.19.2 style/::Stylerust-analyzer cargo git-delta 0.19.2 style/Style#;
201
202 parse_style_sectionsrust-analyzer cargo git-delta 0.19.2 ansi/parse_style_sections().(line)
203 .into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().()
204 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(ansi_term_style, s)| {
205 let style = Stylerust-analyzer cargo git-delta 0.19.2 style/Style# {
206 ansi_term_stylerust-analyzer cargo git-delta 0.19.2 style/Style#ansi_term_style.,
207 ..Stylerust-analyzer cargo git-delta 0.19.2 style/Style#::defaultrust-analyzer cargo git-delta 0.19.2 style/impl#[Style][Default]default().()
208 };
209 if colorful {
210 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("({}){}", style.to_painted_stringrust-analyzer cargo git-delta 0.19.2 style/impl#[Style]to_painted_string().(), style.paintrust-analyzer cargo git-delta 0.19.2 style/impl#[Style]paint().(s))
211 } else {
212 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("({style}){s}")
213 }
214 })
215 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
216 }
217
218 #[cfg(test)]
219 mod testsrust-analyzer cargo git-delta 0.19.2 ansi/tests/ {
220 use unicode_widthrust-analyzer cargo unicode-width 0.1.14 crate/::UnicodeWidthStrrust-analyzer cargo unicode-width 0.1.14 UnicodeWidthStr#;
221
222 // Note that src/ansi/console_tests.rs contains additional test coverage for this module.
223 use superrust-analyzer cargo git-delta 0.19.2 ansi/::{
224 ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index()., ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice()., measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width()., parse_first_stylerust-analyzer cargo git-delta 0.19.2 ansi/parse_first_style().,
225 string_starts_with_ansi_style_sequencerust-analyzer cargo git-delta 0.19.2 ansi/string_starts_with_ansi_style_sequence()., strip_ansi_codesrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes()., truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str()., truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().,
226 };
227
228 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
229 fn test_strip_ansi_codesrust-analyzer cargo git-delta 0.19.2 ansi/tests/test_strip_ansi_codes().() {
230 for s in &["src/ansi/mod.rs", "バー", "src/ansi/modバー.rs"] {
231 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_ansi_codesrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes().(s), *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().s);
232 }
233 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_ansi_codesrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes().("\x1b[31mバー\x1b[0m"), "バー");
234 }
235
236 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
237 fn test_measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/tests/test_measure_text_width().() {
238 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("src/ansi/mod.rs"), 15);
239 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("バー"), 4);
240 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("src/ansi/modバー.rs"), 19);
241 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("\x1b[31mバー\x1b[0m"), 4);
242 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("a\nb\n"), 2);
243 }
244
245 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
246 fn test_strip_ansi_codes_osc_hyperlinkrust-analyzer cargo git-delta 0.19.2 ansi/tests/test_strip_ansi_codes_osc_hyperlink().() {
247 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_ansi_codesrust-analyzer cargo git-delta 0.19.2 ansi/strip_ansi_codes().("\x1b[38;5;4m\x1b]8;;file:///Users/dan/src/delta/src/ansi/mod.rs\x1b\\src/ansi/mod.rs\x1b]8;;\x1b\\\x1b[0m\n"),
248 "src/ansi/mod.rs\n");
249 }
250
251 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
252 fn test_measure_text_width_osc_hyperlinkrust-analyzer cargo git-delta 0.19.2 ansi/tests/test_measure_text_width_osc_hyperlink().() {
253 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("\x1b[38;5;4m\x1b]8;;file:///Users/dan/src/delta/src/ansi/mod.rs\x1b\\src/ansi/mod.rs\x1b]8;;\x1b\\\x1b[0m"),
254 measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("src/ansi/mod.rs"));
255 }
256
257 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
258 fn test_measure_text_width_osc_hyperlink_non_asciirust-analyzer cargo git-delta 0.19.2 ansi/tests/test_measure_text_width_osc_hyperlink_non_ascii().() {
259 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("\x1b[38;5;4m\x1b]8;;file:///Users/dan/src/delta/src/ansi/mod.rs\x1b\\src/ansi/modバー.rs\x1b]8;;\x1b\\\x1b[0m"),
260 measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().("src/ansi/modバー.rs"));
261 }
262
263 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
264 fn test_parse_first_stylerust-analyzer cargo git-delta 0.19.2 ansi/tests/test_parse_first_style().() {
265 let minus_line_from_unconfigured_git = "\x1b[31m-____\x1b[m\n";
266 let style = parse_first_stylerust-analyzer cargo git-delta 0.19.2 ansi/parse_first_style().(minus_line_from_unconfigured_git);
267 let expected_style = ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style# {
268 foregroundrust-analyzer cargo nu-ansi-term 0.50.3 style/Style#foreground.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Colorrust-analyzer cargo nu-ansi-term 0.50.3 style/Color#::Redrust-analyzer cargo nu-ansi-term 0.50.3 style/Color#Red#),
269 ..ansi_termrust-analyzer cargo nu-ansi-term 0.50.3 crate/::Stylerust-analyzer cargo nu-ansi-term 0.50.3 style/Style#::defaultrust-analyzer cargo nu-ansi-term 0.50.3 style/impl#[Style][Default]default().()
270 };
271 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(expected_style), style);
272 }
273
274 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
275 fn test_string_starts_with_ansi_escape_sequencerust-analyzer cargo git-delta 0.19.2 ansi/tests/test_string_starts_with_ansi_escape_sequence().() {
276 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().string_starts_with_ansi_style_sequencerust-analyzer cargo git-delta 0.19.2 ansi/string_starts_with_ansi_style_sequence().(""));
277 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().string_starts_with_ansi_style_sequencerust-analyzer cargo git-delta 0.19.2 ansi/string_starts_with_ansi_style_sequence().("-"));
278 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(string_starts_with_ansi_style_sequencerust-analyzer cargo git-delta 0.19.2 ansi/string_starts_with_ansi_style_sequence().(
279 "\x1b[31m-XXX\x1b[m\n"
280 ));
281 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(string_starts_with_ansi_style_sequencerust-analyzer cargo git-delta 0.19.2 ansi/string_starts_with_ansi_style_sequence().("\x1b[32m+XXX"));
282 }
283
284 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
285 fn test_ansi_preserving_slice_and_indexrust-analyzer cargo git-delta 0.19.2 ansi/tests/test_ansi_preserving_slice_and_index().() {
286 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().("", 0), "");
287 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().("", 0), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
288
289 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().("0", 0), "0");
290 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().("0", 0), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0));
291
292 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().("0", 1), "");
293 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().("0", 1), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
294
295 let raw_string = "\x1b[1;35m0123456789\x1b[0m";
296 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
297 ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().(raw_string, 1),
298 "\x1b[1;35m123456789\x1b[0m"
299 );
300 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().(raw_string, 7), "\x1b[1;35m789\x1b[0m");
301 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 0), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(7));
302 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 1), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(8));
303 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 7), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(14));
304
305 let raw_string = "\x1b[1;36m0\x1b[m\x1b[1;36m123456789\x1b[m\n";
306 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
307 ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().(raw_string, 1),
308 "\x1b[1;36m\x1b[m\x1b[1;36m123456789\x1b[m\n"
309 );
310 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 0), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(7));
311 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 1), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(18));
312 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 7), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(24));
313
314 let raw_string = "\x1b[1;36m012345\x1b[m\x1b[1;36m6789\x1b[m\n";
315 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
316 ansi_preserving_slicerust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_slice().(raw_string, 3),
317 "\x1b[1;36m345\x1b[m\x1b[1;36m6789\x1b[m\n"
318 );
319 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 0), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(7));
320 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 1), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(8));
321 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(ansi_preserving_indexrust-analyzer cargo git-delta 0.19.2 ansi/ansi_preserving_index().(raw_string, 7), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(24));
322 }
323
324 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
325 fn test_truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/tests/test_truncate_str().() {
326 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("1", 1, ""), "1");
327 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("12", 1, ""), "1");
328 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("123", 2, "s"), "1s");
329 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("123", 2, "→"), "1→");
330 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("12ݶ", 1, "ݶ"), "ݶ");
331 }
332
333 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
334 fn test_truncate_str_at_double_width_graphemerust-analyzer cargo git-delta 0.19.2 ansi/tests/test_truncate_str_at_double_width_grapheme().() {
335 let one_double_four = "1#4";
336 let double = "/";
337 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(one_double_four.widthrust-analyzer cargo unicode-width 0.1.14 impl#[str][UnicodeWidthStr]width().(), 4);
338 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(double.widthrust-analyzer cargo unicode-width 0.1.14 impl#[str][UnicodeWidthStr]width().(), 2);
339
340 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 1, ""), "1");
341 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 2, ""), "1 ");
342 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 3, ""), "1#");
343 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 4, ""), "1#4");
344
345 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(one_double_four, 1), "1");
346 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(one_double_four, 2), "1"); // !!
347 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(one_double_four, 3), "1#");
348 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(one_double_four, 4), "1#4");
349
350 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 1, double), " ");
351 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 2, double), "/");
352 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 3, double), "1/");
353 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 4, double), "1#4");
354
355 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 0, ""), "");
356 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(one_double_four, 0, double), "");
357 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(one_double_four, 0), "");
358
359 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(double, 0, double), "");
360 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(double, 1, double), " ");
361 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(double, 2, double), double);
362
363 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(double, 0), "");
364 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(double, 1), "");
365 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(truncate_str_shortrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str_short().(double, 2), double);
366 }
367 }