2022-12-25 18:29:09 -05:00
|
|
|
use super::Cmd;
|
2022-12-20 12:05:21 -05:00
|
|
|
use crate::Path;
|
|
|
|
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
|
|
|
|
2022-12-25 23:50:37 -05:00
|
|
|
#[derive(Debug)]
|
2022-12-25 18:29:09 -05:00
|
|
|
pub struct Echo {
|
|
|
|
name: &'static str,
|
|
|
|
path: Option<Path>,
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
|
|
|
|
2022-12-25 18:29:09 -05:00
|
|
|
pub const ECHO: Echo = Echo {
|
|
|
|
name: "echo",
|
|
|
|
path: Some(Path::Bin),
|
|
|
|
};
|
|
|
|
|
|
|
|
impl Cmd for Echo {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new(self.name)
|
|
|
|
.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..),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn Error>> {
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
let idx = match crate::progname() {
|
|
|
|
Some(s) if s.as_str() == self.name() => 1,
|
|
|
|
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
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
|
|
|
self.path
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
|
|
|
}
|