pub struct CodeBlock<'a> { /* private fields */ }Expand description
A themed, syntax-highlighted code block — a language label, a left rail, a code background, and verbatim (never-reflowed) body lines.

Colors come entirely from Theme::code; token classes are
resolved by whatever Highlighter you plug in (none →
plain, theme-colored text).
§Options
| Builder | Default | Effect |
|---|---|---|
new(lang, source) | — | language tag + source (split on \n) |
highlighter(&h) | plain | plug in syntax highlighting |
label(bool) | true | show/hide the language-label row |
line_numbers(bool) | false | show a line-number gutter |
start_line(n) | 1 | first gutter line number |
use tuika::prelude::*;
let theme = Theme::default();
// No highlighter → plain, theme-colored code; hide the label row.
let block = CodeBlock::new("rust", "fn main() {}").label(false);
// `block` is a `View`; render it through `tuika::paint` or embed it in a
// `Flex`. Supply a highlighter with `.highlighter(&my_highlighter)` — see
// the `tuika-codeformatters` crate for a tree-sitter one.Implementations§
Source§impl<'a> CodeBlock<'a>
impl<'a> CodeBlock<'a>
Sourcepub fn new(lang: impl Into<String>, source: impl AsRef<str>) -> Self
pub fn new(lang: impl Into<String>, source: impl AsRef<str>) -> Self
A code block for source in language lang (e.g. "rust", "py", or
"" for no language). source is split on newlines into body lines.
Sourcepub fn highlighter(self, highlighter: &'a dyn Highlighter) -> Self
pub fn highlighter(self, highlighter: &'a dyn Highlighter) -> Self
Plug in a syntax highlighter; without one the body renders plain.
Sourcepub fn line_numbers(self, show: bool) -> Self
pub fn line_numbers(self, show: bool) -> Self
Show (or hide) a right-aligned line-number gutter before the rail. The
gutter counts from start_line (default 1).
Sourcepub fn start_line(self, first: usize) -> Self
pub fn start_line(self, first: usize) -> Self
Set the first gutter line number, implying line_numbers(true).
Useful when a block is a slice of a larger file.
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for CodeBlock<'a>
impl<'a> !Send for CodeBlock<'a>
impl<'a> !Sync for CodeBlock<'a>
impl<'a> !UnwindSafe for CodeBlock<'a>
impl<'a> Freeze for CodeBlock<'a>
impl<'a> Unpin for CodeBlock<'a>
impl<'a> UnsafeUnpin for CodeBlock<'a>
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> 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 more