vibrance 1.0.0

Text colourisation library.
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
use std::{
    fmt::{
        Display,
        Formatter,
        Result
    },
    ops::{
        RangeBounds,
        Bound
    }
};

use crate::consts::{
    Formatting,
    FORMAT_RESET
};


/// An object representing pieces of text and
/// formatting for each part.
///
/// # Examples
///
/// Direct usage.
/// ```
/// use vibrance::ColouredString;
/// let a = ColouredString::new();
/// let b = ColouredString::from("foo");
/// ```
///
/// Shorthand usage.
/// ```
/// use vibrance::{fg, bg, style};
/// let a = fg::red("foo");
/// let b = bg::green("bar" + style::bold("baz"));
/// ```
///
/// # Aliases
///
/// Disable the `us` feature to use `ColouredString` name.
/// This is used by default.
/// 
/// Enable the `us` feature to use `ColoredString` alias.
#[derive(Debug, Clone)]
pub struct ColouredString {
    pub(crate) parts      : Vec<ColouredStringPart>,
    pub(crate) formatting : Vec<Formatting>
}

/// Initialisation
impl ColouredString {
    /// Create a new empty, unformatted, `ColouredString`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vibrance::ColouredString;
    /// let m = ColouredString::new();
    /// assert_eq!(m.unformat(), "");
    /// ```
    pub fn new() -> ColouredString {
        return ColouredString {
            parts      : Vec::new(),
            formatting : Vec::new()
        };
    }
    /// Create a new unformatted `ColouredString` containing some text.
    ///
    /// # Arguments
    /// 
    /// * `text` - Any object that can be converted into a `String`.
    /// 
    /// # Examples
    ///
    /// ```
    /// use vibrance::ColouredString;
    /// let f = ColouredString::from("foo");
    /// assert_eq!(f.unformat(), String::from("foo"));
    /// ```
    pub fn from<S : Into<String>>(text : S) -> ColouredString {
        return ColouredString::from_part(ColouredStringPart::String(text.into()));
    }
    /// Creates a new `ColouredString` contining a single part.
    /// 
    /// # Internal
    pub(crate) fn from_part(part : ColouredStringPart) -> ColouredString {
        return ColouredString {
            parts      : vec![part],
            formatting : Vec::new()
        };
    }
    /// Create a new `ColouredString` containing some text and formatting.
    /// 
    /// Commonly used by implementations of `Colourisable`.
    pub fn from_formatting<S : Into<String>>(text : S, formatting : Vec<Formatting>) -> ColouredString {
        return ColouredString {
            parts      : vec![ColouredStringPart::String(text.into())],
            formatting : formatting
        };
    }
}

/// Mutation and Getters
impl ColouredString {

    /// Create an unformatted string from the parts.
    /// The resulting string will not contain any 
    /// escape codes. If you want one that has the
    /// codes, see [ColouredString::unformat] or use
    /// `format!`.
    ///
    /// # Examples
    ///
    /// ```
    /// use vibrance::fg;
    /// let u = fg::red("foo");
    /// assert_eq!(u.unformat(), "foo");
    /// ```
    /// 
    /// # Aliases
    /// 
    /// [ColouredString::deformat].
    pub fn unformat(&self) -> String {
        return self.parts.iter().map(|part| part.unformat()).collect::<Vec<String>>().join("");
    }

    /// Alias to [ColouredString::unformat].
    pub fn deformat(&self) -> String {
        return self.unformat();
    }


    /// Format the parts with the formatting and given prefix.
    fn format_next(&self, prefix : &Vec<String>) -> String {
        let mut result = String::new();
        let mut next_prefix = prefix.clone();
        next_prefix.append(&mut self.formatting.iter().map(|f| format!("{}", f)).collect::<Vec<String>>());
        for part in &self.parts {
            result += part.format_next(&next_prefix).as_str();
        }
        return result;
    }

    /// Create a formatted string from the parts.
    /// The resulting string will contain several escape
    /// codes. If you want one that doesn't have the
    /// codes, see [ColouredString::unformat].
    pub fn format(&self) -> String {
        return self.format_next(&Vec::new());
    }


    /// Append a `ColouredStringPart` to the end of this `ColouredString`.
    /// 
    /// # Internal
    fn push_piece(&mut self, piece : ColouredStringPart) {
        let mut result = ColouredString::new();
        result.parts.push(ColouredStringPart::Sub(Box::new(self.clone())));
        result.parts.push(piece);
        *self = result;
    }

    /// Append a `ColouredString` to the end of this `ColouredString`.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut s = fg::red("foo");
    /// s.push(String::from("bar"));
    /// s.push("baz");
    /// s.push(fg::red("foo"));
    /// assert_eq!(s.unformat(), "foobarbazfoo");
    /// ```
    pub fn push<S : Into<ColouredString>>(&mut self, string : S) {
        self.push_piece(ColouredStringPart::Sub(Box::new(string.into())));
    }


    /// Splits this `ColouredString` into two parts
    /// at the given index.
    /// 
    /// # Internal
    fn split_2(&self, mut idx : usize) -> [ColouredString; 2] {
        assert!(idx <= self.len(), "Byte index out of bounds.");
        let mut left     = Vec::new();
        let mut right    = Vec::new();
        let mut is_right = false;
        for part in &self.parts {
            if (! is_right) {
                if (idx == 0) {
                    is_right = true;
                    right.push(part.clone());
                } else if (idx < part.len()) {
                    is_right = true;
                    let [part_left, part_right] = part.split_2(idx);
                    left.push(ColouredStringPart::Sub(Box::new(part_left)));
                    right.push(ColouredStringPart::Sub(Box::new(part_right)));
                } else {
                    idx -= part.len();
                    left.push(part.clone());
                }
            } else {
                right.push(part.clone());
            }
        }
        let mut left_string = ColouredString::new();
        left_string.parts      = left;
        left_string.formatting = self.formatting.clone();
        let mut right_string = ColouredString::new();
        right_string.parts      = right;
        right_string.formatting = self.formatting.clone();
        return [left_string, right_string];
    }

    /// Splits this `ColouredString` into three parts
    /// at the start and end of the given range.
    /// 
    /// # Internal
    fn split_3<R : RangeBounds<usize>>(&self, range : R) -> [ColouredString; 3] {
        let start = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n + 1,
            Bound::Unbounded    => 0
        };
        let end = match range.end_bound() {
            Bound::Included(&n) => n + 1,
            Bound::Excluded(&n) => n,
            Bound::Unbounded    => self.len()
        };
        assert!(end   >= start,      "End index nust be greater than or equal to start index.");
        assert!(start <= self.len(), "Start byte index out of bounds.");
        assert!(end   <= self.len(), "End byte index out of bounds.");
        let [left,   right] = self.split_2(start);
        let [center, right] = right.split_2(end - left.len());
        return [left, center, right];
    }

    /// Remove all characters and formatting from this `ColouredString`.
    ///
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut s = fg::red("foo");
    /// 
    /// s.clear();
    /// 
    /// assert!(s.is_empty());
    /// assert_eq!(0, s.len());
    /// ```
    pub fn clear(&mut self) {
        self.parts.clear();
        self.formatting.clear();
    }

    /// Returns a `ColouredString` of the text within
    /// the given range with formatting.
    /// 
    /// # Arguments
    /// 
    /// * range : A `RangeBounds<usize>` which specifies the start and end of the range to get.
    /// 
    /// # Panics
    /// 
    /// * The end of the range is greater than the length of this `ColouredString`.
    /// * The start of the range is greater than the end.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut r = fg::red("red" + fg::green("green")) + "none" + fg::yellow("yellow");
    /// assert_eq!(r.unformat(), "redgreennoneyellow");
    /// assert_eq!(r.get_range(5..14).unformat(), "eennoneye");
    /// ```
    pub fn get_range<R : RangeBounds<usize>>(&mut self, range : R) -> ColouredString {
        let [_, center, _] = self.split_3(range);
        return center;
    }

    /// Removes text within the given range.
    /// Returns the removed text with formatting.
    /// 
    /// # Arguments
    /// 
    /// * range : A `RangeBounds<usize>` which specifies the start and end of the range to remove.
    /// 
    /// # Panics
    /// 
    /// * The end of the range is greater than the length of this `ColouredString`.
    /// * The start of the range is greater than the end.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut r = fg::red("red" + fg::green("green")) + "none" + fg::yellow("yellow");
    /// assert_eq!(r.unformat(), "redgreennoneyellow");
    /// assert_eq!(r.remove_range(5..14).unformat(), "eennoneye");
    /// assert_eq!(r.unformat(), "redgrllow");
    /// ```
    pub fn remove_range<R : RangeBounds<usize>>(&mut self, range : R) -> ColouredString {
        let [left, center, right] = self.split_3(range);
        self.parts.clear();
        self.parts.push(ColouredStringPart::Sub(Box::new(left)));
        self.parts.push(ColouredStringPart::Sub(Box::new(right)));
        return center;
    }

    /// Replaced text within the given range
    /// with other text.
    /// Returns the replaced text with formatting.
    /// 
    /// # Arguments
    /// 
    /// * range : A `RangeBounds<usize>` which specifies the start and end of the range to replace.
    /// 
    /// # Panics
    /// 
    /// * The end of the range is greater than the length of this `ColouredString`.
    /// * The start of the range is greater than the end.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut r = fg::red("red" + fg::green("green")) + "none" + fg::yellow("yellow");
    /// assert_eq!(r.unformat(), "redgreennoneyellow");
    /// assert_eq!(r.replace_range(5..14, "replaced").unformat(), "eennoneye");
    /// assert_eq!(r.unformat(), "redgrreplacedllow");
    /// ```
    pub fn replace_range<R : RangeBounds<usize>, S : Into<ColouredString>>(&mut self, range : R, replace_with : S) -> ColouredString {
        let [left, center, right] = self.split_3(range);
        self.parts.clear();
        self.parts.push(ColouredStringPart::Sub(Box::new(left)));
        self.parts.push(ColouredStringPart::Sub(Box::new(replace_with.into())));
        self.parts.push(ColouredStringPart::Sub(Box::new(right)));
        return center;
    }

    /// Shortens this `ColouredString` to the specified length.
    /// 
    /// # Arguments
    /// 
    /// * length : The maximum length of the new `ColouredString`.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut t = fg::red("red" + fg::green("green")) + "none" + fg::yellow("yellow");
    /// assert_eq!(t.unformat(), "redgreennoneyellow");
    /// t.truncate(7);
    /// assert_eq!(t.unformat(), "redgree");
    /// ```
    pub fn truncate(&mut self, length : usize) {
        let     [left, _] = self.split_2(length);
        let mut result    = ColouredString::new();
        result.parts.push(ColouredStringPart::Sub(Box::new(left)));
        *self = result;
    }

    /// Inserts a `ColouredStringPart` at the given index.
    /// 
    /// # Internal
    fn insert_piece(&mut self, idx : usize, piece : ColouredStringPart) {
        let     [left, right] = self.split_2(idx);
        let mut result        = ColouredString::new();
        result.parts.push(ColouredStringPart::Sub(Box::new(left)));
        result.parts.push(piece);
        result.parts.push(ColouredStringPart::Sub(Box::new(right)));
        *self = result;
    }

    /// Inserts text at the given index.
    /// 
    /// # Arguments
    /// 
    /// * index : A usize which specifies the position at which to insert.
    /// 
    /// # Panics
    /// 
    /// * The index is greater than the length of this `ColouredString`.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut i = fg::red("red" + fg::green("green")) + "none" + fg::yellow("yellow");
    /// assert_eq!(i.unformat(), "redgreennoneyellow");
    /// i.insert(13, "insertion");
    /// assert_eq!(i.unformat(), "redgreennoneyinsertionellow");
    /// ```
    pub fn insert<S : Into<ColouredString>>(&mut self, index : usize, string : S) {
        self.insert_piece(index, ColouredStringPart::Sub(Box::new(string.into())));
    }
    
    /// Returns the sum of the lengths of each part.
    /// This uses the [String::len] method, so it
    /// might not be what a human considers the
    /// length of the string.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let a = fg::red("foo");
    /// assert_eq!(3, a.len());
    /// ```
    pub fn len(&self) -> usize {
        return self.parts.iter().map(|part| part.len()).sum();
    }

    /// Returns `true` is this `ColouredString` has a
    /// length of zero, and `false` otherwise.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::ColouredString;
    /// let mut v = ColouredString::new();
    /// assert!(v.is_empty());
    /// 
    /// v.push('a');
    /// assert!(! v.is_empty())
    /// ```
    pub fn is_empty(&self) -> bool {
        return self.len() == 0;
    }

    /// Removes that last character from this `ColouredString`
    /// and returns it.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use vibrance::fg;
    /// let mut p = fg::red("red") + fg::blue("blue");
    /// assert_eq!(p.unformat(), "redblue");
    /// 
    /// assert_eq!(p.pop().unformat(), "e");
    /// assert_eq!(p.unformat(), "redblu");
    /// ```
    pub fn pop(&mut self) -> ColouredString {
        return self.remove_range(self.len() - 1 .. self.len());
    }

}

/// Formatting
impl Display for ColouredString {
    fn fmt(&self, f : &mut Formatter<'_>) -> Result {
        return write!(f, "{}", self.format());
    }
}


/// A fragment of a `ColouredString`.
#[derive(Debug, Clone)]
pub(crate) enum ColouredStringPart {
    /// A string.
    String(String),
    /// A coloured string as a child.
    Sub(Box<ColouredString>)
}

/// `ColouredString` helpers.
impl ColouredStringPart {

    /// See [ColouredString::unformat].
    fn unformat(&self) -> String {
        return match (self) {
            ColouredStringPart::String (string) => String::from(string),
            ColouredStringPart::Sub    (string) => (*string).unformat()
        };
    }

    /// See [ColouredString::format].
    fn format_next(&self, prefix : &Vec<String>) -> String {
        return match (self) {
            ColouredStringPart::String(string) => format!(
                "{}{}{}",
                prefix.join(""),
                string,
                FORMAT_RESET
            ),
            ColouredStringPart::Sub(string) => string.format_next(prefix)
        };
    }

    /// See [ColouredString::split_2].
    fn split_2(&self, idx : usize) -> [ColouredString; 2] {
        match (self) {
            ColouredStringPart::String(string) => {
                return [ColouredString::from(&string[..idx]), ColouredString::from(&string[idx..])]
            },
            ColouredStringPart::Sub(string) => string.split_2(idx)
        }
    }

    /// See [ColouredString::len].
    fn len(&self) -> usize {
        return match (self) {
            ColouredStringPart::String (string) => string.len(),
            ColouredStringPart::Sub    (string) => string.len()
        };
    }

}