Add sleep applet
This commit is contained in:
parent
228e1c779f
commit
a2e80dc867
10
src/cli.rs
10
src/cli.rs
@ -1,5 +1,5 @@
|
|||||||
use crate::cmd::{bootstrap, echo, head, hostname, r#false, r#true};
|
use crate::cmd::{bootstrap, echo, head, hostname, r#false, r#true, sleep};
|
||||||
use clap::{value_parser, Arg, ArgAction, Command};
|
use clap::Command;
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
let matches = Command::new("shitbox")
|
let matches = Command::new("shitbox")
|
||||||
@ -12,14 +12,16 @@ pub fn run() {
|
|||||||
r#false::cli(),
|
r#false::cli(),
|
||||||
head::cli(),
|
head::cli(),
|
||||||
hostname::cli(),
|
hostname::cli(),
|
||||||
|
sleep::cli(),
|
||||||
r#true::cli(),
|
r#true::cli(),
|
||||||
])
|
])
|
||||||
.get_matches();
|
.get_matches();
|
||||||
match matches.subcommand() {
|
match matches.subcommand() {
|
||||||
Some(("echo", _matches)) => echo::run(),
|
Some(("echo", _matches)) => echo::run(),
|
||||||
Some(("false", _matches)) => r#false::run(),
|
Some(("false", _matches)) => r#false::run(),
|
||||||
Some(("head", matches)) => head::run(&matches),
|
Some(("head", matches)) => head::run(matches),
|
||||||
Some(("hostname", matches)) => hostname::run(&matches),
|
Some(("hostname", matches)) => hostname::run(matches),
|
||||||
|
Some(("sleep", matches)) => sleep::run(matches),
|
||||||
Some(("true", _matches)) => r#true::run(),
|
Some(("true", _matches)) => r#true::run(),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
use clap::{value_parser, Arg, ArgMatches, Command};
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn cli() -> Command {
|
pub fn cli() -> Command {
|
||||||
Command::new("bootstrap")
|
Command::new("bootstrap")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
|
@ -4,6 +4,7 @@ use std::env;
|
|||||||
|
|
||||||
pub const PATH: Path = Path::Bin;
|
pub const PATH: Path = Path::Bin;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn cli() -> Command {
|
pub fn cli() -> Command {
|
||||||
Command::new("echo")
|
Command::new("echo")
|
||||||
.about("Display a line of text")
|
.about("Display a line of text")
|
||||||
@ -30,9 +31,9 @@ pub fn run() {
|
|||||||
let i = if n { idx + 1 } else { idx };
|
let i = if n { idx + 1 } else { idx };
|
||||||
for (index, arg) in args.iter().enumerate().skip(i) {
|
for (index, arg) in args.iter().enumerate().skip(i) {
|
||||||
if index < len - 1 {
|
if index < len - 1 {
|
||||||
print!("{} ", arg);
|
print!("{arg} ");
|
||||||
} else {
|
} else {
|
||||||
print!("{}", arg);
|
print!("{arg}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !n {
|
if !n {
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use clap::Command;
|
|
||||||
use crate::Path;
|
use crate::Path;
|
||||||
|
use clap::Command;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
pub const PATH: Path = Path::Bin;
|
pub const PATH: Path = Path::Bin;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn cli() -> Command {
|
pub fn cli() -> Command {
|
||||||
Command::new("false")
|
Command::new("false")
|
||||||
.about("Does nothing unsuccessfully")
|
.about("Does nothing unsuccessfully")
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
|
||||||
use crate::Path;
|
use crate::Path;
|
||||||
|
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{stdin, Read};
|
use std::io::{stdin, Read};
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
pub const PATH: Path = Path::Bin;
|
pub const PATH: Path = Path::Bin;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn cli() -> Command {
|
pub fn cli() -> Command {
|
||||||
Command::new("head")
|
Command::new("head")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
@ -51,7 +52,7 @@ fn head(file: &str, count: usize, header: bool, bytes: bool) {
|
|||||||
match stdin().read_to_string(&mut contents) {
|
match stdin().read_to_string(&mut contents) {
|
||||||
Ok(_) => true,
|
Ok(_) => true,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("head: {}", e);
|
eprintln!("head: {e}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -60,18 +61,18 @@ fn head(file: &str, count: usize, header: bool, bytes: bool) {
|
|||||||
contents = match buf {
|
contents = match buf {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("head: {}", e);
|
eprintln!("head: {e}");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if header {
|
if header {
|
||||||
println!("==> {} <==", file);
|
println!("==> {file} <==");
|
||||||
}
|
}
|
||||||
if bytes {
|
if bytes {
|
||||||
for (index, char) in contents.chars().into_iter().enumerate() {
|
for (index, char) in contents.chars().into_iter().enumerate() {
|
||||||
if index < count {
|
if index < count {
|
||||||
print!("{}", char);
|
print!("{char}");
|
||||||
} else {
|
} else {
|
||||||
println!();
|
println!();
|
||||||
return;
|
return;
|
||||||
@ -81,7 +82,7 @@ fn head(file: &str, count: usize, header: bool, bytes: bool) {
|
|||||||
} else {
|
} else {
|
||||||
for (index, line) in contents.lines().into_iter().enumerate() {
|
for (index, line) in contents.lines().into_iter().enumerate() {
|
||||||
if index < count {
|
if index < count {
|
||||||
println!("{}", line);
|
println!("{line}");
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use clap::{Arg, Command, ArgMatches, ArgAction};
|
|
||||||
use crate::Path;
|
use crate::Path;
|
||||||
use std::{io, process};
|
use clap::{Arg, ArgAction, ArgMatches, Command};
|
||||||
|
use std::process;
|
||||||
|
|
||||||
pub const PATH: Path = Path::Bin;
|
pub const PATH: Path = Path::Bin;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn cli() -> Command {
|
pub fn cli() -> Command {
|
||||||
Command::new("hostname")
|
Command::new("hostname")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
@ -34,12 +35,11 @@ pub fn run(matches: &ArgMatches) {
|
|||||||
if matches.get_flag("STRIP") {
|
if matches.get_flag("STRIP") {
|
||||||
println!(
|
println!(
|
||||||
"{}",
|
"{}",
|
||||||
match hostname.to_string_lossy().split('.').next() {
|
if let Some(s) = hostname.to_string_lossy().split('.').next() {
|
||||||
Some(c) => c,
|
s
|
||||||
None => {
|
} else {
|
||||||
eprintln!("hostname: missing operand");
|
eprintln!("hostname: missing operand");
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -16,6 +16,6 @@ mod mv;
|
|||||||
mod pwd;
|
mod pwd;
|
||||||
mod rm;
|
mod rm;
|
||||||
mod rmdir;
|
mod rmdir;
|
||||||
mod sleep;
|
pub mod sleep;
|
||||||
mod sync;
|
mod sync;
|
||||||
pub mod r#true;
|
pub mod r#true;
|
||||||
|
@ -1 +1,36 @@
|
|||||||
|
use crate::Path;
|
||||||
|
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
|
||||||
|
use std::{env, thread, time::Duration};
|
||||||
|
|
||||||
|
pub const PATH: Path = Path::Bin;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn cli() -> Command {
|
||||||
|
Command::new("sleep")
|
||||||
|
.about("Suspend execution for an interval of time")
|
||||||
|
.long_about(
|
||||||
|
"The sleep utility suspends execution for a minimum of the specified number of seconds.\n\
|
||||||
|
This number must be positive and may contain a decimal fraction.\n\
|
||||||
|
sleep is commonly used to schedule the execution of other commands"
|
||||||
|
)
|
||||||
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
|
.author(env!("CARGO_PKG_AUTHORS"))
|
||||||
|
.arg(
|
||||||
|
Arg::new("seconds")
|
||||||
|
.help("The number of seconds to sleep")
|
||||||
|
.num_args(1)
|
||||||
|
.value_parser(value_parser!(f64))
|
||||||
|
.required(true)
|
||||||
|
.action(ArgAction::Set)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
|
||||||
|
pub fn run(matches: &ArgMatches) {
|
||||||
|
if let Some(raw) = matches.get_one::<f64>("seconds") {
|
||||||
|
let seconds = *raw as u64;
|
||||||
|
let nanos = ((raw % 1.0) * 10e-9) as u32;
|
||||||
|
let s = Duration::new(seconds, nanos);
|
||||||
|
thread::sleep(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use clap::Command;
|
|
||||||
use crate::Path;
|
use crate::Path;
|
||||||
|
use clap::Command;
|
||||||
use std::process;
|
use std::process;
|
||||||
|
|
||||||
pub const PATH: Path = Path::Bin;
|
pub const PATH: Path = Path::Bin;
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn cli() -> Command {
|
pub fn cli() -> Command {
|
||||||
Command::new("true")
|
Command::new("true")
|
||||||
.about("Does nothing successfully")
|
.about("Does nothing successfully")
|
||||||
|
@ -32,6 +32,7 @@ pub fn run() {
|
|||||||
"hostname" => cmd::hostname::run(&cmd::hostname::cli().get_matches()),
|
"hostname" => cmd::hostname::run(&cmd::hostname::cli().get_matches()),
|
||||||
"true" => cmd::r#true::run(),
|
"true" => cmd::r#true::run(),
|
||||||
"shitbox" => cli::run(),
|
"shitbox" => cli::run(),
|
||||||
|
"sleep" => cmd::sleep::run(&cmd::sleep::cli().get_matches()),
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user