text_style/syntect.rs
1// SPDX-FileCopyrightText: 2020-2021 Robin Krahl <robin.krahl@ireas.org>
2// SPDX-License-Identifier: Apache-2.0 or MIT
3
4//! Conversion methods for [`syntect`][]’s text style types.
5//!
6//! *Requires the `syntect` feature.*
7//!
8//! This module implements these conversions:
9//! - [`syntect::highlighting::Color`][] to [`Color`][]
10//! - [`syntect::highlighting::FontStyle`][] to [`Effects`][]
11//! - [`syntect::highlighting::Style`][] to [`Style`][]
12//! - `(&str, syntect::highlighting::Style)` to [`StyledStr`][]
13//!
14//! # Example
15//!
16//! Converting highlighted ranges to styled strings and rendering them:
17//!
18//! ```
19//! use syntect::{easy, parsing, highlighting, util};
20//!
21//! let ps = parsing::SyntaxSet::load_defaults_newlines();
22//! let ts = highlighting::ThemeSet::load_defaults();
23//!
24//! let syntax = ps.find_syntax_by_extension("rs").unwrap();
25//! let mut h = easy::HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
26//! let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
27//! for line in util::LinesWithEndings::from(s) {
28//! let ranges: Vec<(highlighting::Style, &str)> = h.highlight(line, &ps);
29//! text_style::ansi_term::render_iter(std::io::stdout(), ranges.iter())
30//! .expect("Could not render line");
31//! }
32//! ```
33//!
34//! [`syntect`]: https://docs.rs/syntect
35//! [`syntect::highlighting::Color`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Color.html
36//! [`syntect::highlighting::FontStyle`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Style.html
37//! [`syntect::highlighting::Style`]: https://docs.rs/syntect/latest/syntect/highlighting/struct.Style.html
38//! [`Color`]: ../enum.Color.html
39//! [`Effects`]: ../struct.Effects.html
40//! [`Style`]: ../struct.Style.html
41//! [`StyledStr`]: ../struct.StyledStr.html
42
43use syntect::highlighting;
44
45use crate::{Color, Effects, Style, StyledStr};
46
47impl From<highlighting::Color> for Color {
48 fn from(color: highlighting::Color) -> Color {
49 Color::Rgb {
50 r: color.r,
51 g: color.g,
52 b: color.b,
53 }
54 }
55}
56
57impl From<highlighting::FontStyle> for Effects {
58 fn from(font_style: highlighting::FontStyle) -> Effects {
59 Effects {
60 is_bold: font_style.contains(highlighting::FontStyle::BOLD),
61 is_italic: font_style.contains(highlighting::FontStyle::ITALIC),
62 is_underline: font_style.contains(highlighting::FontStyle::UNDERLINE),
63 is_strikethrough: false,
64 }
65 }
66}
67
68impl From<highlighting::Style> for Style {
69 fn from(style: highlighting::Style) -> Style {
70 Style {
71 fg: Some(style.foreground.into()),
72 bg: Some(style.background.into()),
73 effects: style.font_style.into(),
74 }
75 }
76}
77
78impl<'a, 'b> From<&'b (highlighting::Style, &'a str)> for StyledStr<'a> {
79 fn from((style, s): &'b (highlighting::Style, &'a str)) -> StyledStr<'a> {
80 StyledStr {
81 s,
82 style: Some(Style::from(*style)),
83 }
84 }
85}
86
87impl<'a> From<(highlighting::Style, &'a str)> for StyledStr<'a> {
88 fn from((style, s): (highlighting::Style, &'a str)) -> StyledStr<'a> {
89 StyledStr {
90 s,
91 style: Some(style.into()),
92 }
93 }
94}