-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96dba07
commit 1d5aa9b
Showing
4 changed files
with
186 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
extern crate termcolor; | ||
use std::fmt::Display; | ||
use termcolor::Color::*; | ||
use termcolor::{Color, ColorSpec, StandardStream, WriteColor}; | ||
|
||
pub struct ColorString { | ||
input: String, | ||
fg: Option<Color>, | ||
bg: Option<Color>, | ||
bold: bool, | ||
italic: bool, | ||
underline: bool, | ||
} | ||
|
||
impl Default for ColorString { | ||
fn default() -> Self { | ||
Self { | ||
input: "".to_string(), | ||
fg: None, | ||
bg: None, | ||
bold: false, | ||
italic: false, | ||
underline: false, | ||
} | ||
} | ||
} | ||
|
||
pub trait ColorTrait | ||
where | ||
Self: Sized, | ||
{ | ||
fn fg(self, color: Color) -> ColorString; | ||
fn bg(self, color: Color) -> ColorString; | ||
fn bold(self) -> ColorString; | ||
fn italic(self) -> ColorString; | ||
fn underline(self) -> ColorString; | ||
fn green(self) -> ColorString { | ||
self.fg(Green) | ||
} | ||
fn red(self) -> ColorString { | ||
self.fg(Red) | ||
} | ||
fn cyan(self) -> ColorString { | ||
self.fg(Cyan) | ||
} | ||
fn magenta(self) -> ColorString { | ||
self.fg(Magenta) | ||
} | ||
fn white(self) -> ColorString { | ||
self.fg(White) | ||
} | ||
fn black(self) -> ColorString { | ||
self.fg(Black) | ||
} | ||
fn blue(self) -> ColorString { | ||
self.fg(Blue) | ||
} | ||
fn bg_green(self) -> ColorString { | ||
self.bg(Green) | ||
} | ||
fn bg_red(self) -> ColorString { | ||
self.bg(Red) | ||
} | ||
fn bg_cyan(self) -> ColorString { | ||
self.bg(Cyan) | ||
} | ||
fn bg_magenta(self) -> ColorString { | ||
self.bg(Magenta) | ||
} | ||
fn bg_white(self) -> ColorString { | ||
self.bg(White) | ||
} | ||
fn bg_black(self) -> ColorString { | ||
self.bg(Black) | ||
} | ||
fn bg_blue(self) -> ColorString { | ||
self.bg(Blue) | ||
} | ||
} | ||
|
||
impl ColorTrait for &str { | ||
fn fg(self, color: Color) -> ColorString { | ||
let mut to = ColorString::default(); | ||
to.input = self.to_string(); | ||
to.fg = Some(color); | ||
to | ||
} | ||
|
||
fn bg(self, color: Color) -> ColorString { | ||
let mut to = ColorString::default(); | ||
to.input = self.to_string(); | ||
to.bg = Some(color); | ||
to | ||
} | ||
|
||
fn bold(self) -> ColorString { | ||
let mut to = ColorString::default(); | ||
to.input = self.to_string(); | ||
to.bold = true; | ||
to | ||
} | ||
|
||
fn italic(self) -> ColorString { | ||
let mut to = ColorString::default(); | ||
to.input = self.to_string(); | ||
to.italic = true; | ||
to | ||
} | ||
|
||
fn underline(self) -> ColorString { | ||
let mut to = ColorString::default(); | ||
to.input = self.to_string(); | ||
to.underline = true; | ||
to | ||
} | ||
} | ||
|
||
impl ColorTrait for ColorString { | ||
fn fg(mut self, color: Color) -> ColorString { | ||
self.fg = Some(color); | ||
self | ||
} | ||
|
||
fn bg(mut self, color: Color) -> ColorString { | ||
self.bg = Some(color); | ||
self | ||
} | ||
|
||
fn bold(mut self) -> ColorString { | ||
self.bold = true; | ||
self | ||
} | ||
|
||
fn italic(mut self) -> ColorString { | ||
self.italic = true; | ||
self | ||
} | ||
|
||
fn underline(mut self) -> ColorString { | ||
self.underline = true; | ||
self | ||
} | ||
} | ||
|
||
impl Display for ColorString { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut stdout = StandardStream::stdout(termcolor::ColorChoice::Auto); | ||
stdout | ||
.set_color( | ||
ColorSpec::new() | ||
.set_fg(self.fg) | ||
.set_bg(self.bg) | ||
.set_bold(self.bold) | ||
.set_italic(self.italic) | ||
.set_underline(self.underline), | ||
) | ||
.unwrap_or_default(); | ||
let result = write!(f, "{}", self.input); | ||
stdout.reset().unwrap_or_default(); | ||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1d5aa9b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里替换掉 colored 的原因是:使用 cargo run 运行时正常输出颜色,但是编译后运行居然原样输出颜色控制符,原因是需要设置 cmd.exe 虚拟终端,其实调用 colored::control::set_virtual_terminal 即可。