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
// SPDX-FileCopyrightText: 2020 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: Apache-2.0 or MIT

//! The `text_style` crate provides types and conversions for styled text.
//!
//! # Overview
//!
//! The central types of this crate are [`StyledStr`][] and [`StyledString`][]:  owned and borrowed
//! strings that are annotated with an optional style information, [`Style`][].  This style
//! information consists of foreground and background colors ([`Color`][]) and multiple effects
//! ([`Effect`][]: bold, italic, underline or strikeout).
//!
//! `text_style`’s types can be created directly or converted from or to several formats (all
//! optional and activated by features):
//!
//! - [`ansi_term`][]: convert to [`ansi_term::ANSIString`][]
//! - [`crossterm`][]: convert to [`crossterm::style::StyledContent`][]
//! - [`cursive`][]: convert to [`cursive::utils::markup::StyledString`][]
//! - [`genpdf`][]: convert to [`genpdf::style::StyledStr`][] and [`genpdf::style::StyledString`][]
//! - [`syntect`][]: convert from [`syntect::highlighting::Style`][]
//! - [`termion`][]: convert to a termion escape string
//!
//! # Background
//!
//! There is a plethora of crates that produce or consume styled text.  Most of these crates use
//! very similar types: ANSI and RGB colors, text effects, styled strings.  But converting between
//! these types requires a lot of boilerplate code.  The goal of this crate is to provide a subset
//! of common style types to enable conversion between the different crates.
//!
//! # Usage
//!
//! ## Creating styled text
//!
//! The [`StyledString`][] and [`StyledStr`][] structs provide many utility methods for creating
//! styled strings easily:
//!
//! ```
//! use text_style::{AnsiColor, Effect, StyledStr};
//! let s = StyledStr::plain("text")
//!     .with(AnsiColor::Red.light())
//!     .on(AnsiColor::Green.dark())
//!     .bold();
//! text_style::ansi_term::render(std::io::stdout(), s)
//!     .expect("Could not render line");
//! ```
//!
//! If the `syntect` feature is activated, conversion traits from `syntect`’s style types are
//! implemented:
//!
//! ```
//! use syntect::{easy, parsing, highlighting, util};
//!
//! let ps = parsing::SyntaxSet::load_defaults_newlines();
//! let ts = highlighting::ThemeSet::load_defaults();
//!
//! let syntax = ps.find_syntax_by_extension("rs").unwrap();
//! let mut h = easy::HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
//! let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
//! for line in util::LinesWithEndings::from(s) {
//!     let ranges: Vec<(highlighting::Style, &str)> = h.highlight(line, &ps);
//!     text_style::ansi_term::render_iter(std::io::stdout(), ranges.iter())
//!         .expect("Could not render line");
//! }
//! ```
//!
//! ## Rendering styled text
//!
//! The backends define conversion traits from or to the `text_style` types where applicable.
//!
//! Most backends also define `render` and `render_iter` methods to display a styled string or an
//! iterator over styled strings:
//!
//! ```
//! let s = text_style::StyledStr::plain("test").bold();
//!
//! let mut w = std::io::stdout();
//! text_style::ansi_term::render(&mut w, &s).expect("Rendering failed");
//! text_style::crossterm::render(&mut w, &s).expect("Rendering failed");
//! text_style::termion::render(&mut w, &s).expect("Rendering failed");
//! ```
//!
//! For more information, see the module documentations.
//!
//! [`Color`]: enum.Color.html
//! [`Effect`]: enum.Effect.html
//! [`Style`]: struct.Style.html
//! [`StyledStr`]: struct.StyledStr.html
//! [`StyledString`]: struct.StyledString.html
//! [`ansi_term`]: ./ansi_term/index.html
//! [`crossterm`]: ./crossterm/index.html
//! [`cursive`]: ./cursive/index.html
//! [`genpdf`]: ./genpdf/index.html
//! [`syntect`]: ./syntect/index.html
//! [`termion`]: ./termion/index.html
//! [`ansi_term::ANSIString`]: https://docs.rs/ansi_term/latest/ansi_term/type.ANSIString.html
//! [`crossterm::style::StyledContent`]: https://docs.rs/crossterm/latest/crossterm/style/struct.StyledContent.html
//! [`cursive::utils::markup::StyledString`]: https://docs.rs/cursive/latest/cursive/utils/markup/type.StyledString.html
//! [`genpdf::style::StyledStr`]: https://docs.rs/genpdf/latest/genpdf/style/struct.StyledStr.html
//! [`genpdf::style::StyledString`]: https://docs.rs/genpdf/latest/genpdf/style/struct.StyledString.html
//! [`syntect::highlighting::Style`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Style.html

#![warn(missing_docs, rust_2018_idioms)]

#[cfg(feature = "ansi_term")]
pub mod ansi_term;
#[cfg(feature = "crossterm")]
pub mod crossterm;
#[cfg(feature = "cursive")]
pub mod cursive;
#[cfg(feature = "genpdf")]
pub mod genpdf;
#[cfg(feature = "syntect")]
pub mod syntect;
#[cfg(feature = "termion")]
pub mod termion;

/// A borrowed string with an optional style annotation.
///
/// # Example
///
/// ```
/// let s = text_style::StyledStr::plain("test").bold();
///
/// let s1 = text_style::StyledStr::styled("test", text_style::Style::fg(text_style::AnsiColor::Red.dark()));
/// let s2 = text_style::StyledStr::plain("test").with(text_style::AnsiColor::Red.dark());
/// assert_eq!(s1, s2);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct StyledStr<'a> {
    /// The content of this string.
    pub s: &'a str,
    /// The style of this string.
    pub style: Option<Style>,
}

/// An owned string with an optional style annotation.
///
/// # Example
///
/// ```
/// let s = format!("Some number: {}", 42);
///
/// let s0 = text_style::StyledString::plain(s.clone()).bold();
///
/// let s1 = text_style::StyledString::styled(s.clone(), text_style::Style::fg(text_style::AnsiColor::Red.dark()));
/// let s2 = text_style::StyledString::plain(s.clone()).with(text_style::AnsiColor::Red.dark());
/// assert_eq!(s1, s2);
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct StyledString {
    /// The content of this string.
    pub s: String,
    /// The style of this string.
    pub style: Option<Style>,
}

/// A text style, a combination of a foreground color, a background color and text effects (all
/// optional).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Style {
    /// The foreground color (if set).
    pub fg: Option<Color>,
    /// The background color (if set).
    pub bg: Option<Color>,
    /// The text effects.
    pub effects: Effects,
}

/// A text effect.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Effect {
    /// Bold text.
    Bold,
    /// Italic text.
    Italic,
    /// Underlined text.
    Underline,
    /// Struckthrough text.
    Strikethrough,
}

/// All available text effects.
pub const EFFECTS: &[Effect] = &[
    Effect::Bold,
    Effect::Italic,
    Effect::Underline,
    Effect::Strikethrough,
];

/// A set of text effects.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Effects {
    /// Whether the bold text effect is set.
    pub is_bold: bool,
    /// Whether the italic text effect is set.
    pub is_italic: bool,
    /// Whether the underline text effect is set.
    pub is_underline: bool,
    /// Whether the strikethrough text effect is set.
    pub is_strikethrough: bool,
}

/// An iterator over text effects.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EffectsIter {
    effects: Effects,
    i: usize,
}

/// A color.
///
/// This enum stores colors, either as an ANSI color (see [`AnsiColor`][] and [`AnsiMode`][]) or as
/// an RGB color.
///
/// [`AnsiColor`]: enum.AnsiColor.html
/// [`AnsiMode`]: enum.AnsiMode.html
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Color {
    /// An ANSI color.
    Ansi {
        /// The ANSI base color.
        color: AnsiColor,
        /// The variant of the ANSI base color (light or dark).
        mode: AnsiMode,
    },
    /// An RGB color.
    Rgb {
        /// The red component.
        r: u8,
        /// The green component.
        g: u8,
        /// THe blue component.
        b: u8,
    },
}

/// An ANSI base color.
///
/// This enum contains the basic eight ANSI colors.  These colors are available in two modes:
/// [`Dark`][] and [`Light`][].  Combinations of an ANSI color and a mode are stored in the
/// [`Color`][] enum.
///
/// [`Color`]: enum.Color.html
/// [`Dark`]: enum.AnsiMode.html#variant.Dark
/// [`Light`]: enum.AnsiMode.html#variant.Light
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AnsiColor {
    /// Black (ANSI color #0 (dark) or #8 (light)).
    Black,
    /// Red (ANSI color #1 (dark) or #9 (light)).
    Red,
    /// Green (ANSI color #2 (dark) or #10 (light)).
    Green,
    /// Yellow (ANSI color #3 (dark) or #11 (light)).
    Yellow,
    /// Blue (ANSI color #4 (dark) or #12 (light)).
    Blue,
    /// Magenta (ANSI color #5 (dark) or #13 (light)).
    Magenta,
    /// Cyan (ANSI color #6 (dark) or #14 (light)).
    Cyan,
    /// White (ANSI color #7 (dark) or #15 (light)).
    White,
}

/// An ANSI color mode.
///
/// The ANSI base colors, stored in the [`AnsiColor`][] enum, are available in two modes:
/// [`Dark`][] and [`Light`][].  Combinations of an ANIS color and a mode are stored in the
/// [`Color`][] enum.
///
/// [`AnsiColor`]: enum.AnsiColor.html
/// [`Color`]: enum.Color.html
/// [`Dark`]: #variant.Dark
/// [`Light`]: #variant.Light
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AnsiMode {
    /// The dark variant of an ANSI color.
    Dark,
    /// The light variant of an ANSI color.
    Light,
}

impl<'a> StyledStr<'a> {
    /// Creates a new styled string from the given string and an optional style.
    pub fn new(s: &'a str, style: Option<Style>) -> StyledStr<'a> {
        StyledStr { s, style }
    }

    /// Creates a new styled string from the given string and style.
    pub fn styled(s: &'a str, style: Style) -> StyledStr<'a> {
        StyledStr::new(s, Some(style))
    }

    /// Creates a new styled string from the given string without a style.
    pub fn plain(s: &'a str) -> StyledStr<'a> {
        StyledStr::new(s, None)
    }

    /// Sets the foreground color for this styled string.
    pub fn with(mut self, fg: Color) -> Self {
        self.style_mut().fg = Some(fg);
        self
    }

    /// Sets the background color for this styled string.
    pub fn on(mut self, bg: Color) -> Self {
        self.style_mut().bg = Some(bg);
        self
    }

    /// Sets the bold effect for this styled string.
    pub fn bold(self) -> Self {
        self.effect(Effect::Bold)
    }

    /// Sets the italic effect for this styled string.
    pub fn italic(self) -> Self {
        self.effect(Effect::Italic)
    }

    /// Sets the underline effect for this styled string.
    pub fn underline(self) -> Self {
        self.effect(Effect::Underline)
    }

    /// Sets the strikethrough effect for this styled string.
    pub fn strikethrough(self) -> Self {
        self.effect(Effect::Strikethrough)
    }

    /// Sets the given effect for this styled string.
    pub fn effect(mut self, effect: Effect) -> Self {
        self.style_mut().effects.set(effect, true);
        self
    }

    /// Returns a mutable reference to the style of this string, creating a new style with the
    /// default settings if the style is currently `None`.
    pub fn style_mut(&mut self) -> &mut Style {
        self.style.get_or_insert_with(Default::default)
    }
}

impl StyledString {
    /// Creates a new styled string from the given string and an optional style.
    pub fn new(s: String, style: Option<Style>) -> StyledString {
        StyledString { s, style }
    }

    /// Creates a new styled string from the given string and style.
    pub fn styled(s: String, style: Style) -> StyledString {
        StyledString::new(s, Some(style))
    }

    /// Creates a new styled string from the given string and style.
    pub fn plain(s: String) -> StyledString {
        StyledString::new(s, None)
    }

    /// Sets the foreground color for this styled string.
    pub fn with(mut self, fg: Color) -> Self {
        self.style_mut().fg = Some(fg);
        self
    }

    /// Sets the background color for this styled string.
    pub fn on(mut self, bg: Color) -> Self {
        self.style_mut().bg = Some(bg);
        self
    }

    /// Sets the bold effect for this styled string.
    pub fn bold(self) -> Self {
        self.effect(Effect::Bold)
    }

    /// Sets the italic effect for this styled string.
    pub fn italic(self) -> Self {
        self.effect(Effect::Italic)
    }

    /// Sets the underline effect for this styled string.
    pub fn underline(self) -> Self {
        self.effect(Effect::Underline)
    }

    /// Sets the strikethrough effect for this styled string.
    pub fn strikethrough(self) -> Self {
        self.effect(Effect::Strikethrough)
    }

    /// Sets the given effect for this styled string.
    pub fn effect(mut self, effect: Effect) -> Self {
        self.style_mut().effects.set(effect, true);
        self
    }

    /// Returns a mutable reference to the style of this string, creating a new style with the
    /// default settings if the style is currently `None`.
    pub fn style_mut(&mut self) -> &mut Style {
        self.style.get_or_insert_with(Default::default)
    }
}

impl<'a, 'b> From<&'b StyledStr<'a>> for StyledStr<'a> {
    fn from(s: &'b StyledStr<'a>) -> StyledStr<'a> {
        StyledStr {
            s: &s.s,
            style: s.style,
        }
    }
}

impl<'a> From<&'a StyledString> for StyledStr<'a> {
    fn from(s: &'a StyledString) -> StyledStr<'a> {
        StyledStr {
            s: &s.s,
            style: s.style,
        }
    }
}

impl<'a> From<StyledStr<'a>> for StyledString {
    fn from(s: StyledStr<'a>) -> StyledString {
        StyledString {
            s: s.s.to_owned(),
            style: s.style,
        }
    }
}

impl<'a> From<&'a str> for StyledStr<'a> {
    fn from(s: &'a str) -> StyledStr<'a> {
        StyledStr::plain(s)
    }
}

impl From<String> for StyledString {
    fn from(s: String) -> StyledString {
        StyledString::plain(s)
    }
}

impl Style {
    /// Creates a new style with the given foreground and background colors and effects.
    pub fn new(fg: Option<Color>, bg: Option<Color>, effects: Effects) -> Style {
        Style { fg, bg, effects }
    }

    /// Creates a new style with the given foreground color.
    pub fn fg(color: Color) -> Style {
        Style::new(Some(color), None, Effects::new())
    }

    /// Creates a new style with the given background color.
    pub fn bg(color: Color) -> Style {
        Style::new(None, Some(color), Effects::new())
    }

    /// Creates a new style with the given text effect.
    pub fn effect(effect: Effect) -> Style {
        Style::new(None, None, Effects::only(effect))
    }

    /// Creates a new style with the given text effects.
    pub fn effects(effects: Effects) -> Style {
        Style::new(None, None, effects)
    }

    /// Combines this style with another style.
    ///
    /// If a color is set by both styles, the current color is overwritten.
    ///
    /// # Example
    ///
    /// ```
    /// use text_style::{AnsiColor, Effects, Style};
    ///
    /// assert_eq!(
    ///     Style::fg(AnsiColor::Red.dark()).and(Style::bg(AnsiColor::White.dark())),
    ///     Style::new(Some(AnsiColor::Red.dark()), Some(AnsiColor::White.dark()), Effects::empty()),
    /// );
    /// ```
    pub fn and(mut self, style: Style) -> Style {
        if let Some(fg) = style.fg {
            self.fg = Some(fg);
        }
        if let Some(bg) = style.bg {
            self.bg = Some(bg);
        }
        self.effects = self.effects.and(style.effects);
        self
    }

    /// Sets the foreground color of this style.
    pub fn set_fg(&mut self, color: Color) {
        self.fg = Some(color);
    }

    /// Sets the background color of this style.
    pub fn set_bg(&mut self, color: Color) {
        self.bg = Some(color);
    }

    /// Sets or unsets the bold effect for this style.
    pub fn set_bold(&mut self, bold: bool) {
        self.effects.is_bold = bold;
    }

    /// Sets or unsets the italic effect for this style.
    pub fn set_italic(&mut self, italic: bool) {
        self.effects.is_italic = italic;
    }

    /// Sets or unsets the underline effect for this style.
    pub fn set_underline(&mut self, underline: bool) {
        self.effects.is_underline = underline;
    }

    /// Sets or unsets the strikethrough effect for this style.
    pub fn strikethrough(&mut self, strikethrough: bool) {
        self.effects.is_strikethrough = strikethrough;
    }

    /// Sets or unsets the given effect for this style.
    pub fn set_effect(&mut self, effect: Effect, set: bool) {
        self.effects.set(effect, set);
    }
}

impl From<Effect> for Style {
    fn from(effect: Effect) -> Style {
        Style::effect(effect)
    }
}

impl From<Effects> for Style {
    fn from(effects: Effects) -> Style {
        Style::effects(effects)
    }
}

impl Effects {
    /// Creates an empty set of text effects.
    pub fn new() -> Effects {
        Default::default()
    }

    /// Creates an empty set of text effects.
    pub fn empty() -> Effects {
        Effects::new()
    }

    /// Creates a set of text effects with only the given effect.
    pub fn only(effect: Effect) -> Effects {
        Effects::from(effect)
    }

    /// Sets or unsets the given text effect.
    pub fn set(&mut self, effect: Effect, set: bool) {
        match effect {
            Effect::Bold => self.is_bold = set,
            Effect::Italic => self.is_italic = set,
            Effect::Underline => self.is_underline = set,
            Effect::Strikethrough => self.is_strikethrough = set,
        }
    }

    /// Checks whether the given effect is set.
    pub fn is_set(&self, effect: Effect) -> bool {
        match effect {
            Effect::Bold => self.is_bold,
            Effect::Italic => self.is_italic,
            Effect::Underline => self.is_underline,
            Effect::Strikethrough => self.is_strikethrough,
        }
    }

    /// Combines this set of text effects with another set of text effects.
    pub fn and(&self, other: Effects) -> Effects {
        Effects {
            is_bold: self.is_bold || other.is_bold,
            is_italic: self.is_italic || other.is_italic,
            is_underline: self.is_underline || other.is_underline,
            is_strikethrough: self.is_strikethrough || other.is_strikethrough,
        }
    }

    /// Checks whether this set of text effects is empty.
    pub fn is_empty(&self) -> bool {
        !self.is_bold && !self.is_italic && !self.is_underline && !self.is_strikethrough
    }
}

impl std::iter::FromIterator<Effect> for Effects {
    fn from_iter<I: IntoIterator<Item = Effect>>(iter: I) -> Effects {
        let mut effects = Effects::new();
        for effect in iter {
            effects.set(effect, true);
        }
        effects
    }
}

impl IntoIterator for Effects {
    type Item = Effect;
    type IntoIter = EffectsIter;

    fn into_iter(self) -> EffectsIter {
        EffectsIter::from(self)
    }
}

impl From<Effect> for Effects {
    fn from(effect: Effect) -> Effects {
        let mut effects = Effects::new();
        effects.set(effect, true);
        effects
    }
}

impl Iterator for EffectsIter {
    type Item = Effect;

    fn next(&mut self) -> Option<Effect> {
        let mut next = None;
        while let Some(effect) = EFFECTS.get(self.i) {
            self.i += 1;
            if self.effects.is_set(*effect) {
                next = Some(*effect);
                break;
            }
        }
        next
    }
}

impl From<Effects> for EffectsIter {
    fn from(effects: Effects) -> EffectsIter {
        EffectsIter { effects, i: 0 }
    }
}

impl AnsiColor {
    /// Returns the dark variant of this ANSI color.
    pub fn dark(self) -> Color {
        Color::Ansi {
            color: self,
            mode: AnsiMode::Dark,
        }
    }

    /// Returns the light variant of this ANSI color.
    pub fn light(self) -> Color {
        Color::Ansi {
            color: self,
            mode: AnsiMode::Light,
        }
    }
}