Skip to main content

KeyValue

Struct KeyValue 

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

A list of aligned key-value pairs.

§Examples

use sparcli::{KeyValue, Renderable};

let out = KeyValue::new()
    .add("name", "sparcli")
    .add("license", "MIT")
    .render(40);
assert!(out.plain().contains("sparcli"));

Implementations§

Source§

impl KeyValue

Source

pub fn new() -> Self

Creates an empty key-value list.

Examples found in repository?
examples/output_gallery.rs (line 165)
163fn key_values_and_badges() -> Result<()> {
164    section("Key-value & badges")?;
165    KeyValue::new()
166        .add("Version", "0.1.0")
167        .add("License", "MIT")
168        .print()?;
169    println!();
170    Rendered::new(vec![Line::new(vec![
171        Badge::new("PASS")
172            .style(Style::new().fg(Color::Green).bold())
173            .span(),
174        Span::raw(" "),
175        Badge::new("WARN")
176            .style(Style::new().fg(Color::Yellow).bold())
177            .span(),
178        Span::raw(" "),
179        Badge::new("v0.1").caps("(", ")").span(),
180    ])])
181    .print()?;
182    Ok(())
183}
More examples
Hide additional examples
examples/output-readme.rs (line 172)
171fn right_column() -> Rendered {
172    let kv = KeyValue::new()
173        .key_style(Style::new().fg(Color::Cyan).bold())
174        .value_style(Style::new())
175        .add("host", "localhost")
176        .add("port", "8080")
177        .add("scheme", "https")
178        .add("timeout", "30s")
179        .render(24);
180
181    let badge = |text: &str, color: Color| {
182        Badge::new(text)
183            .pad(1)
184            .style(Style::new().fg(Color::Black).bg(color).bold())
185            .span()
186    };
187    let badges = Rendered::new(vec![
188        Line::new(vec![
189            badge("DONE", Color::Green),
190            Span::raw(" "),
191            badge("INFO", Color::LightBlue),
192        ]),
193        Line::default(),
194        Line::new(vec![
195            badge("WARN", Color::Yellow),
196            Span::raw(" "),
197            badge("FAIL", Color::Red),
198        ]),
199    ]);
200
201    let label = |text: &str| {
202        Rendered::new(vec![Line::styled(
203            text,
204            Style::new().fg(Color::Cyan).bold(),
205        )])
206    };
207    let staggered = Columns::new()
208        .add_rendered(label("Col 1"))
209        .add_rendered(vstack(&[blank(), label("Col 2")], 0))
210        .add_rendered(vstack(&[blank(), blank(), label("Col 3")], 0))
211        .gap(2)
212        .separator(BorderType::Single)
213        .render(40);
214
215    vstack(&[kv, blank(), badges, blank(), staggered], 0)
216}
Source

pub fn add(self, key: impl Into<String>, value: impl Into<Text>) -> Self

Adds a key-value pair.

Examples found in repository?
examples/output_gallery.rs (line 166)
163fn key_values_and_badges() -> Result<()> {
164    section("Key-value & badges")?;
165    KeyValue::new()
166        .add("Version", "0.1.0")
167        .add("License", "MIT")
168        .print()?;
169    println!();
170    Rendered::new(vec![Line::new(vec![
171        Badge::new("PASS")
172            .style(Style::new().fg(Color::Green).bold())
173            .span(),
174        Span::raw(" "),
175        Badge::new("WARN")
176            .style(Style::new().fg(Color::Yellow).bold())
177            .span(),
178        Span::raw(" "),
179        Badge::new("v0.1").caps("(", ")").span(),
180    ])])
181    .print()?;
182    Ok(())
183}
More examples
Hide additional examples
examples/output-readme.rs (line 175)
171fn right_column() -> Rendered {
172    let kv = KeyValue::new()
173        .key_style(Style::new().fg(Color::Cyan).bold())
174        .value_style(Style::new())
175        .add("host", "localhost")
176        .add("port", "8080")
177        .add("scheme", "https")
178        .add("timeout", "30s")
179        .render(24);
180
181    let badge = |text: &str, color: Color| {
182        Badge::new(text)
183            .pad(1)
184            .style(Style::new().fg(Color::Black).bg(color).bold())
185            .span()
186    };
187    let badges = Rendered::new(vec![
188        Line::new(vec![
189            badge("DONE", Color::Green),
190            Span::raw(" "),
191            badge("INFO", Color::LightBlue),
192        ]),
193        Line::default(),
194        Line::new(vec![
195            badge("WARN", Color::Yellow),
196            Span::raw(" "),
197            badge("FAIL", Color::Red),
198        ]),
199    ]);
200
201    let label = |text: &str| {
202        Rendered::new(vec![Line::styled(
203            text,
204            Style::new().fg(Color::Cyan).bold(),
205        )])
206    };
207    let staggered = Columns::new()
208        .add_rendered(label("Col 1"))
209        .add_rendered(vstack(&[blank(), label("Col 2")], 0))
210        .add_rendered(vstack(&[blank(), blank(), label("Col 3")], 0))
211        .gap(2)
212        .separator(BorderType::Single)
213        .render(40);
214
215    vstack(&[kv, blank(), badges, blank(), staggered], 0)
216}
Source

pub fn separator(self, separator: impl Into<String>) -> Self

Sets the separator between key and value.

Source

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

Sets a fixed key column width.

Source

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

Sets the key style.

Examples found in repository?
examples/output-readme.rs (line 173)
171fn right_column() -> Rendered {
172    let kv = KeyValue::new()
173        .key_style(Style::new().fg(Color::Cyan).bold())
174        .value_style(Style::new())
175        .add("host", "localhost")
176        .add("port", "8080")
177        .add("scheme", "https")
178        .add("timeout", "30s")
179        .render(24);
180
181    let badge = |text: &str, color: Color| {
182        Badge::new(text)
183            .pad(1)
184            .style(Style::new().fg(Color::Black).bg(color).bold())
185            .span()
186    };
187    let badges = Rendered::new(vec![
188        Line::new(vec![
189            badge("DONE", Color::Green),
190            Span::raw(" "),
191            badge("INFO", Color::LightBlue),
192        ]),
193        Line::default(),
194        Line::new(vec![
195            badge("WARN", Color::Yellow),
196            Span::raw(" "),
197            badge("FAIL", Color::Red),
198        ]),
199    ]);
200
201    let label = |text: &str| {
202        Rendered::new(vec![Line::styled(
203            text,
204            Style::new().fg(Color::Cyan).bold(),
205        )])
206    };
207    let staggered = Columns::new()
208        .add_rendered(label("Col 1"))
209        .add_rendered(vstack(&[blank(), label("Col 2")], 0))
210        .add_rendered(vstack(&[blank(), blank(), label("Col 3")], 0))
211        .gap(2)
212        .separator(BorderType::Single)
213        .render(40);
214
215    vstack(&[kv, blank(), badges, blank(), staggered], 0)
216}
Source

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

Sets the value style.

Examples found in repository?
examples/output-readme.rs (line 174)
171fn right_column() -> Rendered {
172    let kv = KeyValue::new()
173        .key_style(Style::new().fg(Color::Cyan).bold())
174        .value_style(Style::new())
175        .add("host", "localhost")
176        .add("port", "8080")
177        .add("scheme", "https")
178        .add("timeout", "30s")
179        .render(24);
180
181    let badge = |text: &str, color: Color| {
182        Badge::new(text)
183            .pad(1)
184            .style(Style::new().fg(Color::Black).bg(color).bold())
185            .span()
186    };
187    let badges = Rendered::new(vec![
188        Line::new(vec![
189            badge("DONE", Color::Green),
190            Span::raw(" "),
191            badge("INFO", Color::LightBlue),
192        ]),
193        Line::default(),
194        Line::new(vec![
195            badge("WARN", Color::Yellow),
196            Span::raw(" "),
197            badge("FAIL", Color::Red),
198        ]),
199    ]);
200
201    let label = |text: &str| {
202        Rendered::new(vec![Line::styled(
203            text,
204            Style::new().fg(Color::Cyan).bold(),
205        )])
206    };
207    let staggered = Columns::new()
208        .add_rendered(label("Col 1"))
209        .add_rendered(vstack(&[blank(), label("Col 2")], 0))
210        .add_rendered(vstack(&[blank(), blank(), label("Col 3")], 0))
211        .gap(2)
212        .separator(BorderType::Single)
213        .render(40);
214
215    vstack(&[kv, blank(), badges, blank(), staggered], 0)
216}
Source

pub fn item_gap(self, gap: u16) -> Self

Sets the number of blank lines between pairs.

Source

pub fn wrap_values(self, wrap: bool) -> Self

Enables wrapping of long values.

Source

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

Sets the outer margin.

Trait Implementations§

Source§

impl Default for KeyValue

Source§

fn default() -> Self

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

impl Renderable for KeyValue

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§

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.