feat: add the seq command

This commit is contained in:
Teesh 2025-03-27 21:28:01 +02:00
parent 1bd25373cd
commit 1a405e3f8b
3 changed files with 68 additions and 0 deletions

View file

@ -22,3 +22,4 @@ command!(hostname::Hostname);
command!(tee::Tee);
command!(base64::Base64);
command!(base32::Base32);
command!(seq::Seq);

View file

@ -0,0 +1,66 @@
use boxutils::{args::ArgParser, commands::Command};
pub struct Seq;
impl Command for Seq {
fn execute(&self) {
let args = ArgParser::builder()
.add_option("-s")
.add_flag("-w")
.parse_args("seq");
let pad_with_zeroes = args.get_flag("-w");
let separator = args.get_option("-s").unwrap_or("\n");
// note(teesh): I do not know why Rust considers this
// "unused", so I'm just gonna make it shut
// up about it haha
#[allow(unused_assignments)]
let (mut firstnum, mut incnum, mut lastnum) = (1, 1, 0);
match &args.get_normal_args()[..] {
[last] => {
lastnum = last.parse().expect("seq: invalid last number");
}
[first, last] => {
firstnum = first.parse().expect("seq: invalid first number");
lastnum = last.parse().expect("seq: invalid last number");
}
[first, inc, last] => {
firstnum = first.parse().expect("seq: invalid first number");
incnum = inc.parse().expect("seq: invalid inc number");
lastnum = last.parse().expect("seq: invalid last number");
}
_ => panic!("seq: malformed arguments"),
}
let mut accumulator = firstnum;
// Find the width of the largest number
let width = if pad_with_zeroes {
// Calculate width based on the largest number in the sequence
let max_value = if lastnum > firstnum {
lastnum
} else {
firstnum
};
max_value.to_string().len()
} else {
0 // no padding if not using -w
};
while accumulator <= lastnum {
// If padding is enabled, format the number with leading zeros
if pad_with_zeroes {
print!("{:0width$}", accumulator, width = width);
} else {
print!("{}", accumulator);
}
accumulator += incnum;
if accumulator <= lastnum {
print!("{}", separator);
}
}
}
}

View file

@ -32,6 +32,7 @@ pub fn get_registry() -> CommandRegistry {
"tee" => coreutils::commands::Tee,
"base64" => coreutils::commands::Base64,
"base32" => coreutils::commands::Base32,
"seq" => coreutils::commands::Seq,
"box" => Boxcmd
});