Skip to content
This repository has been archived by the owner on Aug 26, 2023. It is now read-only.

Commit

Permalink
Improve CLIReporter color output
Browse files Browse the repository at this point in the history
  • Loading branch information
lwagner94 committed Feb 4, 2022
1 parent ca9b2ea commit f8f400d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
22 changes: 12 additions & 10 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 @@ -29,7 +29,7 @@ gimli = "0.26.1"
toml = "0.5.0"
serde = { version = "1.0", features = ["derive"] }

ansi_term = "0.12.1"
colored = "2.0.0"
log = "0.4.0"
env_logger = "0.9.0"
num_cpus = "1.13.1"
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ansi_term::Color;
use anyhow::Result;
use clap::{AppSettings, Parser, Subcommand};
use colored::*;
use env_logger::Builder;
use log::*;
use std::path::Path;
Expand Down Expand Up @@ -43,9 +43,9 @@ fn list_functions(config: &Config) -> Result<()> {

for function in module.functions() {
let check_result_str = if policy.check_function(&function) {
Color::Green.paint("allowed: ")
"allowed: ".green()
} else {
Color::Red.paint("denied: ")
"denied: ".red()
};
println!("{check_result_str}{function}");
}
Expand All @@ -59,9 +59,9 @@ fn list_files(config: &Config) -> Result<()> {

for file in module.source_files() {
let check_result_str = if policy.check_file(&file) {
Color::Green.paint("allowed: ")
"allowed: ".green()
} else {
Color::Red.paint("denied: ")
"denied: ".red()
};
println!("{check_result_str}{file}");
}
Expand Down
51 changes: 25 additions & 26 deletions src/reporter/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansi_term::Color;
use colored::*;
use std::{cell::RefCell, io::Write};

use super::{
Expand All @@ -16,16 +16,14 @@ pub struct CLIReporter<'a> {
highlighter_context: SyntectContext,
}

impl std::fmt::Display for MutationOutcome {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
MutationOutcome::Alive => Color::Red.paint("ALIVE"),
MutationOutcome::Killed => Color::Green.paint("KILLED"),
MutationOutcome::Timeout => Color::Yellow.paint("TIMEOUT"),
MutationOutcome::Error => Color::Yellow.paint("ERROR"),
};

write!(f, "{s}")
impl From<MutationOutcome> for ColoredString {
fn from(m: MutationOutcome) -> Self {
match m {
MutationOutcome::Alive => "ALIVE".red(),
MutationOutcome::Killed => "KILLED".green(),
MutationOutcome::Timeout => "TIMEOUT".yellow(),
MutationOutcome::Error => "ERROR".yellow(),
}
}
}

Expand Down Expand Up @@ -56,23 +54,20 @@ impl<'a> CLIReporter<'a> {
);
let mut writer = self.writer.borrow_mut();

let alive_str = format!("{}:", MutationOutcome::Alive);
let timeout_str = format!("{}:", MutationOutcome::Timeout);
let error_str = format!("{}:", MutationOutcome::Error);
let killed_str = format!("{}:", MutationOutcome::Killed);

writeln!(writer).unwrap();
writeln!(writer, "{:>30} {}", alive_str, alive).unwrap();
writeln!(writer, "{:>30} {}", timeout_str, timeout).unwrap();
writeln!(writer, "{:>30} {}", error_str, error).unwrap();
writeln!(writer, "{:>30} {}", killed_str, killed).unwrap();
let alive_str: ColoredString = MutationOutcome::Alive.into();
let timeout_str: ColoredString = MutationOutcome::Timeout.into();
let error_str: ColoredString = MutationOutcome::Error.into();
let killed_str: ColoredString = MutationOutcome::Killed.into();

let mutation_score =
100f32 * (timeout + killed + error) as f32 / (alive + timeout + killed + error) as f32;

let f = format!("{}:", Color::White.paint("Mutation score"));

writeln!(writer, "{:>30} {}%", f, mutation_score).unwrap();
writeln!(writer).unwrap();
writeln!(writer, "{0:15} {1}", alive_str, alive).unwrap();
writeln!(writer, "{0:15} {1}", timeout_str, timeout).unwrap();
writeln!(writer, "{0:15} {1}", error_str, error).unwrap();
writeln!(writer, "{0:15} {1}", killed_str, killed).unwrap();
writeln!(writer, "{0:15} {1}%", "Mutation score", mutation_score).unwrap();
}

fn enumerate_mutants(&self, executed_mutants: &[ExecutedMutant]) -> Result<()> {
Expand Down Expand Up @@ -117,7 +112,11 @@ impl<'a> CLIReporter<'a> {

match Self::get_line_from_file(&file, line_nr) {
Ok(line) => {
line_in_file = highlighter.terminal_string(&line);
line_in_file = if control::ShouldColorize::from_env().should_colorize() {
highlighter.terminal_string(&line)
} else {
line
};
}
Err(e) => {
log::warn!("Could not read from file: {:?}", e);
Expand All @@ -133,7 +132,7 @@ impl<'a> CLIReporter<'a> {
}

let description = mutant.operator.description();
let outcome = &mutant.outcome;
let outcome: ColoredString = mutant.outcome.clone().into();

// let status = color.paint(format!("{:?}", mutant.outcome));

Expand Down
2 changes: 1 addition & 1 deletion src/reporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use syntect::{
parsing::{SyntaxReference, SyntaxSet},
};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum MutationOutcome {
Alive,
Killed,
Expand Down

0 comments on commit f8f400d

Please sign in to comment.