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
/*
    This file is a part of term-string.

    Copyright (C) 2018 Mohammad AlSaleh <CE.Mohammad.AlSaleh at gmail.com>
    https://github.com/rust-alt/term-string

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at <http://mozilla.org/MPL/2.0/>.
*/

#[cfg(test)]
mod tests;
#[macro_use]
mod macros;

pub use term::{
    terminfo::{TermInfo, TerminfoTerminal},
    Terminal,
};

#[cfg(windows)]
pub use term::{WinConsole, WinConsoleInfo};

use isatty;

use std::borrow::Borrow;
use std::io::{self, Write};
use std::ops::{Add, AddAssign};

use error::Result;
use style::TermStyle;

enum Either<T, U> {
    A(T),
    B(U),
}

#[cfg(not(windows))]
pub trait TermWrite: Write {}

// Send is required by WinConsole
#[cfg(windows)]
pub trait TermWrite: Write + Send {}

#[cfg(not(windows))]
impl<W> TermWrite for W where W: Write {}
#[cfg(windows)]
impl<W> TermWrite for W where W: Write + Send {}

#[derive(Clone, Default, PartialEq, Debug)]
struct TermStringElement {
    style: TermStyle,
    text: String,
}

impl TermStringElement {
    fn new(style: TermStyle, text: &str) -> Self {
        Self {
            style,
            text: String::from(text),
        }
    }
}

impl TermStringElement {
    fn try_write_styled<W, TERM>(&self, out: &mut TERM) -> Result<()>
    where
        W: TermWrite,
        TERM: Terminal<Output = W>,
    {
        // It's important to reset so text with empty style does not inherit attrs
        out.reset()?;

        for attr in self.style.attrs.iter().filter_map(|&attr| attr) {
            out.attr(attr)?;
        }

        write!(out, "{}", self.text)?;

        // Ignore the error here to avoid double writes
        let _ = out.reset();

        Ok(())
    }

    fn write_plain<W>(&self, out: &mut W)
    where
        W: TermWrite,
    {
        write!(out, "{}", &self.text).expect("should never happen");
    }

    fn write_styled<W, TERM>(&self, out_term: &mut TERM)
    where
        W: TermWrite,
        TERM: Terminal<Output = W>,
    {
        if self.try_write_styled(out_term).is_err() {
            self.write_plain(out_term.get_mut());
        }
    }
}

#[derive(Clone, Default, Debug)]
/// A string type with term styling info attached to it.
///
/// Internally, [`TermString`] contains multiple strings,
/// each one of them has a [`TermStyle`] attached to it.
pub struct TermString {
    elements: Vec<TermStringElement>,
}

// Essentials
/// Basic methods for constructing and modifying [`TermString`]s,
impl TermString {
    /// Create a [`TermString`] variable from a [`TermStyle`] and a string value.
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let ts = TermString::new(bold, "some bold text");
    /// ts.println();
    /// ```
    pub fn new<S>(style: TermStyle, text: S) -> Self
    where
        S: Borrow<str>,
    {
        let mut elements = Vec::with_capacity(128);
        elements.push(TermStringElement::new(style, text.borrow()));
        Self { elements }
    }

    /// Return the length of the un-styled string contained in [`TermString`].
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let underline = TermStyle::underline(true);
    /// let mut ts = TermString::new(bold, "some bold text ");
    /// ts += TermString::new(underline, "and some underlined text.");
    /// assert_eq!(ts.len(), 40);
    /// ```
    pub fn len(&self) -> usize {
        self.elements.iter().fold(0, |acc, e| acc + e.text.len())
    }

    /// Return true if the un-styled string contained in [`TermString`]
    /// is empty.
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let mut ts = TermString::new(bold, "");
    /// assert!(ts.is_empty());
    /// ts += "this is bold."
    /// ```
    // Note: empty does not imply the struct's internal vector is also empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Return the un-styled string contained in [`TermString`].
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let underline = TermStyle::underline(true);
    /// let mut ts = TermString::new(bold, "some bold text ");
    /// ts += TermString::new(underline, "and some underlined text.");
    /// let s = "some bold text and some underlined text.";
    /// assert_eq!(ts.as_string(), s);
    /// ```
    pub fn as_string(&self) -> String {
        self.elements
            .iter()
            .fold(String::with_capacity(1024), |acc, e| acc + &e.text)
    }

    /// Append a string value to a [`TermString`], inheriting the previous style.
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let mut ts = TermString::new(bold, "some bold text ");
    /// ts.append_str("and other bold text");
    /// ts.println();
    /// ```
    ///
    /// Note that the line:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// ts.append_str("and other bold text");
    /// ```
    ///
    ///  is equivalent to:
    ///
    ///  ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// ts += "and other bold text";
    ///  ```
    pub fn append_str<S>(&mut self, text: S)
    where
        S: Borrow<str>,
    {
        if self.elements.last().is_none() {
            self.append_term_str(text);
        } else if let Some(last) = self.elements.last_mut() {
            last.text += text.borrow();
        }
    }

    /// Append a [`TermString`] to a [`TermString`].
    ///
    /// # Examples
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let underline = TermStyle::underline(true);
    /// let mut ts = TermString::new(bold, "some bold text ");
    /// let ts2 = TermString::new(underline, "and some underlined text.");
    /// ts.append_term_str(ts2);
    /// ts.println();
    /// ```
    ///
    /// Note that the line:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let underline = TermStyle::underline(true);
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// # let ts2 = TermString::new(underline, "and some underlined text.");
    /// ts.append_term_str(ts2);
    /// ```
    ///
    ///  is equivalent to:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let underline = TermStyle::underline(true);
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// # let ts2 = TermString::new(underline, "and some underlined text.");
    /// ts += ts2;
    /// ```
    ///
    /// Also note that the method's argument type is `Into<Self>`, and
    /// `From<S> for TermString where S: Borrow<str>` is implemented.
    ///
    /// So, this works:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let mut ts = TermString::new(bold, "some bold text ");
    /// ts.append_term_str("and some un-styled text.");
    /// ts.println();
    /// ```
    /// Note that the method argument in the example above is converted
    /// into a [`TermString`] with a `Default` style first before appending.
    /// Contrast this with the behavior of [`append_str()`], where the appended
    /// value inherits the previous style.
    ///
    /// [`append_str()`]: TermString::append_str()
    ///
    /// So, the line:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// ts.append_term_str("and some un-styled text.");
    ///```
    ///
    ///  is equivalent to:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// ts += TermString::from("and some un-styled text.");
    /// ```
    ///
    /// which in turn is equivalent to:
    ///
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// # let bold = TermStyle::bold();
    /// # let mut ts = TermString::new(bold, "some bold text ");
    /// ts += TermString::new(TermStyle::default(), "and some un-styled text.");
    /// ```
    pub fn append_term_str<IS>(&mut self, other: IS)
    where
        IS: Into<Self>,
    {
        let mut other = other.into();
        self.elements.retain(|e| *e != TermStringElement::default());
        other
            .elements
            .retain(|e| *e != TermStringElement::default());

        let mut other_elements_iter = other.elements.into_iter();

        if self.elements.is_empty() {
            self.elements.extend(other_elements_iter);
        } else {
            while let Some(next) = other_elements_iter.next() {
                if next.style == self.elements.last().expect("impossible").style {
                    self.append_str(&*next.text);
                } else {
                    self.elements.push(next);
                    break;
                }
            }
            self.elements.extend(other_elements_iter);
        }
    }

    /// This is effectively an alias to [`append_term_str()`]
    ///
    /// [`append_term_str()`]: TermString::append_term_str
    pub fn append<IS>(&mut self, other: IS)
    where
        IS: Into<Self>,
    {
        self.append_term_str(other);
    }

    chaining_fn!(TermString, append_str,
               pub fn with_appended_str<S>(mut self, text: S) -> Self
               where
                   S: Borrow<str>,
               {
                   self.append_str(text);
                   self
               }
    );

    chaining_fn!(TermString, append_term_str,
                  pub fn with_appended_term_str<IS>(mut self, other: IS) -> Self
                  where
                      IS: Into<Self>,
                  {
                      self.append_term_str(other);
                      self
                  }
    );

    chaining_fn!(TermString, append,
                  pub fn with_appended<IS>(mut self, other: IS) -> Self
                  where
                      IS: Into<Self>,
                  {
                      self.append(other);
                      self
                  }
    );
}

// Style
/// Method for modifying the style of all internal elements of a [`TermString`].
///
/// A corresponding method from [`TermStyle`] is used on each internal element
/// of the [`TermString`].
///
/// Remember that [`TermStyle`] is a [`Copy`] type.
impl TermString {
    /// Set the styles of all internal elements of the [`TermString`] to this style.
    ///
    /// # Examples
    ///
    /// ``` rust
    /// use term_string::{TermString, TermStyle, color};
    ///
    /// let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
    /// let underline = TermStyle::underline(true);
    ///
    /// let mut ts = TermString::new(fg_bg, "fg bg");
    /// ts += TermString::new(underline, " underline");
    ///
    /// ts.set_style(TermStyle::bold());
    ///
    /// // This will print "fg bg underline" in bold and without
    /// // foreground or background colors or underline.
    /// ts.println();
    /// ```
    pub fn set_style<IT>(&mut self, style: IT)
    where
        IT: Into<TermStyle>,
    {
        let style = style.into();
        self.elements.iter_mut().for_each(|f| f.style = style);
    }

    chaining_fn!(TermString, set_style,
                  pub fn with_set_style<IT>(mut self, style: IT) -> Self
                  where
                      IT: Into<TermStyle>,
                  {
                      self.set_style(style);
                      self
                  }
    );

    /// Reset the styles of all internal elements of the [`TermString`].
    ///
    /// # Examples
    ///
    /// ``` rust
    /// use term_string::{TermString, TermStyle, color};
    ///
    /// let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
    /// let underline = TermStyle::underline(true);
    ///
    /// let mut ts = TermString::new(fg_bg, "fg bg");
    /// ts += TermString::new(underline, " underline");
    ///
    /// ts.reset_style();
    ///
    /// // This will print "fg bg underline" without any styling
    /// ts.println();
    /// ```
    pub fn reset_style(&mut self) {
        self.elements.iter_mut().for_each(|f| f.style.reset());
    }

    chaining_fn!(TermString, reset_style,
                 pub fn with_reset_style(mut self) -> Self {
                     self.reset_style();
                     self
                 }
    );

    /// Calls [`TermStyle::or_style()`] on each internal element of the [`TermString`].
    ///
    /// # Examples
    ///
    /// ``` rust
    /// use term_string::{TermString, TermStyle, color};
    ///
    /// let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
    /// let underline = TermStyle::underline(true);
    ///
    /// let mut ts = TermString::new(fg_bg, "fg bg");
    /// ts += TermString::new(underline, " underline");
    ///
    /// ts.or_style(TermStyle::bg(color::BLUE));
    ///
    /// // This will print "fg bg" with red background and white foreground,
    /// // then " underline" with underline and blue background.
    /// ts.println();
    /// ```
    pub fn or_style<IT>(&mut self, style: IT)
    where
        IT: Into<TermStyle>,
    {
        let style = style.into();
        self.elements
            .iter_mut()
            .for_each(|f| f.style.or_style(style));
    }

    chaining_fn!(TermString, or_style,
                 pub fn with_ored_style<IT>(mut self, style: IT) -> Self
                 where
                     IT: Into<TermStyle>,
                 {
                     self.or_style(style);
                     self
                 }
    );

    /// Calls [`TermStyle::add_style()`] on each internal element of the [`TermString`].
    ///
    /// # Examples
    ///
    /// ``` rust
    /// use term_string::{TermString, TermStyle, color};
    ///
    /// let fg_bg = TermStyle::bg(color::RED) + TermStyle::fg(color::WHITE);
    /// let underline = TermStyle::underline(true);
    ///
    /// let mut ts = TermString::new(fg_bg, "fg bg");
    /// ts += TermString::new(underline, " underline");
    ///
    /// ts.add_style(TermStyle::bg(color::BLUE));
    ///
    /// // This will print "fg bg" with blue background and white foreground,
    /// // then " underline" with underline and blue background.
    /// ts.println();
    /// ```
    pub fn add_style<IT>(&mut self, style: IT)
    where
        IT: Into<TermStyle>,
    {
        let style = style.into();
        self.elements
            .iter_mut()
            .for_each(|f| f.style.add_style(style));
    }

    chaining_fn!(TermString, add_style,
                 pub fn with_style<IT>(mut self, style: IT) -> Self
                 where
                     IT: Into<TermStyle>,
                 {
                     self.add_style(style);
                     self
                 }
    );
}

// write/print

gen_idents!(print, eprint, stdout, stderr);

/// IO write/print methods
///
/// # Note
/// `TermWrite` is bound to `Write + Send` on Windows, and only `Write`
/// on other platforms.
impl TermString {
    fn term_or_w<W: TermWrite>(out: W) -> Either<TerminfoTerminal<W>, W> {
        match TermInfo::from_env() {
            Ok(ti) => Either::A(TerminfoTerminal::new_with_terminfo(out, ti)),
            Err(_) => Either::B(out),
        }
    }

    #[cfg(windows)]
    fn console_or_w<W: TermWrite>(out: W) -> Either<WinConsole<W>, W> {
        match WinConsoleInfo::from_env() {
            Ok(ci) => Either::A(WinConsole::new_with_consoleinfo(out, ci)),
            Err(_) => Either::B(out),
        }
    }

    /// Write [`TermString`] to `out` without styling.
    ///
    /// # Examples
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let ts = TermString::new(bold, "some bold text");
    ///
    /// // This will write "some bold text" to stdout without
    /// // any formatting, so not really bold.
    /// ts.write_plain(std::io::stdout());
    /// ```
    pub fn write_plain<W: TermWrite>(&self, mut out: W) {
        for e in &self.elements {
            e.write_plain(&mut out);
        }
    }

    #[cfg(not(windows))]
    fn _write_styled<W: TermWrite>(&self, out: W) {
        match Self::term_or_w(out) {
            Either::A(mut out_term) => {
                for e in &self.elements {
                    e.write_styled(&mut out_term);
                }
            },
            Either::B(mut out) => self.write_plain(&mut out),
        }
    }

    #[cfg(windows)]
    fn _write_styled<W: TermWrite>(&self, out: W) {
        match Self::term_or_w(out) {
            Either::A(mut out_term) => {
                for e in &self.elements {
                    e.write_styled(&mut out_term);
                }
            },
            Either::B(out) => match Self::console_or_w(out) {
                Either::A(mut out_term) => {
                    for e in &self.elements {
                        e.write_styled(&mut out_term);
                    }
                },
                Either::B(mut out) => self.write_plain(&mut out),
            },
        }
    }

    /// Write [`TermString`] to `out` with styling.
    ///
    /// # Note
    ///
    /// * `out` doesn't have to be an actual tty.
    /// * If on Windows, and getting terminfo fails, the styling info
    ///   will be set on the current console, regardless of what `out`
    ///   is.
    ///
    /// Check out [`print()`], [`println()`], [`eprint()`], and [`eprintln()`]
    /// below, where `out` is checked before styled output is written to it.
    ///
    ///
    /// [`print()`]: TermString::print
    /// [`println()`]: TermString::println
    /// [`eprint()`]: TermString::eprint
    /// [`eprintln()`]: TermString::eprintln
    ///
    /// # Examples
    /// ``` rust
    /// # use term_string::{TermString, TermStyle};
    /// let bold = TermStyle::bold();
    /// let ts = TermString::new(bold, "some bold text");
    ///
    /// // This will write "some bold text" to stdout with formatting,
    /// // even if stdout is not a tty
    /// ts.write_styled(std::io::stdout());
    /// ```
    pub fn write_styled<W: TermWrite>(&self, out: W) {
        self._write_styled(out)
    }

    gen_print_fns!(stdout, print);
    gen_print_fns!(stderr, eprint);
}

impl<S> From<S> for TermString
where
    S: Borrow<str>,
{
    fn from(s: S) -> Self {
        Self::new(TermStyle::default(), s.borrow())
    }
}

impl<S> Add<S> for TermString
where
    S: Borrow<str>,
{
    type Output = Self;
    fn add(self, text: S) -> Self {
        self.with_appended_str(text)
    }
}

impl<S> AddAssign<S> for TermString
where
    S: Borrow<str>,
{
    fn add_assign(&mut self, text: S) {
        self.append_str(text);
    }
}

impl Add for TermString {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        self.with_appended_term_str(other)
    }
}

impl AddAssign for TermString {
    fn add_assign(&mut self, other: Self) {
        self.append_term_str(other);
    }
}