#[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 flippedFields (Non-exhaustive)§
This struct is marked as non-exhaustive
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
impl Style
Sourcepub const EMPTY: Style
pub const EMPTY: Style
The empty style, with nothing specified. Equivalent to Style::default().
Sourcepub const STRIKETHROUGH: Style
pub const STRIKETHROUGH: Style
Crossed-out text (not widely supported).
Sourcepub fn fg(color: impl Into<Color>) -> Style
pub fn fg(color: impl Into<Color>) -> Style
Creates a style with only the foreground specified.
Sourcepub fn bg(color: impl Into<Color>) -> Style
pub fn bg(color: impl Into<Color>) -> Style
Creates a style with only the background specified.
Examples found in repository?
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}Trait Implementations§
Source§impl AddAssign for Style
impl AddAssign for Style
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read more