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

//! Conversion methods for [`crossterm`][]’s text style types.
//!
//! *Requires the `crossterm` feature.*
//!
//! This module implements these conversions:
//! - [`Color`][] to [`crossterm::style::Color`][]
//! - [`Effect`][] to [`crossterm::style::Attribute`][]
//! - [`Effects`][] to [`crossterm::style::Attributes`][]
//! - [`Style`][] to [`crossterm::style::ContentStyle`][]
//! - [`StyledStr`][] and [`StyledString`][] to [`crossterm::style::StyledContent`][]
//!
//! It also provides the [`render`][] and [`render_iter`][] methods to render strings and iterators
//! over strings.
//!
//! # Examples
//!
//! Rendering a single string:
//!
//! ```
//! let s = text_style::StyledStr::plain("test").bold();
//! text_style::crossterm::render(std::io::stdout(), s)
//!     .expect("Failed to render string");
//! ```
//!
//! Rendering multiple strings:
//!
//! ```
//! let v = vec![
//!     text_style::StyledStr::plain("test").bold(),
//!     text_style::StyledStr::plain(" "),
//!     text_style::StyledStr::plain("test2").italic(),
//! ];
//! text_style::crossterm::render_iter(std::io::stdout(), v.iter())
//!     .expect("Failed to render string");
//! ```
//!
//! [`crossterm`]: https://docs.rs/crossterm
//! [`crossterm::style::Attribute`]: https://docs.rs/crossterm/latest/crossterm/style/enum.Attribute.html
//! [`crossterm::style::Attributes`]: https://docs.rs/crossterm/latest/crossterm/style/struct.Attributes.html
//! [`crossterm::style::Color`]: https://docs.rs/crossterm/latest/crossterm/style/enum.Color.html
//! [`crossterm::style::ContentStyle`]: https://docs.rs/crossterm/latest/crossterm/style/struct.ContentStyle.html
//! [`crossterm::style::StyledContent`]: https://docs.rs/crossterm/latest/crossterm/style/struct.StyledContent.html
//! [`Color`]: ../enum.Color.html
//! [`Effect`]: ../enum.Effect.html
//! [`Effects`]: ../struct.Effects.html
//! [`Style`]: ../struct.Style.html
//! [`StyledStr`]: ../struct.StyledStr.html
//! [`StyledString`]: ../struct.StyledString.html
//! [`render`]: fn.render.html
//! [`render_iter`]: fn.render_iter.html

use std::io;

use crossterm::style;

use crate::{AnsiColor, AnsiMode, Color, Effect, Effects, Style, StyledStr, StyledString};

impl From<Color> for style::Color {
    fn from(color: Color) -> style::Color {
        use AnsiColor::*;
        use AnsiMode::*;

        match color {
            Color::Ansi { color, mode } => match (mode, color) {
                (Dark, Black) => style::Color::Black,
                (Dark, Red) => style::Color::DarkRed,
                (Dark, Green) => style::Color::DarkGreen,
                (Dark, Yellow) => style::Color::DarkYellow,
                (Dark, Blue) => style::Color::DarkBlue,
                (Dark, Magenta) => style::Color::DarkMagenta,
                (Dark, Cyan) => style::Color::DarkCyan,
                // TODO: check greys
                (Dark, White) => style::Color::Grey,
                (Light, Black) => style::Color::DarkGrey,
                (Light, Red) => style::Color::Red,
                (Light, Green) => style::Color::Green,
                (Light, Yellow) => style::Color::Yellow,
                (Light, Blue) => style::Color::Blue,
                (Light, Magenta) => style::Color::Magenta,
                (Light, Cyan) => style::Color::Cyan,
                (Light, White) => style::Color::White,
            },
            Color::Rgb { r, g, b } => style::Color::Rgb { r, g, b },
        }
    }
}

impl From<Effect> for style::Attribute {
    fn from(effect: Effect) -> style::Attribute {
        match effect {
            Effect::Bold => style::Attribute::Bold,
            Effect::Italic => style::Attribute::Italic,
            Effect::Underline => style::Attribute::Underlined,
        }
    }
}

impl From<Effects> for style::Attributes {
    fn from(effects: Effects) -> style::Attributes {
        let mut attributes = style::Attributes::default();
        for effect in effects {
            attributes.set(effect.into());
        }
        attributes
    }
}

impl From<Style> for style::ContentStyle {
    fn from(style: Style) -> style::ContentStyle {
        style::ContentStyle {
            foreground_color: style.fg.map(Into::into),
            background_color: style.bg.map(Into::into),
            attributes: style.effects.into(),
        }
    }
}

impl<'a, 'b> From<&'b StyledStr<'a>> for style::StyledContent<&'a str> {
    fn from(s: &'b StyledStr<'a>) -> style::StyledContent<&'a str> {
        style::StyledContent::new(s.style.map(Into::into).unwrap_or_default(), s.s)
    }
}

impl<'a> From<StyledStr<'a>> for style::StyledContent<&'a str> {
    fn from(s: StyledStr<'a>) -> style::StyledContent<&'a str> {
        style::StyledContent::new(s.style.map(Into::into).unwrap_or_default(), s.s)
    }
}

impl From<StyledString> for style::StyledContent<String> {
    fn from(s: StyledString) -> style::StyledContent<String> {
        style::StyledContent::new(s.style.map(Into::into).unwrap_or_default(), s.s)
    }
}

/// Renders a styled string to the given output using `crossterm`.
///
/// # Example
///
/// ```
/// let s = text_style::StyledStr::plain("test").bold();
/// text_style::crossterm::render(std::io::stdout(), s)
///     .expect("Failed to render string");
/// ```
pub fn render<'a>(mut w: impl io::Write, s: impl Into<StyledStr<'a>>) -> crossterm::Result<()> {
    use crossterm::ExecutableCommand;

    w.execute(crossterm::style::PrintStyledContent(s.into().into()))
        .map(|_| {})
}

/// Renders multiple styled string to the given output using `crossterm`.
///
/// This function queues the draw commands, so the output has to be flushed by the caller.
///
/// # Example
///
/// ```
/// let v = vec![
///     text_style::StyledStr::plain("test").bold(),
///     text_style::StyledStr::plain(" "),
///     text_style::StyledStr::plain("test2").italic(),
/// ];
/// text_style::crossterm::render_iter(std::io::stdout(), v.iter())
///     .expect("Failed to render string");
/// ```
pub fn render_iter<'a, I, Iter, S, W>(mut w: W, iter: I) -> crossterm::Result<()>
where
    I: IntoIterator<Item = S, IntoIter = Iter>,
    Iter: Iterator<Item = S>,
    S: Into<StyledStr<'a>>,
    W: io::Write,
{
    use crossterm::QueueableCommand;

    for s in iter {
        w.queue(crossterm::style::PrintStyledContent(s.into().into()))?;
    }
    Ok(())
}