Skip to content

Commit 1d5aa9b

Browse files
committed
colored -> termcolor
1 parent 96dba07 commit 1d5aa9b

File tree

4 files changed

+186
-6
lines changed

4 files changed

+186
-6
lines changed

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ edition = "2018"
99
[dependencies]
1010
tokio = { version = "1.5.0", features = ["full"] } # An event-driven, non-blocking I/O platform for writing asynchronous I/O backed applications.
1111
colored = "2.0.0" # The most simple way to add colors in your terminal
12+
termcolor = "1.1.2" # A simple cross platform library for writing colored text to a terminal.

src/color.rs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
extern crate termcolor;
2+
use std::fmt::Display;
3+
use termcolor::Color::*;
4+
use termcolor::{Color, ColorSpec, StandardStream, WriteColor};
5+
6+
pub struct ColorString {
7+
input: String,
8+
fg: Option<Color>,
9+
bg: Option<Color>,
10+
bold: bool,
11+
italic: bool,
12+
underline: bool,
13+
}
14+
15+
impl Default for ColorString {
16+
fn default() -> Self {
17+
Self {
18+
input: "".to_string(),
19+
fg: None,
20+
bg: None,
21+
bold: false,
22+
italic: false,
23+
underline: false,
24+
}
25+
}
26+
}
27+
28+
pub trait ColorTrait
29+
where
30+
Self: Sized,
31+
{
32+
fn fg(self, color: Color) -> ColorString;
33+
fn bg(self, color: Color) -> ColorString;
34+
fn bold(self) -> ColorString;
35+
fn italic(self) -> ColorString;
36+
fn underline(self) -> ColorString;
37+
fn green(self) -> ColorString {
38+
self.fg(Green)
39+
}
40+
fn red(self) -> ColorString {
41+
self.fg(Red)
42+
}
43+
fn cyan(self) -> ColorString {
44+
self.fg(Cyan)
45+
}
46+
fn magenta(self) -> ColorString {
47+
self.fg(Magenta)
48+
}
49+
fn white(self) -> ColorString {
50+
self.fg(White)
51+
}
52+
fn black(self) -> ColorString {
53+
self.fg(Black)
54+
}
55+
fn blue(self) -> ColorString {
56+
self.fg(Blue)
57+
}
58+
fn bg_green(self) -> ColorString {
59+
self.bg(Green)
60+
}
61+
fn bg_red(self) -> ColorString {
62+
self.bg(Red)
63+
}
64+
fn bg_cyan(self) -> ColorString {
65+
self.bg(Cyan)
66+
}
67+
fn bg_magenta(self) -> ColorString {
68+
self.bg(Magenta)
69+
}
70+
fn bg_white(self) -> ColorString {
71+
self.bg(White)
72+
}
73+
fn bg_black(self) -> ColorString {
74+
self.bg(Black)
75+
}
76+
fn bg_blue(self) -> ColorString {
77+
self.bg(Blue)
78+
}
79+
}
80+
81+
impl ColorTrait for &str {
82+
fn fg(self, color: Color) -> ColorString {
83+
let mut to = ColorString::default();
84+
to.input = self.to_string();
85+
to.fg = Some(color);
86+
to
87+
}
88+
89+
fn bg(self, color: Color) -> ColorString {
90+
let mut to = ColorString::default();
91+
to.input = self.to_string();
92+
to.bg = Some(color);
93+
to
94+
}
95+
96+
fn bold(self) -> ColorString {
97+
let mut to = ColorString::default();
98+
to.input = self.to_string();
99+
to.bold = true;
100+
to
101+
}
102+
103+
fn italic(self) -> ColorString {
104+
let mut to = ColorString::default();
105+
to.input = self.to_string();
106+
to.italic = true;
107+
to
108+
}
109+
110+
fn underline(self) -> ColorString {
111+
let mut to = ColorString::default();
112+
to.input = self.to_string();
113+
to.underline = true;
114+
to
115+
}
116+
}
117+
118+
impl ColorTrait for ColorString {
119+
fn fg(mut self, color: Color) -> ColorString {
120+
self.fg = Some(color);
121+
self
122+
}
123+
124+
fn bg(mut self, color: Color) -> ColorString {
125+
self.bg = Some(color);
126+
self
127+
}
128+
129+
fn bold(mut self) -> ColorString {
130+
self.bold = true;
131+
self
132+
}
133+
134+
fn italic(mut self) -> ColorString {
135+
self.italic = true;
136+
self
137+
}
138+
139+
fn underline(mut self) -> ColorString {
140+
self.underline = true;
141+
self
142+
}
143+
}
144+
145+
impl Display for ColorString {
146+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
147+
let mut stdout = StandardStream::stdout(termcolor::ColorChoice::Auto);
148+
stdout
149+
.set_color(
150+
ColorSpec::new()
151+
.set_fg(self.fg)
152+
.set_bg(self.bg)
153+
.set_bold(self.bold)
154+
.set_italic(self.italic)
155+
.set_underline(self.underline),
156+
)
157+
.unwrap_or_default();
158+
let result = write!(f, "{}", self.input);
159+
stdout.reset().unwrap_or_default();
160+
result
161+
}
162+
}

src/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use tokio::io::AsyncReadExt;
44
use tokio::io::AsyncWriteExt;
55
use tokio::net::UdpSocket;
66

7-
use colored::*;
7+
mod color;
8+
use color::ColorTrait;
89

910
const DEBUG: bool = false;
1011

@@ -18,10 +19,7 @@ struct DNS<'a> {
1819
}
1920
impl<'a> DNS<'a> {
2021
pub fn with(value: &'a [u8], offset: usize) -> Self {
21-
return Self {
22-
value: value,
23-
offset: offset,
24-
};
22+
return Self { value, offset };
2523
}
2624
pub fn to_string(&self) -> String {
2725
let mut str = String::with_capacity(1024);
@@ -234,7 +232,7 @@ impl<'a> DNS<'a> {
234232
);
235233
}
236234
// println!("{:02x?}", self.value); // 以十六进制而非十进制打印数组
237-
println!("{}", self.to_string().white().reversed());
235+
println!("{}", self.to_string().bg_white().black());
238236
println!(
239237
"-----------------------------------------------------------------------------------"
240238
);

0 commit comments

Comments
 (0)