pub struct Syntax {Show 15 fields
pub code: String,
pub lexer_name: String,
pub theme: String,
pub line_numbers: bool,
pub start_line: usize,
pub line_range: Option<(usize, usize)>,
pub word_wrap: bool,
pub tab_size: usize,
pub padding: (usize, usize, usize, usize),
pub highlight_lines: HashSet<usize>,
pub background_color: Option<String>,
pub indent_guides: bool,
pub code_width: Option<usize>,
pub dedent: bool,
pub style_ranges: Vec<(Style, Range<usize>)>,
}Expand description
Syntax-highlighted code display.
Renders code with syntax highlighting, optional line numbers, word wrap, theme selection, and more.
Fields§
§code: StringThe source code to highlight.
lexer_name: StringLanguage name for syntax lookup (e.g., “rust”, “python”, “json”).
theme: StringTheme name (e.g., “base16-ocean.dark”, “base16-mocha.dark”).
line_numbers: boolWhether to display line numbers.
start_line: usizeStarting line number (default 1).
line_range: Option<(usize, usize)>Optional (start, end) line range to display (1-based, inclusive).
word_wrap: boolWhether to wrap long lines.
tab_size: usizeTab width for tab expansion (default 4).
padding: (usize, usize, usize, usize)(top, right, bottom, left) padding (top/bottom in blank lines, left/right
in spaces inside the code area). Mirrors the CSS shorthand convention used
by Padding and matches rich v14.1.0+ Syntax.padding (4-tuple).
Use Self::with_padding / unpack_padding to construct from any
shorthand variant: n → (n, n, n, n), (v, h) → (v, h, v, h),
(t, h, b) → (t, h, b, h), (t, r, b, l) → as-is.
highlight_lines: HashSet<usize>Line numbers to highlight with a special background.
Stored as a HashSet for O(1) lookup per rendered line.
background_color: Option<String>Optional override for background color (CSS hex like “#282c34”).
indent_guides: boolWhether to show indent guides.
code_width: Option<usize>Fixed width for code area (excluding line numbers), or None for auto.
dedent: boolWhether to auto-dedent code by stripping common leading whitespace.
style_ranges: Vec<(Style, Range<usize>)>Style ranges to apply on top of syntax highlighting. Each entry is a (style, character_range) pair applied during rendering.
Implementations§
Source§impl Syntax
impl Syntax
Sourcepub fn new(code: &str, lexer_name: &str) -> Self
pub fn new(code: &str, lexer_name: &str) -> Self
Create a new Syntax with defaults: base16-mocha.dark theme (Monokai-like), no line numbers.
Sourcepub fn from_path(path: &str) -> Result<Self, SyntaxError>
pub fn from_path(path: &str) -> Result<Self, SyntaxError>
Create a Syntax by reading a file and auto-detecting the language from its extension.
Sourcepub fn with_theme(self, theme: &str) -> Self
pub fn with_theme(self, theme: &str) -> Self
Set the theme.
Sourcepub fn with_line_numbers(self, line_numbers: bool) -> Self
pub fn with_line_numbers(self, line_numbers: bool) -> Self
Enable or disable line numbers.
Sourcepub fn with_start_line(self, start: usize) -> Self
pub fn with_start_line(self, start: usize) -> Self
Set the starting line number.
Sourcepub fn with_line_range(self, range: (usize, usize)) -> Self
pub fn with_line_range(self, range: (usize, usize)) -> Self
Set the line range to display (1-based, inclusive).
Sourcepub fn with_word_wrap(self, wrap: bool) -> Self
pub fn with_word_wrap(self, wrap: bool) -> Self
Enable or disable word wrap.
Sourcepub fn with_tab_size(self, size: usize) -> Self
pub fn with_tab_size(self, size: usize) -> Self
Set the tab size for tab expansion.
Sourcepub fn with_highlight_lines(
self,
lines: impl IntoIterator<Item = usize>,
) -> Self
pub fn with_highlight_lines( self, lines: impl IntoIterator<Item = usize>, ) -> Self
Set which line numbers to highlight.
Accepts any iterator or Vec; lines are stored in a HashSet for O(1)
per-line lookup during rendering.
Sourcepub fn with_indent_guides(self, guides: bool) -> Self
pub fn with_indent_guides(self, guides: bool) -> Self
Enable or disable indent guides.
Sourcepub fn with_code_width(self, width: usize) -> Self
pub fn with_code_width(self, width: usize) -> Self
Set a fixed code width.
Sourcepub fn with_dedent(self, dedent: bool) -> Self
pub fn with_dedent(self, dedent: bool) -> Self
Enable or disable auto-dedent of common leading whitespace.
Sourcepub fn with_padding(self, spec: PaddingSpec) -> Self
pub fn with_padding(self, spec: PaddingSpec) -> Self
Set padding from a PaddingSpec shorthand, expanding to the full
(top, right, bottom, left) tuple stored in self.padding.
let s = Syntax::new("fn main() {}", "rs")
.with_padding(PaddingSpec::VertHoriz(1, 2));
assert_eq!(s.padding, (1, 2, 1, 2));Sourcepub fn stylize_range(&mut self, style: Style, range: Range<usize>)
pub fn stylize_range(&mut self, style: Style, range: Range<usize>)
Add a style to apply over a flat character range of the code.
The range is in terms of character offsets into the original code string (after dedent, if enabled). Multiple ranges may overlap; they are applied in order on top of the syntax highlighting.
Sourcepub fn stylize_range_linecol(
&mut self,
style: Style,
start: (usize, usize),
end: (usize, usize),
)
pub fn stylize_range_linecol( &mut self, style: Style, start: (usize, usize), end: (usize, usize), )
Add a style to apply over a (line, col) range of the code.
Both start and end are (line, col) pairs where line is 1-based and
col is 0-based character offset within the line. The pair is converted
to a flat character offset and delegated to stylize_range.
Matches the start=(line, col), end=(line, col) form used by Python rich.
Sourcepub fn measure(&self) -> Measurement
pub fn measure(&self) -> Measurement
Measure the width required to render this Syntax.
Respects line_range when set — only visible lines contribute to the
maximum width, matching what render_syntax actually outputs.
Trait Implementations§
Source§impl Renderable for Syntax
Implement the Renderable trait so Syntax can be printed by Console.
impl Renderable for Syntax
Implement the Renderable trait so Syntax can be printed by Console.
Source§fn gilt_console(
&self,
_console: &Console,
options: &ConsoleOptions,
) -> Vec<Segment>
fn gilt_console( &self, _console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>
Auto Trait Implementations§
impl Freeze for Syntax
impl RefUnwindSafe for Syntax
impl Send for Syntax
impl Sync for Syntax
impl Unpin for Syntax
impl UnsafeUnpin for Syntax
impl UnwindSafe for Syntax
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> RenderableExt for Twhere
T: Renderable + 'static,
impl<T> RenderableExt for Twhere
T: Renderable + 'static,
Source§fn into_boxed_renderable(self) -> RenderableBox
fn into_boxed_renderable(self) -> RenderableBox
RenderableBox for type-erased storage. Read moreSource§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more