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

//! Conversion methods for [`syntect`][]’s text style types.
//!
//! *Requires the `syntect` feature.*
//!
//! This module implements these conversions:
//! - [`syntect::highlighting::Color`][] to [`Color`][]
//! - [`syntect::highlighting::FontStyle`][] to [`Effects`][]
//! - [`syntect::highlighting::Style`][] to [`Style`][]
//! - `(&str, syntect::highlighting::Style)` to [`StyledStr`][]
//!
//! # Example
//!
//! Converting highlighted ranges to styled strings and rendering them:
//!
//! ```
//! 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");
//! }
//! ```
//!
//! [`syntect`]: https://docs.rs/syntect
//! [`syntect::highlighting::Color`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Color.html
//! [`syntect::highlighting::FontStyle`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Style.html
//! [`syntect::highlighting::Style`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Style.html
//! [`Color`]: ../enum.Color.html
//! [`Effects`]: ../struct.Effects.html
//! [`Style`]: ../struct.Style.html
//! [`StyledStr`]: ../struct.StyledStr.html

use syntect::highlighting;

use crate::{Color, Effects, Style, StyledStr};

impl From<highlighting::Color> for Color {
    fn from(color: highlighting::Color) -> Color {
        Color::Rgb {
            r: color.r,
            g: color.g,
            b: color.b,
        }
    }
}

impl From<highlighting::FontStyle> for Effects {
    fn from(font_style: highlighting::FontStyle) -> Effects {
        Effects {
            is_bold: font_style.contains(highlighting::FontStyle::BOLD),
            is_italic: font_style.contains(highlighting::FontStyle::ITALIC),
            is_underline: font_style.contains(highlighting::FontStyle::UNDERLINE),
        }
    }
}

impl From<highlighting::Style> for Style {
    fn from(style: highlighting::Style) -> Style {
        Style {
            fg: Some(style.foreground.into()),
            bg: Some(style.background.into()),
            effects: style.font_style.into(),
        }
    }
}

impl<'a, 'b> From<&'b (highlighting::Style, &'a str)> for StyledStr<'a> {
    fn from((style, s): &'b (highlighting::Style, &'a str)) -> StyledStr<'a> {
        StyledStr {
            s,
            style: Some(style.clone().into()),
        }
    }
}

impl<'a> From<(highlighting::Style, &'a str)> for StyledStr<'a> {
    fn from((style, s): (highlighting::Style, &'a str)) -> StyledStr<'a> {
        StyledStr {
            s,
            style: Some(style.into()),
        }
    }
}