pub struct Template { /* private fields */ }svg only.Expand description
Template for rendering Transcripts, e.g. into an SVG image.
§Available templates
When using a template created with Self::new(), a transcript is rendered into SVG
with the text content embedded as an HTML fragment. This is because SVG is not good
at laying out multiline texts and text backgrounds, while HTML excels at both.
As a downside of this approach, the resulting SVG requires for its viewer to support
HTML embedding; while web browsers a priori support such embedding, some other SVG viewers
may not.
A template created with Self::pure_svg() renders a transcript into pure SVG,
in which text is laid out manually. The resulting SVG is
supported by more viewers, but it may look incorrectly in certain corner cases.
Snapshot testing functionality produces snapshots using Self::new()
(i.e., with HTML embedding); pure SVG templates cannot be tested.
§Customization
A custom Handlebars template can be supplied via Self::custom(). This can be used
to partially or completely change rendering logic, including the output format (e.g.,
to render to HTML instead of SVG).
Data supplied to a template is HandlebarsData.
Besides built-in Handlebars helpers (a superset of standard helpers), custom templates have access to the following additional helpers. All the helpers are extensively used by the default template; thus, studying it may be a good place to start customizing. Another example is an HTML template from the crate examples.
§Arithmetic operations: add, sub, mul, div
Perform the specified arithmetic operation on the supplied args.
add and mul support any number of numeric args; sub and div exactly 2 numeric args.
{{add 2 3 5}}
{{div (len xs) 3}}§Rounding
Rounds the provided value with a configurable number of decimal digits. Also allows specifying the rounding mode: up / ceil, down / floor, or nearest / round (default).
{{round 7.8}} {{! 8 }}
{{round 7.13 digits=1}} {{! 7.1 }}
{{round 7.13 digits=1 mode="up"}} {{! 7.2 }}§Counting lines: count_lines
Counts the number of lines in the supplied string.
{{count_lines test}}§Integer ranges: range
Creates an array with integers in the range specified by the 2 provided integer args. The “from” bound is inclusive, the “to” one is exclusive.
{{#each (range 0 3)}}{{@index}}, {{/each}}
{{! Will output `0, 1, 2,` }}§Variable scope: scope, set
A block helper that creates a scope with variables specified in the options hash.
In the block, each variable can be obtained using local variable syntax (e.g., @test).
Variables can be set with the set helper:
- If
setis called as a block helper, the variable is set to the contents of the block, which is treated as JSON. - If
setis called as a block helper withappend=true, then the contents of the block is appended to the var, which must be a string. - If the
sethelper is called as an inline helper, it sets values of the listed variables.
{{#scope test=""}}
{{set test="Hello"}}
{{@test}} {{! Outputs `Hello` }}
{{#set "test"}}"{{@test}}, world!"{{/set}}
{{@test}} {{! Outputs `Hello, world!` }}
{{/scope}}§Examples
use term_transcript::{svg::*, Transcript, UserInput};
let mut transcript = Transcript::new();
transcript.add_interaction(
UserInput::command("test"),
"Hello, \u{1b}[32mworld\u{1b}[0m!",
);
let template_options = TemplateOptions {
palette: NamedPalette::Dracula.into(),
..TemplateOptions::default()
}
.validated()?;
let mut buffer = vec![];
Template::new(template_options).render(&transcript, &mut buffer)?;
let buffer = String::from_utf8(buffer)?;
assert!(buffer.contains(r#"Hello, <span class="fg2">world</span>!"#));Implementations§
Source§impl Template
impl Template
Sourcepub fn new(options: ValidTemplateOptions) -> Self
pub fn new(options: ValidTemplateOptions) -> Self
Initializes the default template based on provided options.
Sourcepub fn pure_svg(options: ValidTemplateOptions) -> Self
pub fn pure_svg(options: ValidTemplateOptions) -> Self
Initializes the pure SVG template based on provided options.
Sourcepub fn custom(
template: HandlebarsTemplate,
options: ValidTemplateOptions,
) -> Self
pub fn custom( template: HandlebarsTemplate, options: ValidTemplateOptions, ) -> Self
Initializes a custom template.
Sourcepub fn render<W: Write>(
&self,
transcript: &Transcript,
destination: W,
) -> Result<(), RenderError>
pub fn render<W: Write>( &self, transcript: &Transcript, destination: W, ) -> Result<(), RenderError>
Renders the transcript using the template (usually as an SVG image, although
custom templates may use a different output format).
§Errors
Returns a Handlebars rendering error, if any. Normally, the only errors could be related to I/O (e.g., the output cannot be written to a file).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Template
impl !RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl !UnwindSafe for Template
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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);