pub struct SyntaxHighlighter { /* private fields */ }Expand description
Tree-sitter-based syntax highlighter for TUI code blocks.
Supports Rust, Python, JavaScript, JSON, TOML, and Bash out of the box.
Language aliases ("rs" → "rust", "sh" → "bash", etc.) are
resolved transparently.
Construct via the SYNTAX_HIGHLIGHTER static for process-level sharing,
or call the private new method directly in tests.
§Supported languages
| Identifier | Aliases |
|---|---|
rust | rs |
python | py |
javascript | js |
bash | sh, shell |
json | — |
toml | — |
§Examples
use zeph_tui::highlight::SYNTAX_HIGHLIGHTER;
use zeph_tui::theme::SyntaxTheme;
let theme = SyntaxTheme::default();
// Known language → styled spans
let spans = SYNTAX_HIGHLIGHTER.highlight("rust", "fn main() {}", &theme);
assert!(spans.is_some());
// Alias works the same way
let spans = SYNTAX_HIGHLIGHTER.highlight("rs", "let x = 1;", &theme);
assert!(spans.is_some());
// Unknown language → None
assert!(SYNTAX_HIGHLIGHTER.highlight("brainfuck", "+++", &theme).is_none());Implementations§
Source§impl SyntaxHighlighter
impl SyntaxHighlighter
Sourcepub fn highlight(
&self,
lang: &str,
code: &str,
theme: &SyntaxTheme,
) -> Option<Vec<Span<'static>>>
pub fn highlight( &self, lang: &str, code: &str, theme: &SyntaxTheme, ) -> Option<Vec<Span<'static>>>
Highlight code for the given lang using theme.
Returns None if the language is unsupported or if tree-sitter fails
to parse the input. The returned spans concatenate to the original
source text unchanged.
§Arguments
lang— language identifier or alias (case-insensitive).code— source code to highlight.theme— style mapping for each token class.
§Examples
use zeph_tui::highlight::SYNTAX_HIGHLIGHTER;
use zeph_tui::theme::SyntaxTheme;
let theme = SyntaxTheme::default();
let spans = SYNTAX_HIGHLIGHTER.highlight("rust", "let x = 42;", &theme).unwrap();
let text: String = spans.iter().map(|s| s.content.as_ref()).collect();
assert_eq!(text, "let x = 42;");Auto Trait Implementations§
impl Freeze for SyntaxHighlighter
impl RefUnwindSafe for SyntaxHighlighter
impl Send for SyntaxHighlighter
impl Sync for SyntaxHighlighter
impl Unpin for SyntaxHighlighter
impl UnsafeUnpin for SyntaxHighlighter
impl UnwindSafe for SyntaxHighlighter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request