rusty_prompt 0.3.0

Rusty library for creating interactive runtime command line prompts.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
use std::ops;
use std::ops::{Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};

use typed_builder::TypedBuilder;

use crate::writer::{Color, DisplayAttribute};

#[derive(Clone, Copy, TypedBuilder)]
#[builder(field_defaults(default))]
pub struct ColoredChar {
    #[builder(!default)]
    ch: u8,
    foreground_color: Color,
    background_color: Color,
    attribute: DisplayAttribute,
}

impl ColoredChar {
    pub fn set_char(&mut self, ch: u8) {
        self.ch = ch;
    }

    pub fn set_foreground_color(&mut self, color: Color) {
        self.foreground_color = color;
    }

    pub fn set_background_color(&mut self, color: Color) {
        self.background_color = color;
    }

    pub fn set_attribute(&mut self, attribute: DisplayAttribute) {
        self.attribute = attribute;
    }

    pub fn get_char(&self) -> u8 {
        self.ch
    }

    pub fn get_foreground_color(&self) -> Color {
        self.foreground_color
    }

    pub fn get_background_color(&self) -> Color {
        self.background_color
    }

    pub fn get_attribute(&self) -> DisplayAttribute {
        self.attribute
    }
}

impl PartialEq for ColoredChar {
    fn eq(&self, other: &Self) -> bool {
        self.get_char() == other.get_char() &&
            self.get_background_color() == other.get_background_color() &&
            self.get_foreground_color() == other.get_foreground_color() &&
            self.get_attribute() == other.get_attribute()
    }
}

impl PartialEq<u8> for ColoredChar {
    fn eq(&self, other: &u8) -> bool {
        self.get_char() == *other
    }
}

impl From<char> for ColoredChar {
    fn from(value: char) -> Self {
        ColoredChar::builder().ch(value as u8).build()
    }
}

impl From<u8> for ColoredChar {
    fn from(value: u8) -> Self {
        ColoredChar::builder().ch(value).build()
    }
}

#[derive(Clone)]
pub struct ColoredString {
    data: Vec<ColoredChar>,
}

impl ColoredString {
    pub fn new_from_inner(data: &[ColoredChar]) -> Self {
        Self {
            data: data.to_vec(),
        }
    }

    pub fn new() -> Self {
        Self {
            data: vec![]
        }
    }

    /// Returns a new ColoredString from the data and colors.
    /// If the colors are put in, they must be the same length as the input str.
    pub fn new_from_str(data: &str) -> Self {
        let mut colored_data = Vec::with_capacity(data.len());
        for ch in data.bytes() {
            colored_data.push(ColoredChar::builder().ch(ch).build());
        }
        Self {
            data: colored_data,
        }
    }

    pub fn new_from_bytes(data: &[u8]) -> Self {
        let mut colored_data = Vec::with_capacity(data.len());
        for ch in data {
            colored_data.push(ColoredChar::builder().ch(*ch).build());
        }
        Self {
            data: colored_data,
        }
    }

    pub fn set_color_attribute(&mut self, index: usize, background_color: Option<Color>, foreground_color: Option<Color>, attribute: Option<DisplayAttribute>) {
        if index >= self.len() {
            panic!("Tried to set color at index {} with length of {}", index, self.len());
        }
        let current = self.data.get_mut(index).unwrap();
        if background_color.is_some() {
            current.set_background_color(background_color.unwrap());
        }
        if foreground_color.is_some() {
            current.set_foreground_color(foreground_color.unwrap());
        }
        if attribute.is_some() {
            current.set_attribute(attribute.unwrap());
        }
    }

    pub fn set_char(&mut self, index: usize, ch: u8) {
        if index >= self.len() {
            panic!("Tried to set color at index {} with length of {}", index, self.len());
        }
        let current = self.data.get_mut(index).unwrap();
        current.set_char(ch);
    }

    pub fn with_capacity(cap: usize) -> Self {
        Self {
            data: Vec::with_capacity(cap),
        }
    }

    /// Insert the ch at index. Panics if index > length.
    pub fn insert(&mut self, index: usize, item: ColoredChar) {
        self.data.insert(index, item);
    }

    pub fn push(&mut self, item: ColoredChar) {
        self.data.push(item);
    }

    pub fn pop(&mut self) -> Option<ColoredChar> {
        match self.data.pop() {
            None => { None }
            Some(ch) => {
                Some(ch)
            }
        }
    }

    pub fn remove(&mut self, index: usize) -> ColoredChar {
        self.data.remove(index)
    }

    pub fn get(&self, index: usize) -> Option<&ColoredChar> {
        match self.data.get(index) {
            None => { None }
            Some(ch) => {
                Some(ch)
            }
        }
    }

    pub fn clear(&mut self) {
        self.data.clear();
    }

    pub fn shrink_to_fit(&mut self) {
        self.data.shrink_to_fit();
    }

    pub fn shrink_to(&mut self, size: usize) {
        self.data.shrink_to(size);
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }

    pub fn extend_from_chars(&mut self, other: &[char]) {
        for ch in other {
            self.data.push(ColoredChar::builder().ch(*ch as u8).build());
        }
    }

    pub fn extend_from_bytes(&mut self, other: &[u8]) {
        for ch in other {
            self.data.push(ColoredChar::builder().ch(*ch).build());
        }
    }

    pub fn extend_from_slice(&mut self, other: &[ColoredChar]) {
        self.data.extend_from_slice(other);
    }

    pub fn extend_from_other(&mut self, other: &ColoredString) {
        self.data.extend_from_slice(&other.data);
    }

    pub fn extend_from_str(&mut self, other: &ColoredStr) {
        self.data.extend_from_slice(other.get_all_chars());
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn truncate(&mut self, len: usize) {
        self.data.truncate(len);
    }

    pub fn drain<R>(&mut self, range: R) -> ColoredStringIter
        where
            R: RangeBounds<usize>
    {
        ColoredString::new_from_inner(self.data.drain(range).as_slice()).into_iter()
    }

    pub fn append(&mut self, other: &mut ColoredString) {
        self.data.append(&mut other.data);
    }

    pub fn reserve(&mut self, additional: usize) {
        self.data.reserve(additional);
    }

    pub fn bytes_iter(&self) -> ColoredStringBytesIter {
        ColoredStringBytesIter::new(self)
    }

    pub fn chars_iter(&self) -> ColoredStringCharIter {
        ColoredStringCharIter::new(self)
    }

    pub fn get_all_chars(&self) -> &[ColoredChar] {
        &self.data
    }

    pub fn to_colored_string(&self) -> ColoredString {
        ColoredString::new_from_inner(&self.data)
    }

    pub fn to_string(&self) -> String {
        self.data.iter().fold(String::with_capacity(self.len()), |mut output, ch| {
            output.push(ch.get_char() as char);
            output
        })
    }

    pub fn replace_char(&mut self, prev: u8, with: u8) {
        for ch in self.data.iter_mut() {
            if ch.get_char() == prev {
                ch.set_char(with);
            }
        }
    }

    pub fn replace_background_color(&mut self, prev: Color, with: Color) {
        for ch in self.data.iter_mut() {
            if ch.get_background_color() == prev {
                ch.set_background_color(with);
            }
        }
    }

    pub fn replace_foreground_color(&mut self, prev: Color, with: Color) {
        for ch in self.data.iter_mut() {
            if ch.get_foreground_color() == prev {
                ch.set_foreground_color(with);
            }
        }
    }

    pub fn replace_attribute(&mut self, prev: DisplayAttribute, with: DisplayAttribute) {
        for ch in self.data.iter_mut() {
            if ch.get_attribute() == prev {
                ch.set_attribute(with);
            }
        }
    }

    pub fn set_all_foreground_colors(&mut self, color: Color) {
        self.data.iter_mut().for_each(|ch| {
           ch.set_foreground_color(color);
        });
    }

    pub fn set_all_background_colors(&mut self, color: Color) {
        self.data.iter_mut().for_each(|ch| {
            ch.set_background_color(color);
        });
    }

    pub fn set_all_attributes(&mut self, attribute: DisplayAttribute) {
        self.data.iter_mut().for_each(|ch| {
            ch.set_attribute(attribute);
        });
    }

    pub fn as_str(&self) -> ColoredStr {
        ColoredStr::new(&self.data)
    }
}

impl Extend<u8> for ColoredString {
    fn extend<T: IntoIterator<Item=u8>>(&mut self, iter: T) {
        for ch in iter {
            self.push(ColoredChar::builder().ch(ch).build());
        }
    }
}

impl Extend<ColoredChar> for ColoredString {
    fn extend<T: IntoIterator<Item=ColoredChar>>(&mut self, iter: T) {
        for ch in iter {
            self.push(ch);
        }
    }
}

impl IntoIterator for ColoredString {
    type Item = ColoredChar;
    type IntoIter = ColoredStringIter;

    fn into_iter(self) -> Self::IntoIter {
        ColoredStringIter::new(self)
    }
}

impl<'a> IntoIterator for &'a ColoredString {
    type Item = &'a ColoredChar;
    type IntoIter = ColoredStringIterRef<'a>;

    fn into_iter(self) -> Self::IntoIter {
        ColoredStringIterRef::new(self)
    }
}

impl Into<String> for ColoredString {
    /// Converts the ColoredString into a regular string and removes the color data.
    fn into(self) -> String {
        let mut output = String::with_capacity(self.len());
        for ch in self.data {
            output.push(ch.get_char() as char);
        }
        output
    }
}

impl From<String> for ColoredString {
    fn from(s: String) -> Self {
        ColoredString::new_from_str(&s)
    }
}

impl From<&str> for ColoredString {
    fn from(s: &str) -> Self {
        ColoredString::new_from_str(s)
    }
}

impl PartialEq<String> for ColoredString {
    fn eq(&self, other: &String) -> bool {
        self.eq(other.as_str())
    }
}

impl PartialEq<str> for ColoredString {
    fn eq(&self, other: &str) -> bool {
        for (i, ch) in other.chars().enumerate() {
            if i >= self.len() {
                return false;
            }
            if ch != self.get(i).unwrap().get_char() as char {
                return false;
            }
        }
        true
    }
}

impl PartialEq for ColoredString {
    fn eq(&self, other: &Self) -> bool {
        self.data.eq(&other.data)
    }
}

impl Eq for ColoredString {}

impl ops::Index<usize> for ColoredString {
    type Output = ColoredChar;

    fn index(&self, index: usize) -> &Self::Output {
        match self.get(index) {
            None => { panic!("Index {} out of range! (length: {})", index, self.len()); }
            Some(ch) => {
                &ch
            }
        }
    }
}

impl ops::Index<RangeFrom<usize>> for ColoredString {
    type Output = [ColoredChar];

    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl ops::Index<RangeTo<usize>> for ColoredString {
    type Output = [ColoredChar];

    fn index(&self, index: RangeTo<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl ops::Index<RangeFull> for ColoredString {
    type Output = [ColoredChar];

    fn index(&self, index: RangeFull) -> &Self::Output {
        self.data.index(index)
    }
}

impl ops::Index<RangeInclusive<usize>> for ColoredString {
    type Output = [ColoredChar];

    fn index(&self, index: RangeInclusive<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl ops::Index<RangeToInclusive<usize>> for ColoredString {
    type Output = [ColoredChar];

    fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl ops::Index<Range<usize>> for ColoredString {
    type Output = [ColoredChar];

    fn index(&self, index: Range<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

pub struct ColoredStr<'a> {
    data: &'a [ColoredChar]
}

impl<'a> ColoredStr<'a> {
    fn new(chars: &'a [ColoredChar]) -> Self {
        Self {
            data: chars,
        }
    }

    pub fn get(&self, index: usize) -> Option<&ColoredChar> {
        match self.data.get(index) {
            None => { None }
            Some(ch) => {
                Some(ch)
            }
        }
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }

    pub fn bytes_iter(&self) -> ColoredStrBytesIter {
        ColoredStrBytesIter::new(self)
    }

    pub fn chars_iter(&self) -> ColoredStrCharIter {
        ColoredStrCharIter::new(self)
    }

    pub fn get_all_chars(&self) -> &[ColoredChar] {
        &self.data
    }

    pub fn to_colored_string(&self) -> ColoredString {
        ColoredString::new_from_inner(&self.data)
    }

    pub fn to_string(&self) -> String {
        self.data.iter().fold(String::with_capacity(self.len()), |mut output, ch| {
            output.push(ch.get_char() as char);
            output
        })
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'a> From<&'a [ColoredChar]> for ColoredStr<'a> {
    fn from(chars: &'a [ColoredChar]) -> Self {
        ColoredStr::new(chars)
    }
}

impl<'a> IntoIterator for &'a ColoredStr<'a> {
    type Item = &'a ColoredChar;
    type IntoIter = ColoredStrIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        ColoredStrIter::new(self)
    }
}

impl<'a> Into<String> for ColoredStr<'a> {
    /// Converts the ColoredStr into a regular string and removes the color data.
    fn into(self) -> String {
        self.to_string()
    }
}

impl<'a> PartialEq<String> for ColoredStr<'a> {
    fn eq(&self, other: &String) -> bool {
        self.eq(other.as_str())
    }
}

impl<'a> PartialEq<str> for ColoredStr<'a> {
    fn eq(&self, other: &str) -> bool {
        for (i, ch) in other.chars().enumerate() {
            if i >= self.len() {
                return false;
            }
            if ch != self.get(i).unwrap().get_char() as char {
                return false;
            }
        }
        true
    }
}

impl<'a> PartialEq for ColoredStr<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.data == other.data
    }
}

impl<'a> PartialEq<ColoredString> for ColoredStr<'a> {
    fn eq(&self, other: &ColoredString) -> bool {
        self.data.eq(&other.data)
    }
}

impl<'a> Eq for ColoredStr<'a> {}

impl<'a> ops::Index<usize> for ColoredStr<'a> {
    type Output = ColoredChar;

    fn index(&self, index: usize) -> &Self::Output {
        match self.get(index) {
            None => { panic!("Index {} out of range! (length: {})", index, self.len()); }
            Some(ch) => {
                &ch
            }
        }
    }
}

impl<'a> ops::Index<RangeFrom<usize>> for ColoredStr<'a> {
    type Output = [ColoredChar];

    fn index(&self, index: RangeFrom<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl<'a> ops::Index<RangeTo<usize>> for ColoredStr<'a> {
    type Output = [ColoredChar];

    fn index(&self, index: RangeTo<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl<'a> ops::Index<RangeFull> for ColoredStr<'a> {
    type Output = [ColoredChar];

    fn index(&self, index: RangeFull) -> &Self::Output {
        self.data.index(index)
    }
}

impl<'a> ops::Index<RangeInclusive<usize>> for ColoredStr<'a> {
    type Output = [ColoredChar];

    fn index(&self, index: RangeInclusive<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl<'a> ops::Index<RangeToInclusive<usize>> for ColoredStr<'a> {
    type Output = [ColoredChar];

    fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

impl<'a> ops::Index<Range<usize>> for ColoredStr<'a> {
    type Output = [ColoredChar];

    fn index(&self, index: Range<usize>) -> &Self::Output {
        self.data.index(index)
    }
}

pub struct ColoredStringIter {
    colored_string: ColoredString,
}

impl ColoredStringIter {
    fn new(colored_string: ColoredString) -> Self {
        Self {
            colored_string,
        }
    }
}

impl Iterator for ColoredStringIter {
    type Item = ColoredChar;

    fn next(&mut self) -> Option<Self::Item> {
        if self.colored_string.is_empty() {
            return None;
        }
        Some(self.colored_string.remove(0))
    }
}

pub struct ColoredStringIterRef<'a> {
    colored_string: &'a ColoredString,
    index: usize,
}

impl<'a> ColoredStringIterRef<'a> {
    fn new(colored_string: &'a ColoredString) -> Self {
        Self {
            colored_string,
            index: 0,
        }
    }
}

impl<'a> Iterator for ColoredStringIterRef<'a> {
    type Item = &'a ColoredChar;

    fn next(&mut self) -> Option<Self::Item> {
        let output = self.colored_string.get(self.index);
        self.index += 1;
        output
    }
}

pub struct ColoredStringCharIter<'a> {
    colored_string: ColoredStringIterRef<'a>,
}

impl<'a> ColoredStringCharIter<'a> {
    fn new(colored_string: &'a ColoredString) -> Self {
        Self {
            colored_string: ColoredStringIterRef::new(colored_string)
        }
    }
}

impl<'a> Iterator for ColoredStringCharIter<'a> {
    type Item = char;

    fn next(&mut self) -> Option<Self::Item> {
        match self.colored_string.next() {
            None => { None }
            Some(ch) => { Some(ch.get_char() as char) }
        }
    }
}

pub struct ColoredStringBytesIter<'a> {
    colored_string: ColoredStringIterRef<'a>,
}

impl<'a> ColoredStringBytesIter<'a> {
    fn new(colored_string: &'a ColoredString) -> Self {
        Self {
            colored_string: ColoredStringIterRef::new(colored_string)
        }
    }
}

impl<'a> Iterator for ColoredStringBytesIter<'a> {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        match self.colored_string.next() {
            None => { None }
            Some(ch) => { Some(ch.get_char()) }
        }
    }
}

pub struct ColoredStrIter<'a> {
    colored_str: &'a ColoredStr<'a>,
    index: usize,
}

impl<'a> ColoredStrIter<'a> {
    fn new(colored_str: &'a ColoredStr) -> Self {
        Self {
            colored_str,
            index: 0,
        }
    }
}

impl<'a> Iterator for ColoredStrIter<'a> {
    type Item = &'a ColoredChar;

    fn next(&mut self) -> Option<Self::Item> {
        let output = self.colored_str.get(self.index);
        if output.is_some() {
            self.index += 1;
        }
        output
    }
}

pub struct ColoredStrCharIter<'a> {
    colored_str: ColoredStrIter<'a>,
}

impl<'a> ColoredStrCharIter<'a> {
    fn new(colored_str: &'a ColoredStr) -> Self {
        Self {
            colored_str: ColoredStrIter::new(colored_str)
        }
    }
}

impl<'a> Iterator for ColoredStrCharIter<'a> {
    type Item = char;

    fn next(&mut self) -> Option<Self::Item> {
        match self.colored_str.next() {
            None => { None }
            Some(ch) => { Some(ch.get_char() as char) }
        }
    }
}

pub struct ColoredStrBytesIter<'a> {
    colored_str: ColoredStrIter<'a>,
}

impl<'a> ColoredStrBytesIter<'a> {
    fn new(colored_string: &'a ColoredStr) -> Self {
        Self {
            colored_str: ColoredStrIter::new(colored_string)
        }
    }
}

impl<'a> Iterator for ColoredStrBytesIter<'a> {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        match self.colored_str.next() {
            None => { None }
            Some(ch) => { Some(ch.get_char()) }
        }
    }
}