Skip to content

Commit

Permalink
colored -> termcolor
Browse files Browse the repository at this point in the history
  • Loading branch information
develon2015 committed Jul 25, 2021
1 parent 96dba07 commit 1d5aa9b
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 6 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
tokio = { version = "1.5.0", features = ["full"] } # An event-driven, non-blocking I/O platform for writing asynchronous I/O backed applications.
colored = "2.0.0" # The most simple way to add colors in your terminal
termcolor = "1.1.2" # A simple cross platform library for writing colored text to a terminal.
162 changes: 162 additions & 0 deletions src/color.rs
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
}
}
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
use tokio::net::UdpSocket;

use colored::*;
mod color;
use color::ColorTrait;

const DEBUG: bool = false;

Expand All @@ -18,10 +19,7 @@ struct DNS<'a> {
}
impl<'a> DNS<'a> {
pub fn with(value: &'a [u8], offset: usize) -> Self {
return Self {
value: value,
offset: offset,
};
return Self { value, offset };
}
pub fn to_string(&self) -> String {
let mut str = String::with_capacity(1024);
Expand Down Expand Up @@ -234,7 +232,7 @@ impl<'a> DNS<'a> {
);
}
// println!("{:02x?}", self.value); // 以十六进制而非十进制打印数组
println!("{}", self.to_string().white().reversed());
println!("{}", self.to_string().bg_white().black());
println!(
"-----------------------------------------------------------------------------------"
);
Expand Down

1 comment on commit 1d5aa9b

@develon2015
Copy link
Member Author

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 即可。

Please sign in to comment.