Style

Struct Style 

Source
#[non_exhaustive]
pub struct Style { pub foreground: Option<Color>, pub background: Option<Color>, pub bold: Option<bool>, pub italic: Option<bool>, pub underline: Option<bool>, pub blink: Option<bool>, pub invert: Option<bool>, pub strikethrough: Option<bool>, }
Expand description

A text style, encompassing the color and other style options.

Each field of this struct is an Option. When the value is None, then the particular field is unspecified. By default, this is the same as setting it to Some(Default::default()). However, two style can be merged with either Style::with (or the + operator) or Style::or. This allows another style to override certain fields only if they are unspecified.

§Example

use line_ui::Style;

let style1 = Style::fg(1);
let style2 = Style::fg(2) + Style::BOLD;

assert_eq!(style1 + style2, style2);
assert_eq!(style1.with(style2), style2); // `with` is equivalent to `+`

assert_eq!(style2 + style1, Style::fg(1) + Style::BOLD);
assert_eq!(style1.or(style2), Style::fg(1) + Style::BOLD);
// `or` is equivalent to `+` with operands flipped

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§foreground: Option<Color>

The foreground color.

§background: Option<Color>

The background color.

§bold: Option<bool>

Whether the text should be bold.

§italic: Option<bool>

Whether the text should be italicized.

§underline: Option<bool>

Whether the text should be underlined.

§blink: Option<bool>

Whether the text should be blinking (not widely supported).

§invert: Option<bool>

Whether the text should have its colors inverted.

§strikethrough: Option<bool>

Whether the text should be crossed out (not widely supported).

Implementations§

Source§

impl Style

Source

pub const EMPTY: Style

The empty style, with nothing specified. Equivalent to Style::default().

Source

pub const BOLD: Style

Bold text.

Source

pub const ITALIC: Style

Italicized text.

Source

pub const UNDERLINE: Style

Underlined text.

Blinking text (not widely supported).

Source

pub const INVERT: Style

Inverted colors.

Source

pub const STRIKETHROUGH: Style

Crossed-out text (not widely supported).

Source

pub fn fg(color: impl Into<Color>) -> Style

Creates a style with only the foreground specified.

Examples found in repository?
examples/tic_tac_toe.rs (line 136)
134fn render_player(player: Option<Player>) -> impl Element<'static> {
135    match player {
136        None => "-".styled(Style::fg(245)),
137        Some(Player::X) => "X".styled(Style::BOLD + Style::fg(33)),
138        Some(Player::O) => "O".styled(Style::BOLD + Style::fg(203)),
139    }
140}
Source

pub fn bg(color: impl Into<Color>) -> Style

Creates a style with only the background specified.

Examples found in repository?
examples/input.rs (line 27)
13fn main() -> std::io::Result<()> {
14    let stdout = std::io::stdout().into_raw_mode()?;
15    let mut r = Renderer::new(stdout);
16
17    let mut name = String::new();
18
19    let mut events = std::io::stdin().events();
20    loop {
21        r.reset()?
22            .render((
23                "Enter your name: ".into_element(),
24                (name.into_element(), Cursor, Gap(1))
25                    .fixed_width(20)
26                    .truncated(Direction::Left)
27                    .styled(Style::bg(240)),
28            ))?
29            .finish()?;
30
31        let Some(event) = events.next().transpose()? else {
32            break;
33        };
34        match event {
35            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
36            Event::Key(Key::Char('\n' | '\r')) => break,
37            Event::Key(Key::Backspace) => {
38                name.pop();
39            }
40            _ => {}
41        }
42    }
43
44    r.clear()?;
45    drop(r);
46    println!("Your name is {name:?}");
47    Ok(())
48}
Source

pub fn with(self, other: Style) -> Style

Merges two styles, with other taking precedence.

Source

pub fn or(self, other: Style) -> Style

Merges two styles, with self taking precedence.

Trait Implementations§

Source§

impl Add for Style

Source§

type Output = Style

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for Style

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Style

Source§

fn clone(&self) -> Style

Returns a duplicate of the value. Read more
1.0.0 · 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 Default for Style

Source§

fn default() -> Self

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

impl Display for Style

Source§

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

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

impl PartialEq for Style

Source§

fn eq(&self, other: &Style) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Style

Source§

impl Eq for Style

Source§

impl StructuralPartialEq for Style

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin 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.