Skip to main content

Style

Struct Style 

Source
pub struct Style { /* private fields */ }
Available on crate feature std only.
Expand description

A piece of text together with the color and attributes to render it with.

Created by style. The setter methods consume and return self, so they chain. Style implements Display: passing one to out renders it, and the color depth matches whatever the terminal supports.

§Examples

use cli_forge::{out, style};

out(style("done").green().bold());
out(style("note").hex("#88aaff"));
out(style("ok").rgb(0, 200, 120));

Implementations§

Source§

impl Style

Source

pub fn black(self) -> Style

Set the foreground to the standard black.

Source

pub fn red(self) -> Style

Set the foreground to the standard red.

Source

pub fn green(self) -> Style

Set the foreground to the standard green.

Source

pub fn yellow(self) -> Style

Set the foreground to the standard yellow.

Source

pub fn blue(self) -> Style

Set the foreground to the standard blue.

Source

pub fn magenta(self) -> Style

Set the foreground to the standard magenta.

Source

pub fn cyan(self) -> Style

Set the foreground to the standard cyan.

Source

pub fn white(self) -> Style

Set the foreground to the standard white.

Source

pub fn hex(self, hex: &str) -> Style

Set the foreground to a 24-bit hex color, e.g. "#ff8800".

The leading # is optional; the rest must be exactly six hex digits. An invalid string leaves the current color unchanged, so the builder never fails. On terminals without 24-bit support the color is downgraded to the nearest representable value at render time.

§Examples
use cli_forge::style;

let link = style("https://example.com").hex("#3b82f6").underline();
assert!(link.render().contains("https://example.com"));

// An invalid hex string is ignored rather than panicking.
let plain = style("x").hex("nope");
assert_eq!(plain.render(), "x");
Source

pub fn rgb(self, r: u8, g: u8, b: u8) -> Style

Set the foreground to a 24-bit RGB color.

On terminals without 24-bit support the color is downgraded to the nearest representable value at render time.

§Examples
use cli_forge::style;

let teal = style("ok").rgb(0, 200, 120);
assert!(teal.render().contains("ok"));
Source

pub fn bold(self) -> Style

Render the text in bold.

§Examples
use cli_forge::style;

let heading = style("Summary").bold();
assert!(heading.render().contains("Summary"));
Source

pub fn underline(self) -> Style

Underline the text.

§Examples
use cli_forge::style;

let link = style("docs").underline();
assert!(link.render().contains("docs"));
Source

pub fn render(&self) -> String

Render to an owned String, ready to print or store.

Equivalent to formatting the Style via its Display implementation. The color depth matches the terminal detected for standard output, so on a pipe or a NO_COLOR environment the result is the plain text.

§Examples
use cli_forge::style;

let s = style("ready").green().render();
assert!(s.contains("ready"));

Trait Implementations§

Source§

impl Clone for Style

Source§

fn clone(&self) -> Style

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Style

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Style

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin for Style

§

impl UnsafeUnpin for Style

§

impl UnwindSafe for Style

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.