#syntect #syntax-highlighting #tui #ratatui #translation #layer #ui

syntect-tui

A lightweight translation layer between syntect.rs and tui.rs style types

7 stable releases

new 3.0.3 Jul 25, 2024
3.0.2 Mar 8, 2024
3.0.1 Jan 7, 2024
2.0.0 Oct 12, 2023
1.0.1 Dec 27, 2022

#235 in Command-line interface

Download history 161/week @ 2024-04-01 207/week @ 2024-04-08 72/week @ 2024-04-15 78/week @ 2024-04-22 113/week @ 2024-04-29 117/week @ 2024-05-06 55/week @ 2024-05-13 81/week @ 2024-05-20 57/week @ 2024-05-27 70/week @ 2024-06-03 34/week @ 2024-06-10 25/week @ 2024-06-17 42/week @ 2024-06-24 43/week @ 2024-07-01 73/week @ 2024-07-08 55/week @ 2024-07-15

216 downloads per month
Used in 4 crates

MIT license

18KB
156 lines

syntect-tui Build Status

A lightweight translation layer between syntect and ratatui style types. If you're building a CLI app with a UI powered by tui.rs 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.

Getting Started

syntect-tui is available on crates.io. You can install it by adding the following line to your Cargo.toml:

syntect-tui = "3.0"

Docs

For more usage information read the docs

Example Code

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 syntect::easy::HighlightLines;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::{ThemeSet, Style};
use syntect::util::LinesWithEndings;
use syntui::into_span;

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"]);
let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
for line in LinesWithEndings::from(s) { // LinesWithEndings enables use of newlines mode
    let line_spans: Vec<tui::text::Span> =
        h.highlight_line(line, &ps)
         .unwrap()
         .into_iter()
         .filter_map(|segment| into_span(segment).ok())
         .collect();
    let spans = tui::text::Spans::from(line_spans);
    print!("{:?}", spans);
}

Licence & Acknowledgements

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:

Dependencies

~8–16MB
~179K SLoC