1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) 2022 R3BL LLC
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use crate::{ch,
ChUnit,
GraphemeClusterSegment,
SelectionRange,
Size,
UnicodeString,
UnicodeStringSegmentSliceResult};
impl UnicodeString {
/// If any segment in `self.vec_segment` has a `display_col_offset` greater than 1
/// then this is true. The semantic is that the string is displayed using more than 1
/// column of the terminal.
pub fn contains_wide_segments(&self) -> bool {
let mut contains_wide_segments = false;
for grapheme_cluster_segment in self.iter() {
if grapheme_cluster_segment.unicode_width > ch!(1) {
contains_wide_segments = true;
break;
}
}
contains_wide_segments
}
pub fn char_display_width(character: char) -> usize {
let display_width: usize = UnicodeWidthChar::width(character).unwrap_or(0);
display_width
}
pub fn str_display_width(string: &str) -> usize {
let display_width: usize = UnicodeWidthStr::width(string);
display_width
}
pub fn truncate_to_fit_size(&self, size: Size) -> &str {
let display_cols: ChUnit = size.col_count;
self.truncate_end_to_fit_width(display_cols)
}
pub fn truncate_end_by_n_col(&self, n_display_col: ChUnit) -> &str {
let mut countdown_col_count = n_display_col;
let mut string_end_byte_index = 0;
for segment in self.iter().rev() {
let segment_display_width = segment.unicode_width;
string_end_byte_index = segment.byte_offset;
countdown_col_count -= segment_display_width;
if countdown_col_count == ch!(0) {
// We are done skipping.
break;
}
}
&self.string[..string_end_byte_index]
}
/// Removes segments from the start of the string so that `col_count` (width) is
/// skipped.
///
/// ```rust
/// use r3bl_core::UnicodeString;
/// use r3bl_core::ChUnit;
///
/// let col_count:r3bl_core::ChUnit = 2.into();
/// let display_cols:r3bl_core::ChUnit = 5.into();
/// let expected_clipped_string = "rst s";
/// let line = "first second";
/// let line = UnicodeString::from(line);
///
/// let truncated_line = line.truncate_start_by_n_col(col_count);
/// let truncated_line = UnicodeString::from(truncated_line);
///
/// let truncated_line = truncated_line.truncate_end_to_fit_width(display_cols);
///
/// assert_eq!(truncated_line, expected_clipped_string);
/// ```
pub fn truncate_start_by_n_col(&self, n_display_col: ChUnit) -> &str {
let mut skip_col_count = n_display_col;
let mut string_start_byte_index = 0;
for segment in self.iter() {
if skip_col_count != ch!(0) {
// Skip segment.unicode_width.
skip_col_count -= segment.unicode_width;
string_start_byte_index += segment.byte_size;
} else {
// We are done skipping.
break;
}
}
&self.string[string_start_byte_index..]
}
/// Returns a string slice from `self.string` w/ the segments removed from the end of
/// the string that don't fit in the given viewport width (which is 1 based, and not 0
/// based). Note that the character at `display_col_count` *index* is NOT included in
/// the result; please see the example below.
///
/// ```text
/// ←─3─→ : size (or "width" or "col count" or "count", 1 based)
/// R ┌───┐
/// 0 │fir│st second
/// └───┘
/// C012 345678901 : index (0 based)
/// ```
///
/// Example.
/// ```rust
/// use r3bl_core::UnicodeString;
/// use r3bl_core::ChUnit;
///
/// let scroll_offset_col:r3bl_core::ChUnit = 0.into();
/// let display_cols:r3bl_core::ChUnit = 3.into();
/// let expected_clipped_string = "fir";
/// let line = "first second";
/// let line = UnicodeString::from(line);
///
/// let truncated_line = line.truncate_start_by_n_col(scroll_offset_col);
/// let truncated_line = UnicodeString::from(truncated_line);
///
/// let truncated_line = truncated_line.truncate_end_to_fit_width(display_cols);
///
/// assert_eq!(truncated_line, expected_clipped_string);
/// ```
pub fn truncate_end_to_fit_width(&self, display_col_count: ChUnit) -> &str {
let mut avail_cols = display_col_count;
let mut string_end_byte_index = 0;
for segment in self.iter() {
if avail_cols < segment.unicode_width {
break;
}
string_end_byte_index += segment.byte_size;
avail_cols -= segment.unicode_width;
}
&self.string[..string_end_byte_index]
}
/// Returns a new [String] that is the result of padding `self.string` to fit the
/// given width w/ the given spacer character.
pub fn pad_end_with_spaces_to_fit_width(
&self,
spacer: &str,
max_display_col_count: ChUnit,
) -> String {
let pad_len = max_display_col_count - self.display_width;
if pad_len > ch!(0) {
let pad_str = spacer.repeat(ch!(@to_usize pad_len));
format!("{}{}", self.string, pad_str)
} else {
self.string.to_owned()
}
}
/// Uses [SelectionRange] to calculate width and simply calls
/// [clip_to_width](Self::clip_to_width).
pub fn clip_to_range(&self, range: SelectionRange) -> &str {
let SelectionRange {
start_display_col_index,
end_display_col_index,
} = range;
let max_display_col_count = end_display_col_index - start_display_col_index;
self.clip_to_width(start_display_col_index, max_display_col_count)
}
/// Clip the content starting from `start_col_index` and take as many columns as
/// possible until `max_display_col_count` is reached.
///
/// # Arguments
/// - `start_display_col_index`: This an index value.
/// - `max_display_col_count`: The is not an index value, but a size or count value.
pub fn clip_to_width(
&self,
/* index */ start_display_col_index: ChUnit,
/* width */ max_display_col_count: ChUnit,
) -> &str {
let string_start_byte_index = {
let mut it = 0;
let mut skip_col_count = start_display_col_index;
for segment in self.iter() {
// Skip scroll_offset_col_index columns.
if skip_col_count != ch!(0) {
// Skip segment.unicode_width.
skip_col_count -= segment.unicode_width;
it += segment.byte_size;
} else {
// We are done skipping.
break;
}
}
it
};
let string_end_byte_index = {
let mut it = 0;
let mut avail_col_count = max_display_col_count;
let mut skip_col_count = start_display_col_index;
for segment in self.iter() {
// Skip scroll_offset_col_index columns (again).
if skip_col_count != ch!(0) {
// Skip segment.unicode_width.
skip_col_count -= segment.unicode_width;
it += segment.byte_size;
}
// Clip max_display_col_count columns.
else {
if avail_col_count < segment.unicode_width {
break;
}
it += segment.byte_size;
avail_col_count -= segment.unicode_width;
}
}
it
};
&self.string[string_start_byte_index..string_end_byte_index]
}
/// If `self.string` is shorter than `max_display_col_count` then a padding string is
/// returned (that is comprised of the `pad_char` repeated).
pub fn try_get_postfix_padding_for(
&self,
pad_char: char,
max_display_col_count: ChUnit,
) -> Option<String> {
// Pad the line to the max cols w/ spaces. This removes any "ghost" carets that
// were painted in a previous render.
let display_width = UnicodeString::from(&self.string).display_width;
if display_width < max_display_col_count {
let padding = max_display_col_count - display_width;
Some(pad_char.to_string().repeat(ch!(@to_usize padding)))
} else {
None
}
}
/// `local_index` is the index of the grapheme cluster in the `vec_segment`.
pub fn at_logical_index(
&self,
logical_index: usize,
) -> Option<&GraphemeClusterSegment> {
self.get(logical_index)
}
/// `display_col` is the col index in the terminal where this grapheme cluster can be
/// displayed.
pub fn at_display_col_index(
&self,
display_col: ChUnit,
) -> Option<&GraphemeClusterSegment> {
self.iter().find(|&grapheme_cluster_segment| {
let segment_display_col_start: ChUnit =
grapheme_cluster_segment.display_col_offset;
let segment_display_col_end: ChUnit =
segment_display_col_start + grapheme_cluster_segment.unicode_width;
display_col >= segment_display_col_start
&& display_col < segment_display_col_end
})
}
/// Convert a `display_col` to a `logical_index`.
/// - `local_index` is the index of the grapheme cluster in the `vec_segment`.
/// - `display_col` is the col index in the terminal where this grapheme cluster can
/// be displayed.
pub fn logical_index_at_display_col_index(
&self,
display_col: ChUnit,
) -> Option<usize> {
self.at_display_col_index(display_col)
.map(|segment| segment.logical_index)
}
/// Convert a `logical_index` to a `display_col`.
/// - `local_index` is the index of the grapheme cluster in the `vec_segment`.
/// - `display_col` is the col index in the terminal where this grapheme cluster can
/// be displayed.
pub fn display_col_index_at_logical_index(
&self,
logical_index: usize,
) -> Option<ChUnit> {
self.at_logical_index(logical_index)
.map(|segment| segment.display_col_offset)
}
/// Return the string and unicode width of the grapheme cluster segment at the given
/// `display_col`. If this `display_col` falls in the middle of a grapheme cluster,
/// then return [None].
pub fn get_string_at_display_col_index(
&self,
display_col: ChUnit,
) -> Option<UnicodeStringSegmentSliceResult> {
let segment = self.at_display_col_index(display_col)?;
// What if the display_col is in the middle of a grapheme cluster?
if display_col != segment.display_col_offset {
None
} else {
Some(UnicodeStringSegmentSliceResult::new(
&segment.string,
segment.unicode_width,
segment.display_col_offset,
))
}
}
/// If the given `display_col` falls in the middle of a grapheme cluster, then return
/// the [GraphemeClusterSegment] at that `display_col`. Otherwise return [None].
pub fn is_display_col_index_in_middle_of_grapheme_cluster(
&self,
display_col: ChUnit,
) -> Option<GraphemeClusterSegment> {
let segment = self.at_display_col_index(display_col);
if let Some(segment) = segment {
if display_col != segment.display_col_offset {
return Some(segment.clone());
}
}
None
}
pub fn get_string_at_right_of_display_col_index(
&self,
display_col: ChUnit,
) -> Option<UnicodeStringSegmentSliceResult> {
let segment_at_col = self.at_display_col_index(display_col)?;
if segment_at_col.logical_index < self.len() - 1 {
let segment_right_of_col =
self.at_logical_index(segment_at_col.logical_index + 1)?;
Some(UnicodeStringSegmentSliceResult::new(
&segment_right_of_col.string,
segment_right_of_col.unicode_width,
segment_right_of_col.display_col_offset,
))
} else {
None
}
}
pub fn get_string_at_left_of_display_col_index(
&self,
display_col: ChUnit,
) -> Option<UnicodeStringSegmentSliceResult> {
let segment_at_col = self.at_display_col_index(display_col)?;
if segment_at_col.logical_index > 0 {
let segment_left_of_col =
self.at_logical_index(segment_at_col.logical_index - 1)?;
Some(UnicodeStringSegmentSliceResult::new(
&segment_left_of_col.string,
segment_left_of_col.unicode_width,
segment_left_of_col.display_col_offset,
))
} else {
None
}
}
pub fn get_string_at_end(&self) -> Option<UnicodeStringSegmentSliceResult> {
let segment = self.last()?;
Some(UnicodeStringSegmentSliceResult::new(
&segment.string,
segment.unicode_width,
segment.display_col_offset,
))
}
}