-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcut.rs
More file actions
175 lines (134 loc) · 4.87 KB
/
rcut.rs
File metadata and controls
175 lines (134 loc) · 4.87 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::io;
use std::path::PathBuf;
use structopt::StructOpt;
use std::fs::File;
use std::io::{BufRead};
use std::path::Path;
// handle sigpipe properly so that things like rcut | head won't panic
#[cfg(unix)]
fn handle_sigpipe() {
unsafe {
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
}
}
#[cfg(not(unix))]
fn handle_sigpipe() {
// no-op
}
static EOLMARKER: &str = "@#$%^&*|";
#[derive(Debug, StructOpt)]
#[structopt(name = "rcut", about = "cut written in rust, supporting string delimeters (not just a single char)")]
struct RcutOpt {
/// Activate verbose mode
// short and long flags (-v, --verbose) will be deduced from the field's name
#[structopt(short, long)]
verbose: bool,
///Show count number (one-indexed) with each field. -f will be ignored
#[structopt(short = "n", long = "--show-count")]
numbercount: bool,
#[structopt(short = "d", long = "delimeter string", default_value = " ")]
delim: String,
#[structopt(short = "f", long = "fields", default_value = "1")]
fields: String,
// the /// comments will be used as annotation in the help output
/// Input file (default will be stdin)
#[structopt(parse(from_os_str))]
input: Option<PathBuf> // makes it optional
}
//fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn get_nth_token(line: &String, delim: String, field: usize) -> String {
let token_opt = line.split(&delim).nth(field - 1); //unwrap().to_string();
if token_opt == None {
return String::from(EOLMARKER);
}
return token_opt.unwrap().to_string();
}
fn main() {
handle_sigpipe();
let opt = RcutOpt::from_args();
if opt.verbose {
println!("{:?}", opt);
// println!("delim: {}", opt.delim);
}
let mut fields = Vec::new();
for d in opt.fields.split(',') {
fields.push(d.parse::<usize>().unwrap());
}
let delim = &opt.delim;
if opt.input == None {
let stdin = io::stdin();
// do the if statement up here so that it doesn't have compare every time
// pro is faster speed, con is a dup code
if opt.numbercount == true {
for line in stdin.lock().lines() {
// println!("{}", line.unwrap());
let l = &line.unwrap();
let mut c = 1;
loop {
// default output separator is space
let t = get_nth_token(l, delim.to_string(), c);
if t == EOLMARKER {
break;
}
// check that line has ended
print!("[{}]{} ", c, t);
c += 1;
}
println!();
}
} else {
for line in stdin.lock().lines() {
// println!("{}", line.unwrap());
let l = &line.unwrap();
for f in &fields {
// default output separator is space
let t = get_nth_token(l, delim.to_string(),*f);
if t != EOLMARKER {
print!("{} ", t);
}
}
println!();
}
}
} else {
if let Ok(lines) = read_lines(&opt.input.unwrap().into_os_string()) {
// Consumes the iterator, returns an (Optional) String
if opt.numbercount == true {
for line in lines {
let mut c = 1;
loop {
// default output separator is space
if let Ok(l) = &line {
let t = get_nth_token(&l, delim.to_string(), c);
if t == EOLMARKER {
break;
}
// check that line has ended
print!("[{}]{} ", c, t);
c += 1;
}
}
println!();
}
} else {
for line in lines {
// println!("{:?}", line);
for f in &fields {
if let Ok(l) = &line {
let t = get_nth_token(&l, delim.to_string(), *f);
if t != EOLMARKER {
print!("{} ", t);
}
}
}
println!();
}
}
}
}
}