diff --git a/Cargo.lock b/Cargo.lock index 11e9ea4..083f1ae 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,7 @@ name = "dnsd" version = "0.1.0" dependencies = [ "colored", + "termcolor", "tokio", ] @@ -245,6 +246,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + [[package]] name = "tokio" version = "1.5.0" @@ -298,6 +308,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 4d1b953..81221ac 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -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. diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..fa78cd8 --- /dev/null +++ b/src/color.rs @@ -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, + bg: Option, + 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 + } +} diff --git a/src/main.rs b/src/main.rs index 47e9a3e..fa0a773 100755 --- a/src/main.rs +++ b/src/main.rs @@ -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; @@ -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); @@ -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!( "-----------------------------------------------------------------------------------" );