Browse Source

Rustfmt code

pull/2/head
JuanLeon Lahoz 5 years ago
parent
commit
85bac44dc4
  1. 405
      Cargo.lock
  2. 47
      src/histogram.rs
  3. 48
      src/main.rs
  4. 17
      src/plot.rs
  5. 30
      src/reader.rs
  6. 24
      src/stats.rs

405
Cargo.lock generated

@ -0,0 +1,405 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "clap"
version = "3.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"indexmap",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "clap_derive"
version = "3.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "darling"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9d6ddad5866bb2170686ed03f6839d31a76e5407d80b1c334a2c24618543ffa"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9ced1fd13dc386d5a8315899de465708cf34ee2a6d9394654515214e67bb846"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a7a1445d54b2f9792e3b31a3e715feabbace393f38dc4ffd49d94ee9bc487d5"
dependencies = [
"darling_core",
"quote",
"syn",
]
[[package]]
name = "derive_builder"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ef25735c9f0d0c547d2794701600c94abf030ecb740fad1673fa64461f3573"
dependencies = [
"derive_builder_core",
"derive_builder_macro",
]
[[package]]
name = "derive_builder_core"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3150f1e84602847b99d3eeb702487fc364f7d6c94f634e944a68fdbaea09e457"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "derive_builder_macro"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca1008bddefdc08d1e734aeb27b94f384390af261b4d1a8fb51fe19c577f05c"
dependencies = [
"derive_builder_core",
"syn",
]
[[package]]
name = "float_eq"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb23b6902f3cdc0544f9916b4c092f46f4ff984e219d5a0c538b6b3539885af3"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
[[package]]
name = "heck"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
dependencies = [
"libc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "indexmap"
version = "1.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "isatty"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31a8281fc93ec9693494da65fbf28c0c2aa60a2eaec25dc58e2f31952e95edc"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"winapi",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
[[package]]
name = "lowcharts"
version = "0.1.0"
dependencies = [
"clap",
"derive_builder",
"float_eq",
"isatty",
"regex",
"yansi",
]
[[package]]
name = "memchr"
version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]]
name = "os_str_bytes"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "regex"
version = "1.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48fe99c6bd8b1cc636890bcc071842de909d902c81ac7dab53ba33c421ab8ffb"
dependencies = [
"proc-macro2",
"quote",
"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 = "textwrap"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-segmentation"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "yansi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fc79f4a1e39857fc00c3f662cbf2651c771f00e9c15fe2abc341806bd46bd71"

47
src/histogram.rs

@ -1,11 +1,10 @@
use std::fmt;
use std::ops::Range;
use yansi::Color::{Red, Blue, Green};
use yansi::Color::{Blue, Green, Red};
use crate::stats::Stats;
#[derive(Debug)]
pub struct Bucket {
range: Range<f64>,
@ -14,10 +13,7 @@ pub struct Bucket {
impl Bucket {
fn new(range: Range<f64>) -> Bucket {
Bucket {
range,
count: 0,
}
Bucket { range, count: 0 }
}
fn inc(&mut self) {
@ -78,7 +74,9 @@ impl Histogram {
impl fmt::Display for Histogram {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.stats)?;
let writer = HistWriter {width: f.width().unwrap_or(110)};
let writer = HistWriter {
width: f.width().unwrap_or(110),
};
writer.write(f, &self)
}
}
@ -88,7 +86,6 @@ struct HistWriter {
}
impl HistWriter {
pub fn write(&self, f: &mut fmt::Formatter, hist: &Histogram) -> fmt::Result {
let width_range = Self::get_width(hist);
let width_count = ((hist.top as f64).log10().ceil() as usize).max(1);
@ -105,27 +102,33 @@ impl HistWriter {
Ok(())
}
fn write_bucket(&self, f: &mut fmt::Formatter, bucket: &Bucket, divisor: usize, width: usize, width_count: usize) -> fmt::Result {
let bar = Red.paint(format!("{:∎<width$}", "", width=bucket.count / divisor));
fn write_bucket(
&self,
f: &mut fmt::Formatter,
bucket: &Bucket,
divisor: usize,
width: usize,
width_count: usize,
) -> fmt::Result {
let bar = Red.paint(format!("{:∎<width$}", "", width = bucket.count / divisor));
writeln!(
f,
"[{range}] [{count}] {bar}",
range=Blue.paint(
format!(
"{:width$.3} .. {:width$.3}",
bucket.range.start,
bucket.range.end,
width = width,
)
),
count=Green.paint(format!("{:width$}", bucket.count, width=width_count)),
bar=bar
range = Blue.paint(format!(
"{:width$.3} .. {:width$.3}",
bucket.range.start,
bucket.range.end,
width = width,
)),
count = Green.paint(format!("{:width$}", bucket.count, width = width_count)),
bar = bar
)
}
fn get_width(hist: &Histogram) -> usize {
format!("{:.3}", hist.stats.min).len().max(format!("{:.3}", hist.max).len())
format!("{:.3}", hist.stats.min)
.len()
.max(format!("{:.3}", hist.max).len())
}
fn get_max_bar_len(&self, fixed_width: usize) -> usize {

48
src/main.rs

@ -1,39 +1,35 @@
use std::env;
use yansi::Paint;
use yansi::Color::Red;
use yansi::Color::Yellow;
use clap::{AppSettings, Clap};
use isatty::stdout_isatty;
use regex::Regex;
use yansi::Color::Red;
use yansi::Color::Yellow;
use yansi::Paint;
#[macro_use]
extern crate derive_builder;
mod stats;
mod histogram;
mod reader;
mod plot;
mod reader;
mod stats;
fn disable_color_if_needed(option: &str) {
match option {
"no" => Paint::disable(),
"auto" => {
match env::var("TERM") {
Ok(value) if value == "dumb" => Paint::disable(),
_ => {
if !stdout_isatty() {
Paint::disable();
}
"auto" => match env::var("TERM") {
Ok(value) if value == "dumb" => Paint::disable(),
_ => {
if !stdout_isatty() {
Paint::disable();
}
}
},
_ => ()
_ => (),
}
}
/// Tool to draw low-resolution graphs in terminal
#[derive(Clap)]
#[clap(setting = AppSettings::ColoredHelp)]
@ -68,7 +64,6 @@ struct Opts {
subcmd: SubCommand,
}
#[derive(Clap)]
enum SubCommand {
/// Plot an histogram from input values
@ -94,23 +89,20 @@ struct Plot {
height: usize,
}
fn main() {
let opts: Opts = Opts::parse();
disable_color_if_needed(&opts.color);
let mut builder = reader::DataReaderBuilder::default();
builder.verbose(opts.verbose);
if opts.min.is_some() || opts.max.is_some() {
builder.range(
opts.min.unwrap_or(f64::NEG_INFINITY)..opts.max.unwrap_or(f64::INFINITY)
);
builder.range(opts.min.unwrap_or(f64::NEG_INFINITY)..opts.max.unwrap_or(f64::INFINITY));
}
if let Some(string) = opts.regex {
match Regex::new(&string) {
Ok(re) => {
builder.regex(re);
},
_ => eprintln!("[{}]: Failed to parse regex {}", Red.paint("ERROR"), string)
}
_ => eprintln!("[{}]: Failed to parse regex {}", Red.paint("ERROR"), string),
};
}
let reader = builder.build().unwrap();
@ -127,17 +119,13 @@ fn main() {
let mut histogram = histogram::Histogram::new(
o.intervals,
(stats.max - stats.min) / o.intervals as f64,
stats
stats,
);
histogram.load(&vec);
println!("{:width$}", histogram, width=opts.width);
},
println!("{:width$}", histogram, width = opts.width);
}
SubCommand::Plot(o) => {
let mut plot = plot::Plot::new(
opts.width,
o.height,
stats
);
let mut plot = plot::Plot::new(opts.width, o.height, stats);
plot.load(&vec);
print!("{}", plot);
}

17
src/plot.rs

@ -1,11 +1,10 @@
use std::fmt;
use std::ops::Range;
use yansi::Color::{Red, Blue};
use yansi::Color::{Blue, Red};
use crate::stats::Stats;
#[derive(Debug)]
pub struct Plot {
x_axis: Vec<f64>,
@ -56,24 +55,28 @@ impl fmt::Display for Plot {
}
}
fn print_line(f: &mut fmt::Formatter, x_axis: &[f64], range: Range<f64>, y_width: usize) -> fmt::Result {
let mut row = format!("{: <width$}", "", width=x_axis.len());
fn print_line(
f: &mut fmt::Formatter,
x_axis: &[f64],
range: Range<f64>,
y_width: usize,
) -> fmt::Result {
let mut row = format!("{: <width$}", "", width = x_axis.len());
// The reverse in the enumeration is to avoid breaking char boundaries
// because of unicode char ● having more bytes than ascii chars.
for (x, value) in x_axis.iter().enumerate().rev() {
if range.contains(value) {
row.replace_range(x..x+1, "●".as_ref());
row.replace_range(x..x + 1, "●".as_ref());
}
}
writeln!(
f,
"[{}] {}",
Blue.paint(format!("{y:.*}", y_width, y=range.start.to_string())),
Blue.paint(format!("{y:.*}", y_width, y = range.start.to_string())),
Red.paint(row),
)
}
#[cfg(test)]
mod tests {
use super::*;

30
src/reader.rs

@ -1,10 +1,9 @@
use std::io::{self, BufRead};
use std::fs::File;
use std::io::{self, BufRead};
use std::ops::Range;
use regex::Regex;
use yansi::Color::{Red, Magenta};
use yansi::Color::{Magenta, Red};
#[derive(Debug, Default, Builder)]
pub struct DataReader {
@ -16,10 +15,7 @@ pub struct DataReader {
verbose: bool,
}
impl DataReader {
pub fn read(&self, path: String) -> Vec<f64> {
let mut vec: Vec<f64> = vec![];
match path.as_str() {
@ -43,18 +39,22 @@ impl DataReader {
let mut vec: Vec<f64> = Vec::new();
let line_parser = match self.regex {
Some(_) => Self::parse_regex,
None => Self::parse_float
None => Self::parse_float,
};
for line in lines {
match line {
Ok(as_string) => if let Some(n) = line_parser(&self, &as_string) {
match &self.range {
Some(range) => if range.contains(&n) {
vec.push(n);
},
_ => vec.push(n)
Ok(as_string) => {
if let Some(n) = line_parser(&self, &as_string) {
match &self.range {
Some(range) => {
if range.contains(&n) {
vec.push(n);
}
}
_ => vec.push(n),
}
}
},
}
Err(error) => eprintln!("[{}]: {}", Red.paint("ERROR"), error),
}
}
@ -86,7 +86,7 @@ impl DataReader {
} else {
None
}
},
}
None => {
if self.verbose {
eprintln!(

24
src/stats.rs

@ -2,7 +2,6 @@ use std::fmt;
use yansi::Color::Blue;
#[derive(Debug)]
pub struct Stats {
pub min: f64,
@ -15,7 +14,6 @@ pub struct Stats {
}
impl Stats {
pub fn new(vec: &[f64]) -> Stats {
let mut max = vec[0];
let mut min = max;
@ -29,7 +27,15 @@ impl Stats {
}
let var = temp / vec.len() as f64;
let std = var.sqrt();
Stats { min, max, avg, std, var, sum, samples: vec.len() }
Stats {
min,
max,
avg,
std,
var,
sum,
samples: vec.len(),
}
}
}
@ -38,16 +44,16 @@ impl fmt::Display for Stats {
writeln!(
f,
"Samples = {len:.5}; Min = {min:.5}; Max = {max:.5}",
len=Blue.paint(self.samples.to_string()),
min=Blue.paint(self.min.to_string()),
max=Blue.paint(self.max.to_string()),
len = Blue.paint(self.samples.to_string()),
min = Blue.paint(self.min.to_string()),
max = Blue.paint(self.max.to_string()),
)?;
writeln!(
f,
"Average = {avg:.5}; Variance = {var:.5}; STD = {std:.5}",
avg=Blue.paint(self.avg.to_string()),
var=Blue.paint(self.var.to_string()),
std=Blue.paint(self.std.to_string())
avg = Blue.paint(self.avg.to_string()),
var = Blue.paint(self.var.to_string()),
std = Blue.paint(self.std.to_string())
)
}
}

Loading…
Cancel
Save