Skip to content

Commit 395ccbe

Browse files
committed
Add -k column selector. Remove serde, use StringRecord directly to keep performance.
1 parent 11f2eb0 commit 395ccbe

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

Cargo.toml

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

1111
[dependencies]
1212
plotters = "^0.3"
13-
serde = { version = "^1.0", features = ["derive"] }
1413
csv = "^1.1"
1514
structopt = "^0.3"
1615
textplots = "^0.8"

README.md

Lines changed: 2 additions & 1 deletion
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.2
13+
hist 0.4.4
1414
Plots histogram of input
1515
1616
USAGE:
@@ -25,6 +25,7 @@ FLAGS:
2525
OPTIONS:
2626
-T, --Title <Title> optional title above the plot [default: Counts distribution]
2727
-g, --geometry <geometry> the x and y size of the plot [default: 1280x960]
28+
-k, --key <key> key (column) selector [default: 1]
2829
-o, --output <output> file to save PNG plot to [default: histogram.png]
2930
-s, --save <save> save counts data to file as TSV, use - for STDOUT
3031
--xdesc <xdesc> x-axis label [default: Rank]

src/bin/hist.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate plotters;
2-
extern crate serde;
32
extern crate csv;
3+
use csv::StringRecord;
44
use plotters::prelude::*;
5-
use serde::Deserialize;
65
use std::io;
76
use std::collections::BTreeMap;
87
use structopt::StructOpt;
@@ -19,6 +18,10 @@ struct Opt
1918
/// optional file with on entry per line [default: STDIN]
2019
input: Option<PathBuf>,
2120

21+
#[structopt(long, short, default_value = "1")]
22+
/// key (column) selector
23+
key: usize,
24+
2225
#[structopt(parse(from_os_str), long, short, default_value = "histogram.png")]
2326
/// file to save PNG plot to
2427
output: PathBuf,
@@ -52,12 +55,6 @@ struct Opt
5255
ydesc: String,
5356
}
5457

55-
#[derive(Debug, Deserialize)]
56-
struct Record
57-
{
58-
key: String,
59-
}
60-
6158
fn main() -> Result<(), Box<dyn Error>>
6259
{
6360
let opt = Opt::from_args();
@@ -72,19 +69,18 @@ fn main() -> Result<(), Box<dyn Error>>
7269
Box::new(io::stdin())
7370
};
7471

75-
let key_counts =
76-
csv::ReaderBuilder::new()
72+
let mut reader = csv::ReaderBuilder::new()
7773
.has_headers(false)
7874
.delimiter(b'\t')
79-
.from_reader(input)
80-
.deserialize::<Record>()
81-
.fold(BTreeMap::new(), |mut map,rec|
82-
{
83-
let s = rec.unwrap().key;
84-
//*map.entry(s).or_insert(0) += 1;
85-
map.entry(s).and_modify(|e| *e += 1 ).or_insert(1);
86-
map
87-
});
75+
.from_reader(input);
76+
77+
let mut key_counts = BTreeMap::new();
78+
let mut record = StringRecord::new();
79+
while reader.read_record(&mut record)?
80+
{
81+
let s = record.get(opt.key - 1).unwrap().to_string();
82+
key_counts.entry(s).and_modify(|e| *e += 1 ).or_insert(1);
83+
}
8884

8985
if let Some(path) = &opt.save
9086
{

0 commit comments

Comments
 (0)