Skip to content

Commit

Permalink
Add option to specify delimiter including escaped.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahcm committed May 25, 2022
1 parent 395ccbe commit f77708f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hist-cli"
version = "0.4.4"
version = "0.4.5"
edition = "2021"
authors = ["Andreas Hauser <[email protected]>"]
description = "Commandline tool for plotting frequency ranked histograms of TSV/CSV data"
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export PATH="$HOME/.cargo/bin:$PATH"

## Usage
```
hist 0.4.4
hist 0.4.5
Plots histogram of input
USAGE:
Expand All @@ -23,13 +23,14 @@ FLAGS:
-V, --version Prints version information
OPTIONS:
-T, --Title <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]
Expand Down
15 changes: 14 additions & 1 deletion src/bin/hist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f77708f

Please sign in to comment.