Skip to content

Commit f77708f

Browse files
committed
Add option to specify delimiter including escaped.
1 parent 395ccbe commit f77708f

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hist-cli"
3-
version = "0.4.4"
3+
version = "0.4.5"
44
edition = "2021"
55
authors = ["Andreas Hauser <Andreas.Hauser@LMU.de>"]
66
description = "Commandline tool for plotting frequency ranked histograms of TSV/CSV data"

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export PATH="$HOME/.cargo/bin:$PATH"
1010

1111
## Usage
1212
```
13-
hist 0.4.4
13+
hist 0.4.5
1414
Plots histogram of input
1515
1616
USAGE:
@@ -23,13 +23,14 @@ FLAGS:
2323
-V, --version Prints version information
2424
2525
OPTIONS:
26-
-T, --Title <Title> optional title above the plot [default: Counts distribution]
27-
-g, --geometry <geometry> the x and y size of the plot [default: 1280x960]
28-
-k, --key <key> key (column) selector [default: 1]
29-
-o, --output <output> file to save PNG plot to [default: histogram.png]
30-
-s, --save <save> save counts data to file as TSV, use - for STDOUT
31-
--xdesc <xdesc> x-axis label [default: Rank]
32-
--ydesc <ydesc> y-axis label [default: Counts]
26+
-T, --Title <Title> optional title above the plot [default: Counts distribution]
27+
-d, --delimiter <delimiter> column delimiter [default: \t]
28+
-g, --geometry <geometry> the x and y size of the plot [default: 1280x960]
29+
-k, --key <key> key (column) selector [default: 1]
30+
-o, --output <output> file to save PNG plot to [default: histogram.png]
31+
-s, --save <save> save counts data to file as TSV, use - for STDOUT
32+
--xdesc <xdesc> x-axis label [default: Rank]
33+
--ydesc <ydesc> y-axis label [default: Counts]
3334
3435
ARGS:
3536
<input> optional file with on entry per line [default: STDIN]

src/bin/hist.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ struct Opt
1818
/// optional file with on entry per line [default: STDIN]
1919
input: Option<PathBuf>,
2020

21+
// r"" makes it prinable as escaped in default
22+
#[structopt(short, long, default_value = r"\t")]
23+
/// column delimiter
24+
delimiter: String,
25+
2126
#[structopt(long, short, default_value = "1")]
2227
/// key (column) selector
2328
key: usize,
@@ -69,9 +74,17 @@ fn main() -> Result<(), Box<dyn Error>>
6974
Box::new(io::stdin())
7075
};
7176

77+
// accept escaped delimiters
78+
// could be expanded to aliases e.g. "TAB"
79+
let delimiter = match opt.delimiter.as_str()
80+
{
81+
r"\t" => b'\t', // structopt needs r"" to show default as escaped, also for sepcifiying as escaped in CLI
82+
_ => *opt.delimiter.as_bytes().first().expect("Not a valid delimiter")
83+
};
84+
7285
let mut reader = csv::ReaderBuilder::new()
7386
.has_headers(false)
74-
.delimiter(b'\t')
87+
.delimiter(delimiter)
7588
.from_reader(input);
7689

7790
let mut key_counts = BTreeMap::new();

0 commit comments

Comments
 (0)