pub fn translate_style(syntect_style: Style) -> Result<Style, SyntectTuiError>
Expand description

Converts a syntect::highlighting::Style into a ratatui::style::Style.

Syntect colours are RGBA while Ratatui colours are RGB, so colour conversion is lossy. However, if a Syntect colour’s alpha value is 0, then we preserve this to some degree by returning a value of None for that colour (i.e. its colourless).

§Examples

Basic usage:

let input = syntect::highlighting::Style {
    foreground: syntect::highlighting::Color { r: 255, g: 0, b: 0, a: 255 },
    background: syntect::highlighting::Color { r: 0, g: 0, b: 0, a: 0 },
    font_style: syntect::highlighting::FontStyle::BOLD
};
let expected = ratatui::style::Style {
    fg: Some(ratatui::style::Color::Rgb(255, 0, 0)),
    bg: None,
    underline_color: Some(ratatui::style::Color::Rgb(255, 0, 0)),
    add_modifier: ratatui::style::Modifier::BOLD,
    sub_modifier: ratatui::style::Modifier::empty()
};
let actual = syntect_tui::translate_style(input).unwrap();
assert_eq!(expected, actual);

§Errors

Can return SyntectTuiError::UnknownFontStyle if the input FontStyle is not supported.

All explicit compositions of BOLD, ITALIC & UNDERLINE are supported, however, implicit bitflag coercions are not. For example, even though FontStyle::from_bits(3) is coerced to Some(FontStyle::BOLD | FontStyle::ITALIC), we ignore this result as it would be a pain to handle all implicit coercions.