Struct term_transcript::svg::Template
source · pub struct Template { /* private fields */ }
svg
only.Expand description
Template for rendering Transcript
s, 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 and backgrounds use a hack (lines of text with
appropriately colored █
chars placed behind the content lines). The resulting SVG is
supported by more viewers, but it may look incorrectly in certain corner cases. For example,
if the font family used in the template does not contain █
or some chars
used in the transcript, the background may be mispositioned.
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 helpers: 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.
div
also supports rounding via round
hash option. round=true
rounds to the nearest
integer; round="up"
/ round="down"
perform rounding in the specified direction.
{{add 2 3 5}}
{{div (len xs) 3 round="up"}}
§Counting lines: count_lines
Counts the number of lines in the supplied string. If format="html"
hash option is included,
line breaks introduced by <br/>
tags are also counted.
{{count_lines test}}
{{count_lines test format="html"}}
§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
A block helper that creates a scope with variables specified in the options hash. In the block, each variable can be obtained or set using an eponymous helper:
- If the variable helper is called as a block helper, the variable is set to the contents of the block, which is treated as JSON.
- If the variable helper is called as an inline helper with the
set
option, the variable is set to the value of the option. - Otherwise, the variable helper acts as a getter for the current value of the variable.
{{#scope test=""}}
{{test set="Hello"}}
{{test}} {{! Outputs `Hello` }}
{{#test}}{{test}}, world!{{/test}}
{{test}} {{! Outputs `Hello, world!` }}
{{/scope}}
Since variable getters are helpers, not “real” variables, they should be enclosed
in parentheses ()
if used as args / options for other helpers, e.g. {{add (test) 2}}
.
§Partial evaluation: eval
Evaluates a partial with the provided name and parses its output as JSON. This can be used to define “functions” for better code structuring. Function args can be supplied in options hash.
{{#*inline "some_function"}}
{{add x y}}
{{/inline}}
{{#with (eval "some_function" x=3 y=5) as |sum|}}
{{sum}} {{! Outputs 8 }}
{{/with}}
§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()
};
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: TemplateOptions) -> Self
pub fn new(options: TemplateOptions) -> Self
Initializes the default template based on provided options
.
sourcepub fn pure_svg(options: TemplateOptions) -> Self
pub fn pure_svg(options: TemplateOptions) -> Self
Initializes the pure SVG template based on provided options
.
sourcepub fn custom(template: HandlebarsTemplate, options: TemplateOptions) -> Self
pub fn custom(template: HandlebarsTemplate, options: TemplateOptions) -> 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);