idiom_tui 1.0.1

Bundel of tui widgest and layout componenets (split from idiom editor)
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
mod chunks;
pub use chunks::{ByteChunks, CharLimitedWidths, StrChunks, WriteChunks};
use std::ops::Range;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};

pub type Utf8Byte = usize;
pub type Utf16Byte = usize;

/// Trait allowing UTF8 safe operations on str/String
pub trait UTFSafe {
    /// returns str that will fit into width of columns, removing chars at the end returning info about remaining width
    fn truncate_width(&self, width: usize) -> (usize, &str);
    /// returns str that will fit into width of columns, removing chars from the start returng info about remaining width
    fn truncate_width_start(&self, width: usize) -> (usize, &str);
    /// return Some(&str) if wider than allowed width
    fn truncate_if_wider(&self, width: usize) -> Result<&str, usize>;
    /// return Some(&str) truncated from start if wider than allowed width
    fn truncate_if_wider_start(&self, width: usize) -> Result<&str, usize>;
    /// split on width
    fn width_split(&self, width: usize) -> (&str, Option<&str>);
    /// returns display len of the str
    fn width(&self) -> usize;
    /// calcs the width at position
    fn width_at(&self, at: usize) -> usize;
    /// returns utf8 chars len
    fn char_len(&self) -> usize;
    /// utf16 len
    fn utf16_len(&self) -> usize;
    /// return utf8 split at char idx
    fn split_at_char(&self, mid: usize) -> (&str, &str);
    /// splits utf8 if not ascii (needs precalculated utf8 len)
    fn cached_split_at_char(&self, mid: usize, utf8_len: usize) -> (&str, &str);
    /// limits str within range based on utf char locations
    /// panics if out of bounds
    fn unchecked_get_char_range(&self, from: usize, to: usize) -> &str;
    /// removes "from" chars from the begining of the string
    /// panics if out of bounds
    fn unchecked_get_from_char(&self, from: usize) -> &str;
    /// limits str to char idx
    /// panics if out of bounds
    fn unchecked_get_to_char(&self, to: usize) -> &str;
    /// get checked utf8 slice
    fn get_char_range(&self, from_char: usize, to_char: usize) -> Option<&str>;
    /// get checked utf8 from
    fn get_from_char(&self, from_char: usize) -> Option<&str>;
    /// get checked utf8 to
    fn get_to_char(&self, to_char: usize) -> Option<&str>;
}

/// String specific extension
pub trait UTFSafeStringExt {
    fn insert_at_char(&mut self, idx: usize, ch: char);
    fn insert_at_char_with_utf8_idx(&mut self, idx: usize, ch: char) -> Utf8Byte;
    fn insert_at_char_with_utf16_idx(&mut self, idx: usize, ch: char) -> Utf16Byte;
    fn insert_str_at_char(&mut self, idx: usize, string: &str);
    fn insert_str_at_char_with_utf8_idx(&mut self, idx: usize, string: &str) -> Utf8Byte;
    fn insert_str_at_char_with_utf16_idx(&mut self, idx: usize, string: &str) -> Utf16Byte;
    fn remove_at_char(&mut self, idx: usize) -> char;
    /// returns the removed char with the utf8 idx from where it was removed
    fn remove_at_char_with_utf8_idx(&mut self, idx: usize) -> (Utf8Byte, char);
    /// returns the removed char with the utf16 idx from where it was removed
    fn remove_at_char_with_utf16_idx(&mut self, idx: usize) -> (Utf16Byte, char);
    fn replace_char_range(&mut self, range: Range<usize>, string: &str);
    fn replace_till_char(&mut self, to: usize, string: &str);
    fn replace_from_char(&mut self, from: usize, string: &str);
    fn split_off_at_char(&mut self, at: usize) -> Self;
}

impl UTFSafe for str {
    #[inline]
    fn truncate_width(&self, mut width: usize) -> (usize, &str) {
        let mut end = 0;
        for char in self.chars() {
            let char_width = UnicodeWidthChar::width(char).unwrap_or(0);
            if char_width > width {
                return (width, unsafe { self.get_unchecked(..end) });
            };
            width -= char_width;
            end += char.len_utf8();
        }
        (width, self)
    }

    #[inline]
    fn truncate_width_start(&self, mut width: usize) -> (usize, &str) {
        let mut start = 0;
        for char in self.chars().rev() {
            let char_width = UnicodeWidthChar::width(char).unwrap_or(0);
            if char_width > width {
                return (width, unsafe { self.get_unchecked(self.len() - start..) });
            }
            width -= char_width;
            start += char.len_utf8();
        }
        (width, self)
    }

    #[inline]
    fn truncate_if_wider(&self, width: usize) -> Result<&str, usize> {
        let mut end = 0;
        let mut current_width = 0;
        for char in self.chars() {
            current_width += UnicodeWidthChar::width(char).unwrap_or(0);
            if current_width > width {
                return Ok(unsafe { self.get_unchecked(..end) });
            };
            end += char.len_utf8();
        }
        Err(current_width)
    }

    #[inline]
    fn truncate_if_wider_start(&self, width: usize) -> Result<&str, usize> {
        let mut start = 0;
        let mut current_width = 0;
        for char in self.chars().rev() {
            current_width += UnicodeWidthChar::width(char).unwrap_or(0);
            if current_width > width {
                return Ok(unsafe { self.get_unchecked(self.len() - start..) });
            }
            start += char.len_utf8();
        }
        Err(current_width)
    }

    #[inline]
    fn width_split(&self, mut width: usize) -> (&str, Option<&str>) {
        for (current_mid, ch) in self.char_indices() {
            let ch_width = ch.width().unwrap_or(0);
            match ch_width > width {
                true => {
                    let (current, remaining) = self.split_at(current_mid);
                    return (current, Some(remaining));
                }
                false => {
                    width -= ch_width;
                }
            }
        }
        (self, None)
    }

    #[inline]
    fn width(&self) -> usize {
        UnicodeWidthStr::width(self)
    }

    #[inline]
    fn width_at(&self, at: usize) -> usize {
        self.chars()
            .take(at)
            .fold(0, |l, r| l + UnicodeWidthChar::width(r).unwrap_or(0))
    }

    #[inline]
    fn char_len(&self) -> usize {
        self.chars().count()
    }

    #[inline]
    fn utf16_len(&self) -> usize {
        self.chars().fold(0, |sum, ch| sum + ch.len_utf16())
    }

    #[inline]
    fn split_at_char(&self, mid: usize) -> (&str, &str) {
        self.split_at(prev_char_bytes_end(self, mid))
    }

    #[inline]
    fn cached_split_at_char(&self, mid: usize, utf8_len: usize) -> (&str, &str) {
        if self.len() == utf8_len {
            return self.split_at(mid);
        }
        self.split_at_char(mid)
    }

    #[inline]
    fn get_char_range(&self, from: usize, to: usize) -> Option<&str> {
        maybe_prev_char_bytes_end(self, from)
            .and_then(|from_checked| Some(from_checked..maybe_prev_char_bytes_end(self, to)?))
            .map(|range| unsafe { self.get_unchecked(range) })
    }

    #[inline]
    fn get_from_char(&self, from: usize) -> Option<&str> {
        maybe_prev_char_bytes_end(self, from)
            .map(|from_checked| unsafe { self.get_unchecked(from_checked..) })
    }

    #[inline]
    fn get_to_char(&self, to: usize) -> Option<&str> {
        maybe_prev_char_bytes_end(self, to)
            .map(|to_checked| unsafe { self.get_unchecked(..to_checked) })
    }

    #[inline]
    fn unchecked_get_char_range(&self, from: usize, to: usize) -> &str {
        unsafe {
            self.get_unchecked(prev_char_bytes_end(self, from)..prev_char_bytes_end(self, to))
        }
    }

    #[inline]
    fn unchecked_get_from_char(&self, from: usize) -> &str {
        unsafe { self.get_unchecked(prev_char_bytes_end(self, from)..) }
    }

    #[inline]
    fn unchecked_get_to_char(&self, to: usize) -> &str {
        unsafe { self.get_unchecked(..prev_char_bytes_end(self, to)) }
    }
}

impl UTFSafe for String {
    #[inline]
    fn truncate_width(&self, width: usize) -> (usize, &str) {
        self.as_str().truncate_width(width)
    }

    #[inline]
    fn truncate_width_start(&self, width: usize) -> (usize, &str) {
        self.as_str().truncate_width_start(width)
    }

    #[inline]
    fn truncate_if_wider(&self, width: usize) -> Result<&str, usize> {
        self.as_str().truncate_if_wider(width)
    }

    #[inline]
    fn truncate_if_wider_start(&self, width: usize) -> Result<&str, usize> {
        self.as_str().truncate_if_wider_start(width)
    }

    #[inline]
    fn width_split(&self, width: usize) -> (&str, Option<&str>) {
        self.as_str().width_split(width)
    }

    #[inline]
    fn width(&self) -> usize {
        UnicodeWidthStr::width(self.as_str())
    }

    #[inline]
    fn width_at(&self, at: usize) -> usize {
        self.as_str().width_at(at)
    }

    #[inline]
    fn char_len(&self) -> usize {
        self.chars().count()
    }

    #[inline]
    fn utf16_len(&self) -> usize {
        self.as_str().utf16_len()
    }

    #[inline]
    fn split_at_char(&self, mid: usize) -> (&str, &str) {
        self.as_str().split_at_char(mid)
    }

    #[inline]
    fn cached_split_at_char(&self, mid: usize, utf8_len: usize) -> (&str, &str) {
        self.as_str().cached_split_at_char(mid, utf8_len)
    }

    #[inline]
    fn get_char_range(&self, from: usize, to: usize) -> Option<&str> {
        self.as_str().get_char_range(from, to)
    }

    #[inline]
    fn get_from_char(&self, from: usize) -> Option<&str> {
        self.as_str().get_from_char(from)
    }

    #[inline]
    fn get_to_char(&self, to: usize) -> Option<&str> {
        self.as_str().get_to_char(to)
    }

    #[inline]
    fn unchecked_get_char_range(&self, from: usize, to: usize) -> &str {
        self.as_str().unchecked_get_char_range(from, to)
    }

    #[inline]
    fn unchecked_get_from_char(&self, from: usize) -> &str {
        self.as_str().unchecked_get_from_char(from)
    }

    #[inline]
    fn unchecked_get_to_char(&self, to: usize) -> &str {
        self.as_str().unchecked_get_to_char(to)
    }
}

impl UTFSafeStringExt for String {
    #[inline]
    fn insert_at_char(&mut self, idx: usize, ch: char) {
        self.insert(prev_char_bytes_end(self, idx), ch);
    }

    #[inline]
    fn insert_at_char_with_utf8_idx(&mut self, idx: usize, ch: char) -> Utf8Byte {
        let byte_idx = prev_char_bytes_end(self, idx);
        self.insert(byte_idx, ch);
        byte_idx
    }

    #[inline]
    fn insert_at_char_with_utf16_idx(&mut self, idx: usize, ch: char) -> Utf16Byte {
        let (byte_idx, utf16_byte) = prev_char_utf8_and_utf16(self, idx);
        self.insert(byte_idx, ch);
        utf16_byte
    }

    #[inline]
    fn insert_str_at_char(&mut self, idx: usize, string: &str) {
        self.insert_str(prev_char_bytes_end(self, idx), string)
    }

    #[inline]
    fn insert_str_at_char_with_utf8_idx(&mut self, idx: usize, string: &str) -> Utf8Byte {
        let byte_idx = prev_char_bytes_end(self, idx);
        self.insert_str(byte_idx, string);
        byte_idx
    }

    #[inline]
    fn insert_str_at_char_with_utf16_idx(&mut self, idx: usize, string: &str) -> Utf16Byte {
        let (byte_idx, utf16_byte) = prev_char_utf8_and_utf16(self, idx);
        self.insert_str(byte_idx, string);
        utf16_byte
    }

    #[inline]
    fn remove_at_char(&mut self, idx: usize) -> char {
        self.remove(prev_char_bytes_end(self, idx))
    }

    #[inline]
    fn remove_at_char_with_utf8_idx(&mut self, idx: usize) -> (Utf8Byte, char) {
        let byte_idx = prev_char_bytes_end(self, idx);
        (byte_idx, self.remove(byte_idx))
    }

    #[inline]
    fn remove_at_char_with_utf16_idx(&mut self, idx: usize) -> (Utf16Byte, char) {
        let (byte_idx, utf16_byte) = prev_char_utf8_and_utf16(self, idx);
        (utf16_byte, self.remove(byte_idx))
    }

    #[inline]
    fn replace_char_range(&mut self, range: Range<usize>, text: &str) {
        let start = prev_char_bytes_end(self, range.start);
        let end = prev_char_bytes_end(self, range.end);
        self.replace_range(start..end, text);
    }

    #[inline]
    fn replace_from_char(&mut self, from: usize, string: &str) {
        self.truncate(prev_char_bytes_end(self, from));
        self.push_str(string);
    }

    #[inline]
    fn replace_till_char(&mut self, to: usize, string: &str) {
        self.replace_range(..prev_char_bytes_end(self, to), string);
    }

    #[inline]
    fn split_off_at_char(&mut self, at: usize) -> Self {
        self.split_off(prev_char_bytes_end(self, at))
    }
}

#[inline]
fn prev_char_bytes_end(text: &str, idx: usize) -> Utf8Byte {
    if idx == 0 {
        return 0;
    }
    if let Some((byte_idx, ch)) = text.char_indices().nth(idx - 1) {
        return byte_idx + ch.len_utf8();
    }
    panic!(
        "Index out of bound! Max len {} with index {}",
        text.char_len(),
        idx
    )
}

#[inline]
fn prev_char_utf8_and_utf16(text: &str, idx: usize) -> (Utf8Byte, Utf16Byte) {
    if idx == 0 {
        return (0, 0);
    }
    let mut counter = idx;
    let mut utf8_byte = 0;
    let mut utf16_byte = 0;
    for ch in text.chars() {
        counter -= 1;
        utf8_byte += ch.len_utf8();
        utf16_byte += ch.len_utf16();
        if counter == 0 {
            return (utf8_byte, utf16_byte);
        }
    }
    panic!(
        "Index out of bound! Max len {} with index {}",
        text.char_len(),
        idx
    )
}

#[inline]
fn maybe_prev_char_bytes_end(text: &str, idx: usize) -> Option<usize> {
    if idx == 0 {
        return Some(idx);
    }
    text.char_indices()
        .nth(idx - 1)
        .map(|(byte_idx, ch)| byte_idx + ch.len_utf8())
}

#[cfg(test)]
mod tests;