forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.rs
85 lines (75 loc) · 2.79 KB
/
flatten.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
static USAGE: &str = "
Prints flattened records such that fields are labeled separated by a new line.
This mode is particularly useful for viewing one record at a time. Each
record is separated by a special '#' character (on a line by itself), which
can be changed with the --separator flag.
There is also a condensed view (-c or --condense) that will shorten the
contents of each field to provide a summary view.
Usage:
qsv flatten [options] [<input>]
qsv flatten --help
flatten options:
-c, --condense <arg> Limits the length of each field to the value
specified. If the field is UTF-8 encoded, then
<arg> refers to the number of code points.
Otherwise, it refers to the number of bytes.
-s, --separator <arg> A string of characters to write after each record.
When non-empty, a new line is automatically
appended to the separator.
[default: #]
Common options:
-h, --help Display this message
-n, --no-headers When set, the first row will not be interpreted
as headers. When set, the name of each field
will be its index.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
";
use std::{
borrow::Cow,
io::{self, Write},
};
use serde::Deserialize;
use tabwriter::TabWriter;
use crate::{
config::{Config, Delimiter},
util, CliResult,
};
#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
flag_condense: Option<usize>,
flag_separator: String,
flag_no_headers: bool,
flag_delimiter: Option<Delimiter>,
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
.checkutf8(false)
.no_headers(args.flag_no_headers);
let mut rdr = rconfig.reader()?;
let headers = rdr.byte_headers()?.clone();
let mut wtr = TabWriter::new(io::stdout());
let mut first = true;
for r in rdr.byte_records() {
if !first && !args.flag_separator.is_empty() {
writeln!(&mut wtr, "{}", args.flag_separator)?;
}
first = false;
let r = r?;
for (i, (header, field)) in headers.iter().zip(&r).enumerate() {
if rconfig.no_headers {
write!(&mut wtr, "{i}")?;
} else {
wtr.write_all(header)?;
}
wtr.write_all(b"\t")?;
wtr.write_all(&util::condense(Cow::Borrowed(field), args.flag_condense))?;
wtr.write_all(b"\n")?;
}
}
wtr.flush()?;
Ok(())
}