Skip to main content

Rule

Struct Rule 

Source
pub struct Rule { /* private fields */ }
Expand description

A horizontal divider line, optionally labelled with a title.

§Examples

use sparcli::{Renderable, Rule};

let out = Rule::with_title("Settings").render(30);
assert!(out.plain().contains("Settings"));

Implementations§

Source§

impl Rule

Source

pub fn new() -> Self

Creates a plain rule with no title.

Source

pub fn with_title(title: impl Into<Text>) -> Self

Creates a rule with an embedded title.

Examples found in repository?
examples/output_gallery.rs (line 35)
33fn section(title: &str) -> Result<()> {
34    println!();
35    Rule::with_title(title).align(Align::Left).print()?;
36    println!();
37    Ok(())
38}
39
40/// Styled spans, attributes, an OSC-8 hyperlink and optional markup.
41fn styled_text() -> Result<()> {
42    Rule::with_title("sparcli output gallery").print()?;
43    println!();
44    Rendered::new(vec![Line::new(vec![
45        Span::styled("bold ", Style::new().bold()),
46        Span::styled("dim ", Style::new().dim()),
47        Span::styled("italic ", Style::new().italic()),
48        Span::styled("red ", Style::new().fg(Color::Red)),
49        Span::styled("on-blue ", Style::new().bg(Color::Blue)),
50        Span::raw("link: "),
51        Span::raw("sparcli").link("https://example.com/sparcli"),
52    ])])
53    .print()?;
54
55    #[cfg(feature = "markup")]
56    markup_println("[bold green]markup[/]: [#ff8800]orange[/] and `code`")?;
57    Ok(())
58}
59
60/// Rules in all three alignments.
61fn sections() -> Result<()> {
62    section("Rules")?;
63    Rule::with_title("left")
64        .align(Align::Left)
65        .border(BorderType::Single)
66        .print()?;
67    Rule::with_title("center").align(Align::Center).print()?;
68    Rule::with_title("right")
69        .align(Align::Right)
70        .border(BorderType::Thick)
71        .print()?;
72    Ok(())
73}
More examples
Hide additional examples
examples/output-readme.rs (line 163)
162fn rule(title: &str, border: BorderType, color: Color) -> Rendered {
163    Rule::with_title(Text::styled(title, Style::new().fg(color).bold()))
164        .border(border)
165        .style(Style::new().fg(color))
166        .width(20)
167        .render(20)
168}
Source

pub fn border(self, border: BorderType) -> Self

Sets the line style (border type).

Examples found in repository?
examples/output-readme.rs (line 164)
162fn rule(title: &str, border: BorderType, color: Color) -> Rendered {
163    Rule::with_title(Text::styled(title, Style::new().fg(color).bold()))
164        .border(border)
165        .style(Style::new().fg(color))
166        .width(20)
167        .render(20)
168}
More examples
Hide additional examples
examples/output_gallery.rs (line 65)
61fn sections() -> Result<()> {
62    section("Rules")?;
63    Rule::with_title("left")
64        .align(Align::Left)
65        .border(BorderType::Single)
66        .print()?;
67    Rule::with_title("center").align(Align::Center).print()?;
68    Rule::with_title("right")
69        .align(Align::Right)
70        .border(BorderType::Thick)
71        .print()?;
72    Ok(())
73}
Source

pub fn style(self, style: Style) -> Self

Sets the line/glyph style.

Examples found in repository?
examples/output-readme.rs (line 165)
162fn rule(title: &str, border: BorderType, color: Color) -> Rendered {
163    Rule::with_title(Text::styled(title, Style::new().fg(color).bold()))
164        .border(border)
165        .style(Style::new().fg(color))
166        .width(20)
167        .render(20)
168}
Source

pub fn align(self, align: Align) -> Self

Sets the title alignment.

Examples found in repository?
examples/output_gallery.rs (line 35)
33fn section(title: &str) -> Result<()> {
34    println!();
35    Rule::with_title(title).align(Align::Left).print()?;
36    println!();
37    Ok(())
38}
39
40/// Styled spans, attributes, an OSC-8 hyperlink and optional markup.
41fn styled_text() -> Result<()> {
42    Rule::with_title("sparcli output gallery").print()?;
43    println!();
44    Rendered::new(vec![Line::new(vec![
45        Span::styled("bold ", Style::new().bold()),
46        Span::styled("dim ", Style::new().dim()),
47        Span::styled("italic ", Style::new().italic()),
48        Span::styled("red ", Style::new().fg(Color::Red)),
49        Span::styled("on-blue ", Style::new().bg(Color::Blue)),
50        Span::raw("link: "),
51        Span::raw("sparcli").link("https://example.com/sparcli"),
52    ])])
53    .print()?;
54
55    #[cfg(feature = "markup")]
56    markup_println("[bold green]markup[/]: [#ff8800]orange[/] and `code`")?;
57    Ok(())
58}
59
60/// Rules in all three alignments.
61fn sections() -> Result<()> {
62    section("Rules")?;
63    Rule::with_title("left")
64        .align(Align::Left)
65        .border(BorderType::Single)
66        .print()?;
67    Rule::with_title("center").align(Align::Center).print()?;
68    Rule::with_title("right")
69        .align(Align::Right)
70        .border(BorderType::Thick)
71        .print()?;
72    Ok(())
73}
Source

pub fn width(self, width: u16) -> Self

Sets a fixed width in columns.

Examples found in repository?
examples/output-readme.rs (line 166)
162fn rule(title: &str, border: BorderType, color: Color) -> Rendered {
163    Rule::with_title(Text::styled(title, Style::new().fg(color).bold()))
164        .border(border)
165        .style(Style::new().fg(color))
166        .width(20)
167        .render(20)
168}
Source

pub fn margin(self, margin: Edges) -> Self

Sets the outer margin.

Trait Implementations§

Source§

impl Default for Rule

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Renderable for Rule

Source§

fn render(&self, max_width: u16) -> Rendered

Lays the value out for a terminal at most max_width columns wide.
Source§

fn print(&self) -> Result<()>

Renders at the current terminal width and prints to standard output. Read more
Source§

fn print_to<W: Write>(&self, writer: &mut W) -> Result<()>

Renders at the current terminal width and writes to writer. Read more

Auto Trait Implementations§

§

impl Freeze for Rule

§

impl RefUnwindSafe for Rule

§

impl Send for Rule

§

impl Sync for Rule

§

impl Unpin for Rule

§

impl UnsafeUnpin for Rule

§

impl UnwindSafe for Rule

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.