diff --git a/Cargo.lock b/Cargo.lock index 082eff0..b950c0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1313,6 +1313,6 @@ checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" [[package]] name = "yansi" -version = "0.5.1" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" diff --git a/Cargo.toml b/Cargo.toml index d59723c..1e38a12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ depends = "" [dependencies] -yansi = "^0" +yansi = "^1" atty = "^0" derive_builder = "^0" regex = "^1.9" diff --git a/README.md b/README.md index 53a1790..c208c29 100644 --- a/README.md +++ b/README.md @@ -206,8 +206,8 @@ print!("{}", histogram); You can disable coloring by doing: ```rust -// use yansi::Paint; -Paint::disable(); +// use use yansi; +yansi::disable(); ``` You can find the documentation [here](https://docs.rs/lowcharts/latest/lowcharts/). diff --git a/src/format/mod.rs b/src/format/mod.rs index 085b10f..872632f 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -1,7 +1,6 @@ use std::fmt; use std::ops::Range; -use yansi::Color::{Blue, Green, Red}; use yansi::Paint; // Units-based suffixes for human formatting. @@ -80,12 +79,12 @@ impl HorizontalScale { } } - pub fn get_bar(&self, units: usize) -> Paint { - Red.paint(format!("{:∎ yansi::Painted { + yansi::Paint::new(format!("{:∎ Paint { - Green.paint(format!("{units:width$}")) + pub fn get_count(&self, units: usize, width: usize) -> yansi::Painted { + yansi::Paint::new(format!("{units:width$}")).green() } pub fn get_scale(&self) -> usize { @@ -98,8 +97,8 @@ impl fmt::Display for HorizontalScale { writeln!( formatter, "Each {} represents a count of {}", - Red.paint(BAR_CHAR), - Blue.paint(self.scale.to_string()), + BAR_CHAR.red(), + self.scale.to_string().blue(), ) } } @@ -107,6 +106,7 @@ impl fmt::Display for HorizontalScale { #[cfg(test)] mod tests { use super::*; + use yansi; use yansi::Paint; #[test] @@ -200,7 +200,7 @@ mod tests { #[test] fn test_horizontal_scale() { - Paint::disable(); + yansi::disable(); assert_eq!( format!("{}", HorizontalScale::new(123)), format!("Each {BAR_CHAR} represents a count of 123\n") @@ -209,7 +209,7 @@ mod tests { #[test] fn test_horizontal_scale_with_zero_scale() { - Paint::disable(); + yansi::disable(); let scale = HorizontalScale::new(0); assert_eq!(scale.get_scale(), 1); assert_eq!( @@ -221,15 +221,20 @@ mod tests { #[test] fn test_horizontal_scale_bar() { let scale = HorizontalScale::new(10); + yansi::disable(); assert_eq!( - scale.get_bar(80), - Red.paint(format!("{:∎(vec: &[T], min: usize) -> bool { @@ -30,14 +29,14 @@ fn configure_output(option: &str, verbose: bool) { let mut color_choice = ColorChoice::Auto; match option { "no" => { - Paint::disable(); + yansi::disable(); color_choice = ColorChoice::Never; } "auto" => match env::var("TERM") { - Ok(value) if value == "dumb" => Paint::disable(), + Ok(value) if value == "dumb" => yansi::disable(), _ => { if atty::isnt(atty::Stream::Stdout) { - Paint::disable(); + yansi::disable(); } } }, @@ -292,6 +291,7 @@ fn main() { mod tests { use super::*; use yansi::Color::Blue; + use yansi::Paint; // `yansi::Paint::{enable,disable}` mutates global state; if we run // `configure_output` (which calls `Paint::disable`) tests in parallel we @@ -301,9 +301,9 @@ mod tests { #[test] #[serial] fn test_output_yes() { - Paint::enable(); + yansi::enable(); configure_output("yes", true); - let display = format!("{}", Blue.paint("blue")); + let display = format!("{}", "blue".paint(Blue)); assert_eq!("\u{1b}[34mblue\u{1b}[0m", display); assert_eq!(LevelFilter::Debug, log::max_level()); } @@ -311,9 +311,9 @@ mod tests { #[test] #[serial] fn test_output_no() { - Paint::enable(); + yansi::enable(); configure_output("no", false); - let display = format!("{}", Blue.paint("blue")); + let display = format!("{}", "blue".paint(Blue)); assert_eq!("blue", display); assert_eq!(LevelFilter::Info, log::max_level()); } @@ -321,10 +321,10 @@ mod tests { #[test] #[serial] fn test_output_auto() { - Paint::enable(); + yansi::enable(); env::set_var("TERM", "dumb"); configure_output("auto", false); - let display = format!("{}", Blue.paint("blue")); + let display = format!("{}", "blue".paint(Blue)); assert_eq!("blue", display); } diff --git a/src/plot/histogram.rs b/src/plot/histogram.rs index ac9d15e..173ce46 100644 --- a/src/plot/histogram.rs +++ b/src/plot/histogram.rs @@ -1,7 +1,7 @@ use std::fmt; use std::ops::Range; -use yansi::Color::Blue; +use yansi::Paint; use crate::format::{F64Formatter, HorizontalScale}; use crate::stats::Stats; @@ -186,12 +186,13 @@ impl HistWriter { writeln!( f, "[{range}] [{count}] {bar}", - range = Blue.paint(format!( + range = format!( "{:>width$} .. {:>width$}", self.formatter.format(bucket.range.start), self.formatter.format(bucket.range.end), width = width, - )), + ) + .blue(), count = horizontal_scale.get_count(bucket.count, width_count), bar = horizontal_scale.get_bar(bucket.count) ) @@ -218,7 +219,7 @@ impl HistWriter { mod tests { use super::*; use float_eq::assert_float_eq; - use yansi::Paint; + use yansi; #[test] fn test_buckets() { @@ -264,7 +265,7 @@ mod tests { hist.load(&[ -1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96, ]); - Paint::disable(); + yansi::disable(); let display = format!("{hist}"); assert!(display.contains("[-2.000 .. 0.000] [3] ∎∎∎\n")); assert!(display.contains("[ 0.000 .. 2.000] [5] ∎∎∎∎∎\n")); @@ -284,7 +285,7 @@ mod tests { hist.load(&[ -1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96, ]); - Paint::disable(); + yansi::disable(); let display = format!("{hist:2}"); assert!(display.contains("[-2.000 .. 0.000] [3] ∎∎∎\n")); } @@ -308,7 +309,7 @@ mod tests { ..Default::default() }, ); - Paint::disable(); + yansi::disable(); let display = format!("{hist}"); assert!(display.contains("[-12.0 M .. -10.4 M] [4] ∎∎∎∎\n")); assert!(display.contains("[ -2.6 M .. -1.1 M] [1] ∎\n")); @@ -327,7 +328,7 @@ mod tests { ..Default::default() }, ); - Paint::disable(); + yansi::disable(); let display = format!("{hist}"); assert!(display.contains("[ 0.00 .. 1.00] [5] ∎∎∎∎∎\n")); assert!(display.contains("[ 1.00 .. 3.00] [1] ∎\n")); diff --git a/src/plot/matchbar.rs b/src/plot/matchbar.rs index c6639d3..739f1e1 100644 --- a/src/plot/matchbar.rs +++ b/src/plot/matchbar.rs @@ -1,6 +1,6 @@ use std::fmt; -use yansi::Color::Blue; +use yansi::Paint; use crate::format::HorizontalScale; @@ -61,17 +61,14 @@ impl fmt::Display for MatchBar { writeln!( f, "Matches: {}.", - Blue.paint(format!( - "{}", - self.vec.iter().map(|r| r.count).sum::() - )), + format!("{}", self.vec.iter().map(|r| r.count).sum::()).blue(), )?; writeln!(f, "{horizontal_scale}")?; for row in &self.vec { writeln!( f, "[{label}] [{count}] {bar}", - label = Blue.paint(format!("{:width$}", row.label, width = self.top_length)), + label = format!("{:width$}", row.label, width = self.top_length).blue(), count = horizontal_scale.get_count(row.count, width_count), bar = horizontal_scale.get_bar(row.count) )?; @@ -83,7 +80,7 @@ impl fmt::Display for MatchBar { #[cfg(test)] mod tests { use super::*; - use yansi::Paint; + use yansi; #[test] fn test_matchbar() { @@ -97,7 +94,7 @@ mod tests { let mb = MatchBar::new(vec![row0, row1, MatchBarRow::new("label333")]); assert_eq!(mb.top_length, 8); assert_eq!(mb.top_values, 3); - Paint::disable(); + yansi::disable(); let display = format!("{mb}"); assert!(display.contains("[label1 ] [3] ∎∎∎\n")); diff --git a/src/plot/splittimehist.rs b/src/plot/splittimehist.rs index eeafba3..8ab6fb5 100644 --- a/src/plot/splittimehist.rs +++ b/src/plot/splittimehist.rs @@ -2,6 +2,7 @@ use std::fmt; use chrono::{DateTime, Duration, FixedOffset}; use yansi::Color::{Blue, Cyan, Green, Magenta, Red}; +use yansi::Paint; use crate::format::{HorizontalScale, BAR_CHAR}; use crate::plot::date_fmt_string; @@ -111,16 +112,12 @@ impl SplitTimeHistogram { widths: &[usize], ts_fmt: &str, ) -> fmt::Result { - write!( - f, - "[{}] [", - Blue.paint(format!("{}", row.start.format(ts_fmt))) - )?; + write!(f, "[{}] [", format!("{}", row.start.format(ts_fmt)).blue())?; for i in 0..self.strings.len() { write!( f, "{}", - COLORS[i].paint(format!("{:width$}", row.count[i], width = widths[i])) + format!("{:width$}", row.count[i], width = widths[i]).paint(COLORS[i]) )?; if i < self.strings.len() - 1 { write!(f, "/")?; @@ -131,7 +128,10 @@ impl SplitTimeHistogram { write!( f, "{}", - COLORS[i].paint(BAR_CHAR.repeat(row.count[i] / divisor).to_string()) + BAR_CHAR + .repeat(row.count[i] / divisor) + .to_string() + .paint(COLORS[i]) )?; } writeln!(f) @@ -158,7 +158,7 @@ impl fmt::Display for SplitTimeHistogram { writeln!(f, "Matches: {total}.")?; for (i, s) in self.strings.iter().enumerate() { let total = self.vec.iter().map(|r| r.count[i]).sum::(); - writeln!(f, "{}: {total}.", COLORS[i].paint(s))?; + writeln!(f, "{}: {total}.", s.paint(COLORS[i]))?; } writeln!(f, "{horizontal_scale}")?; let ts_fmt = date_fmt_string(self.step.num_seconds()); @@ -172,11 +172,11 @@ impl fmt::Display for SplitTimeHistogram { #[cfg(test)] mod tests { use super::*; - use yansi::Paint; + use yansi; #[test] fn test_big_time_interval() { - Paint::disable(); + yansi::disable(); let mut vec = vec![ ( DateTime::parse_from_rfc3339("2021-04-15T04:25:00+00:00").unwrap(), diff --git a/src/plot/terms.rs b/src/plot/terms.rs index 52cc6d1..84516a5 100644 --- a/src/plot/terms.rs +++ b/src/plot/terms.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::fmt; -use yansi::Color::Blue; +use yansi::Paint; use crate::format::HorizontalScale; @@ -51,7 +51,7 @@ impl fmt::Display for CommonTerms { writeln!( f, "[{label}] [{count}] {bar}", - label = Blue.paint(format!("{term:>label_width$}")), + label = format!("{term:>label_width$}").blue(), count = horizontal_scale.get_count(**count, width_count), bar = horizontal_scale.get_bar(**count) )?; @@ -63,12 +63,12 @@ impl fmt::Display for CommonTerms { #[cfg(test)] mod tests { use super::*; - use yansi::Paint; + use yansi; #[test] fn test_common_terms_empty() { let terms = CommonTerms::new(10); - Paint::disable(); + yansi::disable(); let display = format!("{terms}"); assert_eq!(display, "No data\n"); } @@ -85,7 +85,7 @@ mod tests { for _ in 0..20 { terms.observe(String::from("barbar")); } - Paint::disable(); + yansi::disable(); let display = format!("{terms:10}"); println!("{display}"); diff --git a/src/plot/timehist.rs b/src/plot/timehist.rs index 5ab9994..ef7b224 100644 --- a/src/plot/timehist.rs +++ b/src/plot/timehist.rs @@ -1,7 +1,7 @@ use std::fmt; use chrono::{DateTime, Duration, FixedOffset}; -use yansi::Color::Blue; +use yansi::Paint; use crate::format::HorizontalScale; use crate::plot::date_fmt_string; @@ -103,10 +103,7 @@ impl fmt::Display for TimeHistogram { writeln!( f, "Matches: {}.", - Blue.paint(format!( - "{}", - self.vec.iter().map(|r| r.count).sum::() - )), + format!("{}", self.vec.iter().map(|r| r.count).sum::()).blue(), )?; writeln!(f, "{horizontal_scale}")?; let ts_fmt = date_fmt_string(self.step.num_seconds()); @@ -114,7 +111,7 @@ impl fmt::Display for TimeHistogram { writeln!( f, "[{label}] [{count}] {bar}", - label = Blue.paint(format!("{}", row.start.format(ts_fmt))), + label = format!("{}", row.start.format(ts_fmt)).blue(), count = horizontal_scale.get_count(row.count, width_count), bar = horizontal_scale.get_bar(row.count) )?; @@ -126,11 +123,11 @@ impl fmt::Display for TimeHistogram { #[cfg(test)] mod tests { use super::*; - use yansi::Paint; + use yansi; #[test] fn test_big_time_interval() { - Paint::disable(); + yansi::disable(); let vec = vec![ DateTime::parse_from_rfc3339("2021-04-15T04:25:00+00:00").unwrap(), DateTime::parse_from_rfc3339("2022-04-15T04:25:00+00:00").unwrap(), @@ -149,7 +146,7 @@ mod tests { #[test] fn test_small_time_interval() { - Paint::disable(); + yansi::disable(); let vec = vec![ DateTime::parse_from_rfc3339("2022-04-15T04:25:00.001+00:00").unwrap(), DateTime::parse_from_rfc3339("2022-04-15T04:25:00.002+00:00").unwrap(), @@ -167,7 +164,7 @@ mod tests { #[test] fn test_single_timestamp() { - Paint::disable(); + yansi::disable(); let vec = vec![ DateTime::parse_from_rfc3339("2022-04-15T04:25:00.001+00:00").unwrap(), DateTime::parse_from_rfc3339("2022-04-15T04:25:00.001+00:00").unwrap(), diff --git a/src/plot/xy.rs b/src/plot/xy.rs index 6e1a6e3..33db6d6 100644 --- a/src/plot/xy.rs +++ b/src/plot/xy.rs @@ -2,7 +2,7 @@ use std::borrow::BorrowMut; use std::fmt; use std::ops::Range; -use yansi::Color::{Blue, Red}; +use yansi::Paint; use crate::format::F64Formatter; use crate::stats::Stats; @@ -121,12 +121,8 @@ fn print_line( writeln!( f, "[{}] {}", - Blue.paint(format!( - "{:>width$}", - f64fmt.format(range.start), - width = y_width - )), - Red.paint(row), + format!("{:>width$}", f64fmt.format(range.start), width = y_width).blue(), + row.red(), ) } @@ -134,7 +130,7 @@ fn print_line( mod tests { use super::*; use float_eq::assert_float_eq; - use yansi::Paint; + use yansi; #[test] fn basic_test() { @@ -155,7 +151,7 @@ mod tests { let stats = Stats::new(&mut [-1.0, 4.0], None); let mut plot = XyPlot::new_with_stats(3, 5, stats, Some(3)); plot.load(&[-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, -1.0]); - Paint::disable(); + yansi::disable(); let display = format!("{plot}"); assert!(display.contains("[ 3.000] ● ")); assert!(display.contains("[ 2.000] ")); @@ -167,7 +163,7 @@ mod tests { fn display_test_human_units() { let vector = &mut [1000000.0, -1000000.0, -2000000.0, -4000000.0]; let plot = XyPlot::new(vector, 3, 5, None); - Paint::disable(); + yansi::disable(); let display = format!("{plot}"); assert!(display.contains("[ 0 K] ● ")); assert!(display.contains("[-1000 K] ● ")); diff --git a/src/stats/mod.rs b/src/stats/mod.rs index 1aea0cd..dfb3e01 100644 --- a/src/stats/mod.rs +++ b/src/stats/mod.rs @@ -1,6 +1,6 @@ use std::fmt; -use yansi::Color::Blue; +use yansi::Paint; use crate::format::F64Formatter; @@ -89,24 +89,24 @@ impl fmt::Display for Stats { writeln!( f, "Samples = {len}; Min = {min}; Max = {max}", - len = Blue.paint(self.samples.to_string()), - min = Blue.paint(formatter.format(self.min)), - max = Blue.paint(formatter.format(self.max)), + len = self.samples.to_string().blue(), + min = formatter.format(self.min).blue(), + max = formatter.format(self.max).blue(), )?; writeln!( f, "Average = {avg}; Variance = {var}; STD = {std}", - avg = Blue.paint(formatter.format(self.avg)), - var = Blue.paint(format!("{:.3}", self.var)), - std = Blue.paint(format!("{:.3}", self.std)), + avg = formatter.format(self.avg).blue(), + var = format!("{:.3}", self.var).blue(), + std = format!("{:.3}", self.std).blue(), )?; writeln!( f, "p50 = {p50}; p90 = {p90}; p95 = {p95}; p99 = {p99}", - p50 = Blue.paint(formatter.format(self.p50)), - p90 = Blue.paint(formatter.format(self.p90)), - p95 = Blue.paint(formatter.format(self.p95)), - p99 = Blue.paint(formatter.format(self.p99)), + p50 = formatter.format(self.p50).blue(), + p90 = formatter.format(self.p90).blue(), + p95 = formatter.format(self.p95).blue(), + p99 = formatter.format(self.p99).blue(), ) } } @@ -116,7 +116,7 @@ mod tests { use super::*; use float_eq::assert_float_eq; use rand::{rng, seq::SliceRandom}; - use yansi::Paint; + use yansi; #[test] fn basic_test() { @@ -132,7 +132,7 @@ mod tests { #[test] fn test_display() { let stats = Stats::new(&mut [1.1, 3.3, 2.2], Some(3)); - Paint::disable(); + yansi::disable(); let display = format!("{stats}"); assert!(display.contains("Samples = 3")); assert!(display.contains("Min = 1.100")); @@ -143,7 +143,7 @@ mod tests { #[test] fn test_big_num() { let stats = Stats::new(&mut [123456789.1234, 123456788.1234], None); - Paint::disable(); + yansi::disable(); let display = format!("{stats}"); assert!(display.contains("Samples = 2")); assert!(display.contains("Min = 123456788.123")); @@ -155,7 +155,7 @@ mod tests { let mut vec: Vec = (0..100).map(|i| i as f64).collect(); vec.shuffle(&mut rng()); let stats = Stats::new(&mut vec, Some(1)); - Paint::disable(); + yansi::disable(); let display = format!("{stats}"); println!("{}", display); assert!(display.contains("p50 = 50.0"));