A lightweight translation layer between syntect and ratatui style types. If you're building a CLI app with a UI powered by Ratatui and need syntax highlighting, then you may find this crate useful!
Given the limited scope of this crate I do not have plans to extend existing functionality much further. However, I am open to requests and/or contributions, so feel free to fork and submit a pull request.
syntect-tui
is available on crates.io. You can install it by adding the following line to your Cargo.toml
:
syntect-tui = "3.0"
For more usage information read the docs
Building upon syntect's simple example, here's a snippet that parses some rust code, highlights it using syntect and converts it into ratatui::text::Line ready for rendering in a tui appliction:
use ratatui::text::{Line, Span};
use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;
use syntect::util::LinesWithEndings;
use syntect_tui::into_span;
const EXAMPLE: &str = "
pub struct Wow {
hi: u64
}
fn blah() -> u64 {}
";
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
for line in LinesWithEndings::from(EXAMPLE) {
// LinesWithEndings enables use of newlines mode
let spans: Vec<Span> = h
.highlight_line(line, &ps)
.unwrap()
.into_iter()
.filter_map(|segment| into_span(segment).ok())
.collect();
let line = Line::from(spans);
println!("{:?}", line);
}
All code is released under the MIT License. Thanks to trishume,
fdehau, and the ratatui
community for building sytect
, tui
, and ratatui
!
Also a big thank you to fellow rustaceans who have contributed to the maintenance of this crate: