From b2c34ccbb878564b3d7beb8c09418b84e2f1fc2d Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Sun, 24 Mar 2024 12:00:45 +0100 Subject: [PATCH] chore: upgrade `yansi` crate to version 1.0.1 --- Cargo.lock | 14 +++++------ Cargo.toml | 2 +- cli/src/commands/check.rs | 48 ++++++++++++++++++------------------- cli/src/commands/scan.rs | 10 ++++---- cli/src/main.rs | 6 ++--- proto-yaml/src/lib.rs | 44 +++++++++++++++++----------------- proto-yaml/src/tests/mod.rs | 3 +-- 7 files changed, 62 insertions(+), 65 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c79663440..fe5834cf5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1349,7 +1349,7 @@ dependencies = [ "scopeguard", "similar-asserts", "tempfile", - "yansi 1.0.0-rc.1", + "yansi 1.0.1", ] [[package]] @@ -4161,9 +4161,9 @@ checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" [[package]] name = "yansi" -version = "1.0.0-rc.1" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1367295b8f788d371ce2dbc842c7b709c73ee1364d30351dd300ec2203b12377" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" [[package]] name = "yara-x" @@ -4216,7 +4216,7 @@ dependencies = [ "uuid", "walrus", "wasmtime", - "yansi 0.5.1", + "yansi 1.0.1", "yara-x-macros", "yara-x-parser", "yara-x-proto", @@ -4253,7 +4253,7 @@ dependencies = [ "strum_macros", "superconsole", "wild", - "yansi 0.5.1", + "yansi 1.0.1", "yara-x", "yara-x-fmt", "yara-x-parser", @@ -4301,7 +4301,7 @@ dependencies = [ "pretty_assertions", "thiserror", "yaml-rust", - "yansi 0.5.1", + "yansi 1.0.1", "yara-x-macros", ] @@ -4326,7 +4326,7 @@ dependencies = [ "protobuf-codegen", "protobuf-parse", "protobuf-support", - "yansi 0.5.1", + "yansi 1.0.1", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 6ccff9076..e62385707 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,7 +88,7 @@ uuid = "1.4.1" walrus = "0.20.2" wasmtime = "18.0.2" yaml-rust = "0.4.5" -yansi = "0.5.1" +yansi = "1.0.1" yara-x = { path = "lib" } yara-x-fmt = { path = "fmt" } yara-x-macros = { path = "macros" } diff --git a/cli/src/commands/check.rs b/cli/src/commands/check.rs index 44b2f94de..dd910a620 100644 --- a/cli/src/commands/check.rs +++ b/cli/src/commands/check.rs @@ -7,6 +7,7 @@ use clap::{arg, value_parser, ArgAction, ArgMatches, Command}; use crossterm::tty::IsTty; use superconsole::{Component, Line, Lines, Span}; use yansi::Color::{Green, Red, Yellow}; +use yansi::Paint; use yara_x_parser::{Parser, SourceCode}; use crate::walk::Message; @@ -92,7 +93,7 @@ pub fn exec_check(args: &ArgMatches) -> anyhow::Result<()> { state.files_passed.fetch_add(1, Ordering::Relaxed); lines.push(format!( "[ {} ] {}", - Green.paint("PASS").bold(), + "PASS".paint(Green).bold(), file_path.display() )); } else { @@ -101,7 +102,7 @@ pub fn exec_check(args: &ArgMatches) -> anyhow::Result<()> { .fetch_add(ast.warnings.len(), Ordering::Relaxed); lines.push(format!( "[ {} ] {}", - Yellow.paint("WARN").bold(), + "WARN".paint(Yellow).bold(), file_path.display() )); for warning in ast.warnings { @@ -113,7 +114,7 @@ pub fn exec_check(args: &ArgMatches) -> anyhow::Result<()> { state.errors.fetch_add(1, Ordering::Relaxed); lines.push(format!( "[ {} ] {}\n{}", - Red.paint("FAIL").bold(), + "FAIL".paint(Red).bold(), file_path.display(), err, )); @@ -127,7 +128,7 @@ pub fn exec_check(args: &ArgMatches) -> anyhow::Result<()> { |err, output| { let _ = output.send(Message::Error(format!( "{} {}", - Red.paint("error:").bold(), + "error:".paint(Red).bold(), err ))); @@ -163,28 +164,25 @@ impl Component for CheckState { ) -> anyhow::Result { let res = match mode { superconsole::DrawMode::Normal | superconsole::DrawMode::Final => { - let ok = Green - .paint(format!( - "{} file(s) ok. ", - self.files_passed.load(Ordering::Relaxed) - )) - .bold(); - let warnings = Yellow - .paint(format!( - "warnings: {}. ", - self.warnings.load(Ordering::Relaxed) - )) - .bold(); - let errors = Red - .paint(format!( - "errors: {}.", - self.errors.load(Ordering::Relaxed) - )) - .bold(); + let ok = format!( + "{} file(s) ok. ", + self.files_passed.load(Ordering::Relaxed) + ); + + let warnings = format!( + "warnings: {}. ", + self.warnings.load(Ordering::Relaxed) + ); + + let errors = format!( + "errors: {}.", + self.errors.load(Ordering::Relaxed) + ); + Line::from_iter([ - Span::new_unstyled(ok)?, - Span::new_unstyled(warnings)?, - Span::new_unstyled(errors)?, + Span::new_unstyled(ok.paint(Red).bold())?, + Span::new_unstyled(warnings.paint(Yellow).bold())?, + Span::new_unstyled(errors.paint(Red).bold())?, ]) } }; diff --git a/cli/src/commands/scan.rs b/cli/src/commands/scan.rs index 94bb478a1..dfb234a88 100644 --- a/cli/src/commands/scan.rs +++ b/cli/src/commands/scan.rs @@ -174,7 +174,7 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> { let output = output.clone(); scanner.console_log(move |msg| { output - .send(Message::Error(format!("{}", Yellow.paint(msg)))) + .send(Message::Error(format!("{}", msg.paint(Yellow)))) .unwrap(); }); } @@ -251,7 +251,7 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> { |err, output| { let _ = output.send(Message::Error(format!( "{} {}: {}", - Red.paint("error:").bold(), + "error: ".paint(Red).bold(), err, err.root_cause(), ))); @@ -289,14 +289,14 @@ fn print_matching_rules( let line = if print_namespace { format!( "{}:{} {}", - Cyan.paint(matching_rule.namespace()).bold(), - Cyan.paint(matching_rule.identifier()).bold(), + matching_rule.namespace().paint(Cyan).bold(), + matching_rule.identifier().paint(Cyan).bold(), file_path.display(), ) } else { format!( "{} {}", - Cyan.paint(matching_rule.identifier()).bold(), + matching_rule.identifier().paint(Cyan).bold(), file_path.display() ) }; diff --git a/cli/src/main.rs b/cli/src/main.rs index 0b6d4a8c6..05f353caf 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -30,7 +30,7 @@ fn main() -> anyhow::Result<()> { // file) turn off colors. This way you can redirect the output to a file // without ANSI escape codes messing up the file content. if !io::stdout().is_tty() { - Paint::disable(); + yansi::disable(); } let args = command!() @@ -94,12 +94,12 @@ fn main() -> anyhow::Result<()> { if let Some(source) = err.source() { eprintln!( "{} {}: {}", - Red.paint("error:").bold(), + "error: ".paint(Red).bold(), err, source ); } else { - eprintln!("{} {}", Red.paint("error:").bold(), err); + eprintln!("{} {}", "error: ".paint(Red).bold(), err); } std::process::exit(1); } diff --git a/proto-yaml/src/lib.rs b/proto-yaml/src/lib.rs index 10154d3fd..c1d8cb617 100644 --- a/proto-yaml/src/lib.rs +++ b/proto-yaml/src/lib.rs @@ -54,8 +54,7 @@ use itertools::Itertools; use protobuf::MessageDyn; use std::cmp::Ordering; use std::io::{Error, Write}; -use yansi::Paint; -use yansi::{Color, Style}; +use yansi::{Color, Paint, Style}; use protobuf::descriptor::FieldDescriptorProto; use protobuf::reflect::ReflectFieldRef::{Map, Optional, Repeated}; @@ -111,10 +110,10 @@ impl Serializer { pub fn with_colors(&mut self, b: bool) -> &mut Self { self.colors = if b { Colors { - string: Color::Green.style(), - field_name: Color::Yellow.style(), - repeated_name: Color::Yellow.style(), - comment: Color::RGB(222, 184, 135).style(), + string: Color::Green.foreground(), + field_name: Color::Yellow.foreground(), + repeated_name: Color::Yellow.foreground(), + comment: Color::Rgb(222, 184, 135).foreground(), } } else { Colors::default() @@ -148,22 +147,22 @@ impl Serializer { value: T, value_options: &ValueOptions, ) -> Result<(), std::io::Error> { - let field_value = if value_options.is_hex { - format!("0x{:x}", value.into()) + if value_options.is_hex { + write!(self.output, "0x{:x}", value.into())?; } else if value_options.is_timestamp { let timestamp = DateTime::::from_naive_utc_and_offset( NaiveDateTime::from_timestamp_opt(value.into(), 0).unwrap(), Utc, - ); - format!( - "{} {}", - value.to_string(), - self.write_as_a_comment(timestamp.to_string()) ) + .to_string(); + + write!(self.output, "{} ", value.to_string())?; + self.write_comment(×tamp)?; } else { - value.to_string() + write!(self.output, "{}", value.to_string())?; }; - write!(self.output, "{}", field_value) + + Ok(()) } fn quote_bytes(&mut self, bytes: &[u8]) -> String { @@ -205,16 +204,17 @@ impl Serializer { result } - fn write_as_a_comment(&mut self, value: String) -> Paint { - self.colors.comment.paint(format!("{} {}", "#", value)) + fn write_comment(&mut self, comment: &str) -> Result<(), Error> { + let comment = format!("# {}", comment); + write!(self.output, "{}", comment.paint(self.colors.comment)) } fn write_field_name(&mut self, name: &str) -> Result<(), Error> { - write!(self.output, "{}:", self.colors.field_name.paint(name)) + write!(self.output, "{}:", name.paint(self.colors.field_name)) } fn write_repeated_name(&mut self, name: &str) -> Result<(), Error> { - write!(self.output, "{}:", self.colors.repeated_name.paint(name)) + write!(self.output, "{}:", name.paint(self.colors.repeated_name)) } fn write_msg(&mut self, msg: &MessageRef) -> Result<(), Error> { @@ -249,7 +249,7 @@ impl Serializer { self.output, "{}{} ", " ".repeat((INDENTATION - 2) as usize), - self.colors.repeated_name.paint("-") + "-".paint(self.colors.repeated_name) )?; self.indent += INDENTATION; self.write_value(&field, &value)?; @@ -325,11 +325,11 @@ impl Serializer { ReflectValueRef::Bool(v) => write!(self.output, "{}", v)?, ReflectValueRef::String(v) => { let quoted = self.quote_str(v); - write!(self.output, "{}", self.colors.string.paint(quoted))?; + write!(self.output, "{}", quoted.paint(self.colors.string))?; } ReflectValueRef::Bytes(v) => { let quoted = self.quote_bytes(v); - write!(self.output, "{}", self.colors.string.paint(quoted))?; + write!(self.output, "{}", quoted.paint(self.colors.string))?; } ReflectValueRef::Enum(d, v) => match d.value_by_number(*v) { Some(e) => write!(self.output, "{}", e.name())?, diff --git a/proto-yaml/src/tests/mod.rs b/proto-yaml/src/tests/mod.rs index c0345aee6..410435781 100644 --- a/proto-yaml/src/tests/mod.rs +++ b/proto-yaml/src/tests/mod.rs @@ -1,13 +1,12 @@ use protobuf::text_format::parse_from_str; use std::fs; -use yansi::Paint; use crate::Serializer; #[test] fn yaml_serializer() { // Disable colors for testing. - Paint::disable(); + yansi::disable(); // Create goldenfile mint. let mut mint = goldenfile::Mint::new(".");