Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Refactors #2

Merged
merged 21 commits into from
Sep 30, 2019
Prev Previous commit
Next Next commit
reset color/set attr uses command api instead of stdout
  • Loading branch information
Timon Post committed Sep 29, 2019
commit 03e198ef60d11497368cb200139794772e05e001
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,28 @@ where

#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
// attributes are not supported by WinAPI.
Ok(())
}
}

/// When executed, this command will reset the console colors back to default
///
/// See `crossterm/examples/command.rs` for more information on how to execute commands.
pub struct ResetColor;

impl Command for ResetColor {
type AnsiType = String;

fn ansi_code(&self) -> Self::AnsiType {
ansi::RESET_CSI_SEQUENCE.to_string()
}

#[cfg(windows)]
fn execute_winapi(&self) -> Result<()> {
WinApiColor::new().reset()
}
}

impl_display!(for SetFg);
impl_display!(for SetBg);
impl_display!(for SetAttr);
Expand Down
9 changes: 6 additions & 3 deletions src/styledobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
use std::fmt::{self, Display, Formatter};
use std::result;

use crossterm_utils::{csi, queue};
use crossterm_utils::queue;

use super::{color, Attribute, Color, Colorize, ObjectStyle, SetBg, SetFg, Styler};
use crate::{
color, Attribute, Color, Colorize, ObjectStyle, ResetColor, SetAttr, SetBg, SetFg, Styler,
};

/// Contains both the style and the content which can be styled.
#[derive(Clone)]
TimonPost marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -64,13 +66,14 @@ impl<D: Display + Clone> Display for StyledObject<D> {
}

for attr in self.object_style.attrs.iter() {
fmt::Display::fmt(&format!(csi!("{}m"), *attr as i16), f)?;
queue!(f, SetAttr(*attr)).map_err(|_| fmt::Error)?;
reset = true;
}

fmt::Display::fmt(&self.content, f)?;

if reset {
queue!(f, ResetColor).map_err(|_| fmt::Error)?;
colored_terminal.reset().map_err(|_| fmt::Error)?;
}

Expand Down