minicrossterm/crossterm_style/src/objectstyle.rs
Timon 1e332daaed
Refactor and API stabilization (#115)
- Major refactor and cleanup.
- Improved performance; 
    - No locking when writing to stdout. 
    - UNIX doesn't have any dynamic dispatch anymore. 
    - Windows has improved the way to check if ANSI modes are enabled.
    - Removed lot's of complex API calls: `from_screen`, `from_output`
    - Removed `Arc<TerminalOutput>` from all internal Api's. 
- Removed termios dependency for UNIX systems.
- Upgraded deps.
- Removed about 1000 lines of code
    - `TerminalOutput` 
    - `Screen`
    - unsafe code
    - Some duplicated code introduced by a previous refactor.
- Raw modes UNIX systems improved     
- Added `NoItalic` attribute
2019-04-10 23:46:30 +02:00

62 lines
1.5 KiB
Rust

//! This module contains the `object style` that can be applied to an `styled object`.
use super::{Color, StyledObject};
use std::fmt::Display;
use super::Attribute;
/// Struct that contains the style properties that can be applied to an displayable object.
#[derive(Clone)]
pub struct ObjectStyle {
pub fg_color: Option<Color>,
pub bg_color: Option<Color>,
pub attrs: Vec<Attribute>,
}
impl Default for ObjectStyle {
fn default() -> ObjectStyle {
ObjectStyle {
fg_color: None,
bg_color: None,
attrs: Vec::new(),
}
}
}
impl ObjectStyle {
/// Apply an `StyledObject` to the passed displayable object.
pub fn apply_to<D: Display>(&self, val: D) -> StyledObject<D> {
StyledObject {
object_style: self.clone(),
content: val,
}
}
/// Get an new instance of `ObjectStyle`
pub fn new() -> ObjectStyle {
ObjectStyle {
fg_color: None,
bg_color: None,
attrs: Vec::new(),
}
}
/// Set the background color of `ObjectStyle` to the passed color.
pub fn bg(mut self, color: Color) -> ObjectStyle {
self.bg_color = Some(color);
self
}
/// Set the foreground color of `ObjectStyle` to the passed color.
pub fn fg(mut self, color: Color) -> ObjectStyle {
self.fg_color = Some(color);
self
}
/// Add an `Attribute` to the current text. Like italic or bold.
pub fn add_attr(&mut self, attr: Attribute) {
self.attrs.push(attr);
}
}