Added factor applet

This commit is contained in:
Nathan Fisher 2023-01-03 12:49:09 -05:00
parent 2bce4afe7a
commit 829dad2c5f
5 changed files with 94 additions and 4 deletions

View File

@ -25,7 +25,7 @@ impl Cmd for Base32 {
fn cli(&self) -> clap::Command {
Command::new("base32")
.author("The JeanG3nie <jeang3nie@hitchhiker-linux.org>")
.author("Nathan Fisher")
.about("Base32 encode/decode data and print to standard output")
.args([
Arg::new("INPUT")

86
src/cmd/factor/mod.rs Normal file
View File

@ -0,0 +1,86 @@
use clap::{Arg, ArgMatches, Command, value_parser};
use crate::Cmd;
use std::{error::Error, io::{self, BufRead}};
#[derive(Debug)]
pub struct Factor {
name: &'static str,
path: Option<crate::Path>,
}
pub const FACTOR: Factor = Factor {
name: "factor",
path: Some(crate::Path::UsrBin),
};
impl Cmd for Factor {
fn name(&self) -> &str {
self.name
}
fn cli(&self) -> Command {
Command::new("factor")
.about("factor numbers")
.after_help(
"Print the prime factors of each specified integer NUMBER. If none are\n\
specified on the command line, read them from standard input."
)
.arg(
Arg::new("number")
.help("the numbers to factor")
.num_args(0..)
.value_parser(value_parser!(u64))
)
}
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> {
if let Some(matches) = matches {
match matches.get_many::<u64>("number") {
Some(numbers) => {
numbers.for_each(|n| print_factors(*n));
},
None => {
for line in io::stdin().lock().lines() {
for value in line.unwrap().split_whitespace() {
let num: u64 = value.parse()?;
print_factors(num);
}
}
},
}
}
Ok(())
}
fn path(&self) -> Option<crate::Path> {
self.path
}
}
fn is_prime(num: u64) -> bool {
match num {
0 | 1 => false,
2 | 3 | 5 | 7 | 11 | 13 => true,
x if x % 2 == 0 => false,
_ => {
let mut x: u64 = 2;
while x < num / 2 {
if num % x == 0 {
return false;
}
x += 1;
}
true
}
}
}
fn print_factors(num: u64) {
print!("{num}:");
for n in 2..=num / 2 {
if num % n == 0 && is_prime(n) {
print!(" {n}");
}
}
println!();
}

View File

@ -18,6 +18,7 @@ mod date;
mod dd;
pub mod dirname;
pub mod echo;
pub mod factor;
pub mod r#false;
mod getty;
pub mod head;
@ -41,6 +42,7 @@ pub use {
bootstrap::{Bootstrap, BOOTSTRAP},
dirname::{Dirname, DIRNAME},
echo::{Echo, ECHO},
factor::{Factor, FACTOR},
head::{Head, HEAD},
nologin::{Nologin, NOLOGIN},
r#false::{False, FALSE},

View File

@ -1,4 +1,4 @@
use super::{Cmd, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, HEAD, HOSTNAME, NOLOGIN, SLEEP, TRUE};
use super::{Cmd, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FACTOR, FALSE, HEAD, HOSTNAME, NOLOGIN, SLEEP, TRUE};
use clap::Command;
use std::{
error::Error,
@ -35,6 +35,7 @@ impl Cmd for Shitbox {
DIRNAME.cli(),
ECHO.cli(),
FALSE.cli(),
FACTOR.cli(),
HEAD.cli(),
NOLOGIN.cli(),
HOSTNAME.cli(),
@ -54,6 +55,7 @@ impl Cmd for Shitbox {
Some(("bootstrap", matches)) => BOOTSTRAP.run(Some(matches))?,
Some(("dirname", matches)) => DIRNAME.run(Some(matches))?,
Some(("echo", _matches)) => ECHO.run(None)?,
Some(("factor", matches)) => FACTOR.run(Some(matches))?,
Some(("false", _matches)) => FALSE.run(None)?,
Some(("head", matches)) => HEAD.run(Some(matches))?,
Some(("nologin", _matches)) => NOLOGIN.run(None)?,

View File

@ -3,7 +3,7 @@ use std::{env, error::Error, path::PathBuf, string::ToString};
pub mod cmd;
use cmd::{
Cmd, Commands, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, HEAD, HOSTNAME, NOLOGIN, SHITBOX, SLEEP, TRUE,
Cmd, Commands, BASE_32, BOOTSTRAP, DIRNAME, ECHO, FALSE, FACTOR, HEAD, HOSTNAME, NOLOGIN, SHITBOX, SLEEP, TRUE,
};
#[derive(Debug, Clone, Copy)]
@ -39,7 +39,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
cmd::COMMANDS
.set(Commands {
items: vec![
&BASE_32, &BOOTSTRAP, &DIRNAME, &ECHO, &FALSE, &HEAD, &HOSTNAME, &NOLOGIN, &TRUE, &SLEEP,
&BASE_32, &BOOTSTRAP, &DIRNAME, &ECHO, &FALSE, &FACTOR, &HEAD, &HOSTNAME, &NOLOGIN, &TRUE, &SLEEP,
&SHITBOX,
],
})