coreutils/seq/seq.rs

120 lines
3.4 KiB
Rust
Raw Normal View History

2014-03-31 16:40:21 +00:00
#![crate_id(name="seq", vers="1.0.0", author="Daniel MacDougall")]
2014-02-05 19:46:28 +00:00
2014-03-31 16:40:21 +00:00
#![feature(macro_rules)]
2014-02-05 19:46:28 +00:00
// TODO: Make -w flag work with decimals
// TODO: Support -f flag
extern crate getopts;
extern crate libc;
2014-02-05 19:46:28 +00:00
use std::os;
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
mod util;
static NAME: &'static str = "seq";
2014-05-30 08:35:54 +00:00
fn print_usage(opts: &[getopts::OptGroup]) {
2014-02-05 19:46:28 +00:00
println!("seq 1.0.0\n");
println!("Usage:\n seq [-w] [-s string] [-t string] [first [step]] last\n");
println!("{:s}", getopts::usage("Print sequences of numbers", opts));
2014-02-05 19:46:28 +00:00
}
2014-05-25 09:20:52 +00:00
fn parse_float(s: &str) -> Result<f32, String>{
2014-02-05 19:46:28 +00:00
match from_str(s) {
Some(n) => Ok(n),
None => Err(format!("seq: invalid floating point argument: {:s}", s))
}
}
2014-05-25 09:20:52 +00:00
fn escape_sequences(s: &str) -> String {
2014-02-05 19:46:28 +00:00
s.replace("\\n", "\n").
replace("\\t", "\t")
}
#[allow(dead_code)]
fn main() { os::set_exit_status(uumain(os::args())); }
2014-05-28 11:43:37 +00:00
pub fn uumain(args: Vec<String>) -> int {
2014-05-30 08:35:54 +00:00
let opts = [
getopts::optopt("s", "separator", "Separator character (defaults to \\n)", ""),
getopts::optopt("t", "terminator", "Terminator character (defaults to separator)", ""),
getopts::optflag("w", "widths", "Equalize widths of all numbers by padding with zeros"),
getopts::optflag("h", "help", "print this help text and exit"),
getopts::optflag("V", "version", "print version and exit"),
2014-02-05 19:46:28 +00:00
];
let matches = match getopts::getopts(args.tail(), opts) {
2014-02-05 19:46:28 +00:00
Ok(m) => { m }
Err(f) => {
2014-06-15 10:50:40 +00:00
show_error!("{}", f);
2014-02-05 19:46:28 +00:00
print_usage(opts);
return 1;
2014-02-05 19:46:28 +00:00
}
};
if matches.opt_present("help") {
print_usage(opts);
return 0;
2014-02-05 19:46:28 +00:00
}
if matches.opt_present("version") {
println!("seq 1.0.0");
return 0;
2014-02-05 19:46:28 +00:00
}
if matches.free.len() < 1 || matches.free.len() > 3 {
print_usage(opts);
return 1;
2014-02-05 19:46:28 +00:00
}
let first = if matches.free.len() > 1 {
2014-03-22 08:18:52 +00:00
match parse_float(matches.free.get(0).as_slice()) {
2014-02-05 19:46:28 +00:00
Ok(n) => n,
2014-06-09 03:26:51 +00:00
Err(s) => { show_error!("{:s}", s); return 1; }
2014-02-05 19:46:28 +00:00
}
} else {
1.0
};
let step = if matches.free.len() > 2 {
2014-03-22 08:18:52 +00:00
match parse_float(matches.free.get(1).as_slice()) {
2014-02-05 19:46:28 +00:00
Ok(n) => n,
2014-06-09 03:26:51 +00:00
Err(s) => { show_error!("{:s}", s); return 1; }
2014-02-05 19:46:28 +00:00
}
} else {
1.0
};
2014-03-22 08:18:52 +00:00
let last = match parse_float(matches.free.get(matches.free.len()-1).as_slice()) {
2014-02-05 19:46:28 +00:00
Ok(n) => n,
2014-06-09 03:26:51 +00:00
Err(s) => { show_error!("{:s}", s); return 1; }
2014-02-05 19:46:28 +00:00
};
2014-05-28 06:33:39 +00:00
let separator = escape_sequences(matches.opt_str("s").unwrap_or("\n".to_string()).as_slice());
let terminator = escape_sequences(matches.opt_str("t").unwrap_or(separator.to_string()).as_slice());
2014-02-05 19:46:28 +00:00
print_seq(first, step, last, separator, terminator, matches.opt_present("w"));
0
2014-02-05 19:46:28 +00:00
}
fn done_printing(next: f32, step: f32, last: f32) -> bool {
if step > 0f32 {
next > last
} else {
next < last
}
}
2014-05-25 09:20:52 +00:00
fn print_seq(first: f32, step: f32, last: f32, separator: String, terminator: String, pad: bool) {
2014-02-05 19:46:28 +00:00
let mut i = first;
let maxlen = first.max(last).to_str().len();
2014-02-05 19:46:28 +00:00
while !done_printing(i, step, last) {
let ilen = i.to_str().len();
if pad && ilen < maxlen {
for _ in range(0, maxlen - ilen) {
print!("0");
}
}
print!("{:f}", i);
i += step;
if !done_printing(i, step, last) {
print!("{:s}", separator);
}
}
print!("{:s}", terminator);
}