Skip to content

Commit

Permalink
chore: upgrade yansi crate to version 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Mar 24, 2024
1 parent 8079b78 commit b2c34cc
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 65 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
48 changes: 23 additions & 25 deletions cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
));
Expand All @@ -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
)));

Expand Down Expand Up @@ -163,28 +164,25 @@ impl Component for CheckState {
) -> anyhow::Result<superconsole::Lines> {
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())?,
])
}
};
Expand Down
10 changes: 5 additions & 5 deletions cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down Expand Up @@ -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(),
)));
Expand Down Expand Up @@ -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()
)
};
Expand Down
6 changes: 3 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down Expand Up @@ -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);
}
Expand Down
44 changes: 22 additions & 22 deletions proto-yaml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -111,10 +110,10 @@ impl<W: Write> Serializer<W> {
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()
Expand Down Expand Up @@ -148,22 +147,22 @@ impl<W: Write> Serializer<W> {
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::<Utc>::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(&timestamp)?;
} else {
value.to_string()
write!(self.output, "{}", value.to_string())?;
};
write!(self.output, "{}", field_value)

Ok(())
}

fn quote_bytes(&mut self, bytes: &[u8]) -> String {
Expand Down Expand Up @@ -205,16 +204,17 @@ impl<W: Write> Serializer<W> {
result
}

fn write_as_a_comment(&mut self, value: String) -> Paint<String> {
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> {
Expand Down Expand Up @@ -249,7 +249,7 @@ impl<W: Write> Serializer<W> {
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)?;
Expand Down Expand Up @@ -325,11 +325,11 @@ impl<W: Write> Serializer<W> {
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())?,
Expand Down
3 changes: 1 addition & 2 deletions proto-yaml/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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(".");
Expand Down

0 comments on commit b2c34cc

Please sign in to comment.