diff --git a/Cargo.toml b/Cargo.toml index c4aed35..d471cab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hist-cli" -version = "0.4.4" +version = "0.4.5" edition = "2021" authors = ["Andreas Hauser "] description = "Commandline tool for plotting frequency ranked histograms of TSV/CSV data" diff --git a/README.md b/README.md index 5faf828..b2e2055 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ export PATH="$HOME/.cargo/bin:$PATH" ## Usage ``` -hist 0.4.4 +hist 0.4.5 Plots histogram of input USAGE: @@ -23,13 +23,14 @@ FLAGS: -V, --version Prints version information OPTIONS: - -T, --Title optional title above the plot [default: Counts distribution] - -g, --geometry <geometry> the x and y size of the plot [default: 1280x960] - -k, --key <key> key (column) selector [default: 1] - -o, --output <output> file to save PNG plot to [default: histogram.png] - -s, --save <save> save counts data to file as TSV, use - for STDOUT - --xdesc <xdesc> x-axis label [default: Rank] - --ydesc <ydesc> y-axis label [default: Counts] + -T, --Title <Title> optional title above the plot [default: Counts distribution] + -d, --delimiter <delimiter> column delimiter [default: \t] + -g, --geometry <geometry> the x and y size of the plot [default: 1280x960] + -k, --key <key> key (column) selector [default: 1] + -o, --output <output> file to save PNG plot to [default: histogram.png] + -s, --save <save> save counts data to file as TSV, use - for STDOUT + --xdesc <xdesc> x-axis label [default: Rank] + --ydesc <ydesc> y-axis label [default: Counts] ARGS: <input> optional file with on entry per line [default: STDIN] diff --git a/src/bin/hist.rs b/src/bin/hist.rs index 64d84c5..0dbb73d 100644 --- a/src/bin/hist.rs +++ b/src/bin/hist.rs @@ -18,6 +18,11 @@ struct Opt /// optional file with on entry per line [default: STDIN] input: Option<PathBuf>, + // r"" makes it prinable as escaped in default + #[structopt(short, long, default_value = r"\t")] + /// column delimiter + delimiter: String, + #[structopt(long, short, default_value = "1")] /// key (column) selector key: usize, @@ -69,9 +74,17 @@ fn main() -> Result<(), Box<dyn Error>> Box::new(io::stdin()) }; + // accept escaped delimiters + // could be expanded to aliases e.g. "TAB" + let delimiter = match opt.delimiter.as_str() + { + r"\t" => b'\t', // structopt needs r"" to show default as escaped, also for sepcifiying as escaped in CLI + _ => *opt.delimiter.as_bytes().first().expect("Not a valid delimiter") + }; + let mut reader = csv::ReaderBuilder::new() .has_headers(false) - .delimiter(b'\t') + .delimiter(delimiter) .from_reader(input); let mut key_counts = BTreeMap::new();