pub struct Syntax {
pub code: String,
pub language: String,
pub theme: String,
pub start_line: usize,
pub line_numbers: bool,
pub highlight: bool,
pub background_color: Option<Color>,
pub tab_size: usize,
pub line_styles: HashMap<usize, Style>,
}Expand description
A syntax-highlighted source code renderable.
Fields§
§code: StringThe source code.
language: StringThe language name (e.g. “rust”, “python”, “javascript”).
theme: StringOptional theme name.
start_line: usizeStarting line number (for line numbers).
line_numbers: boolIf true, show line numbers.
highlight: boolIf true, highlight the code.
background_color: Option<Color>Optional background color.
tab_size: usizeTab size.
line_styles: HashMap<usize, Style>Per-line styles for line range highlighting (used by stylize_range).
Implementations§
Source§impl Syntax
impl Syntax
Sourcepub fn new(code: impl Into<String>, language: impl Into<String>) -> Self
pub fn new(code: impl Into<String>, language: impl Into<String>) -> Self
Create a new Syntax renderable for the given code and language.
Sourcepub fn theme(self, theme: impl Into<String>) -> Self
pub fn theme(self, theme: impl Into<String>) -> Self
Builder: set the syntect theme name (e.g. "base16-ocean.dark", "monokai").
Sourcepub fn line_numbers(self) -> Self
pub fn line_numbers(self) -> Self
Builder: enable line numbers in the rendered output.
Sourcepub fn start_line(self, n: usize) -> Self
pub fn start_line(self, n: usize) -> Self
Builder: set the starting line number for display (default 1).
Sourcepub fn background(self, color: Color) -> Self
pub fn background(self, color: Color) -> Self
Builder: set a background color for the code block.
Sourcepub fn from_path(
path: impl AsRef<Path>,
line_numbers: bool,
theme: Option<&str>,
) -> Result<Self>
pub fn from_path( path: impl AsRef<Path>, line_numbers: bool, theme: Option<&str>, ) -> Result<Self>
Create a Syntax from a file path, auto-detecting the language from the extension.
Reads the file contents and infers the programming language from the file extension. Optionally enables line numbers and sets a theme.
§Errors
Returns an IO error if the file cannot be read.
§Example
use rusty_rich::Syntax;
let syntax = Syntax::from_path("main.rs", true, Some("monokai")).unwrap();Sourcepub fn guess_lexer(path: impl AsRef<Path>) -> Option<String>
pub fn guess_lexer(path: impl AsRef<Path>) -> Option<String>
Guess the syntax lexer name from a file path’s extension.
Delegates to guess_lexer_for_filename by extracting the file stem
and extension from the provided path.
Sourcepub fn stylize_range(
self,
start_line: usize,
end_line: usize,
style: Style,
) -> Self
pub fn stylize_range( self, start_line: usize, end_line: usize, style: Style, ) -> Self
Apply a background style to a range of lines (for highlighting).
Returns a new Syntax with the style applied to the specified lines
(1-based, inclusive). This is useful for highlighting a specific range
of lines, e.g. the current line in a debugger.
§Example
use rusty_rich::{Syntax, Style, Color};
let syntax = Syntax::new("line1\nline2", "text")
.stylize_range(1, 1, Style::new().bgcolor(Color::from_rgb(255, 255, 200)));Sourcepub fn default_lexer() -> &'static str
pub fn default_lexer() -> &'static str
Return the default lexer name ("text").
Trait Implementations§
Source§impl Renderable for Syntax
impl Renderable for Syntax
Source§fn render(&self, _options: &ConsoleOptions) -> RenderResult
fn render(&self, _options: &ConsoleOptions) -> RenderResult
RenderResult using the provided options. Read moreSource§fn measure(&self, _options: &ConsoleOptions) -> Option<Measurement>
fn measure(&self, _options: &ConsoleOptions) -> Option<Measurement>
__rich_measure__).
Override to provide min/max width constraints for layout.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<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more