2022-12-25 18:29:09 -05:00
|
|
|
use super::Cmd;
|
2022-12-20 12:05:21 -05:00
|
|
|
use clap::{Arg, Command};
|
2022-12-25 18:29:09 -05:00
|
|
|
use std::{env, error::Error};
|
2022-12-20 12:05:21 -05:00
|
|
|
|
2023-04-17 23:27:57 -04:00
|
|
|
#[derive(Debug)]
|
2023-01-13 01:08:32 -05:00
|
|
|
pub struct Echo;
|
2022-12-25 18:29:09 -05:00
|
|
|
|
|
|
|
impl Cmd for Echo {
|
|
|
|
fn cli(&self) -> clap::Command {
|
2023-01-13 01:08:32 -05:00
|
|
|
Command::new("echo")
|
2022-12-25 18:29:09 -05:00
|
|
|
.about("Display a line of text")
|
|
|
|
.long_about("Echo the STRING(s) to standard output")
|
|
|
|
.author("Nathan Fisher")
|
|
|
|
.args([
|
|
|
|
Arg::new("inline")
|
|
|
|
.short('n')
|
|
|
|
.help("Do not output a trailing newline"),
|
|
|
|
Arg::new("STRING").num_args(1..),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2023-02-04 08:54:27 -05:00
|
|
|
fn run(&self, _matches: &clap::ArgMatches) -> Result<(), Box<dyn Error>> {
|
2022-12-25 18:29:09 -05:00
|
|
|
let args: Vec<String> = env::args().collect();
|
2023-02-05 23:50:59 -05:00
|
|
|
let idx = match shitbox::progname() {
|
2023-01-13 01:08:32 -05:00
|
|
|
Some(s) if s.as_str() == "echo" => 1,
|
2022-12-25 18:29:09 -05:00
|
|
|
Some(_) => 2,
|
|
|
|
None => unreachable!(),
|
|
|
|
};
|
|
|
|
let len = args.len();
|
|
|
|
let n = len > idx && args[idx] == "-n";
|
|
|
|
let i = if n { idx + 1 } else { idx };
|
|
|
|
for (index, arg) in args.iter().enumerate().skip(i) {
|
|
|
|
if index < len - 1 {
|
|
|
|
print!("{arg} ");
|
|
|
|
} else {
|
|
|
|
print!("{arg}");
|
|
|
|
}
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
2022-12-25 18:29:09 -05:00
|
|
|
if !n {
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
Ok(())
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
2022-12-25 18:29:09 -05:00
|
|
|
|
2023-02-05 23:50:59 -05:00
|
|
|
fn path(&self) -> Option<shitbox::Path> {
|
|
|
|
Some(shitbox::Path::Bin)
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
|
|
|
}
|