Skip to content

Commit

Permalink
Update hash command (#656)
Browse files Browse the repository at this point in the history
* Update hash command

* Add short option for color
  • Loading branch information
vinc committed Sep 15, 2024
1 parent 2f5bb90 commit 8421c49
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/usr/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use sha2::{Digest, Sha256};

#[derive(Copy, Clone)]
struct Config {
colorized: bool,
full_hash: bool,
color: bool,
short: bool,
recursive: bool,
}

Expand All @@ -21,28 +21,32 @@ pub fn main(args: &[&str]) -> Result<(), ExitCode> {
let n = args.len();
let mut paths = Vec::new();
let mut conf = Config {
colorized: true,
full_hash: false,
color: true,
short: true,
recursive: false,
};
if api::io::is_redirected(1) {
conf.colorized = false;
conf.color = false;
conf.short = false;
}
while i < n {
match args[i] {
"-h" | "--help" => {
help();
return Ok(());
}
"-f" | "--full" => {
conf.full_hash = true;
"-c" | "--color" => {
conf.color = true;
}
"-s" | "--short" => {
conf.short = true;
}
"-l" | "--long" => {
conf.short = false;
}
"-r" | "--recursive" => {
conf.recursive = true;
}
"--color" => {
conf.colorized = true;
}
arg => {
if arg.starts_with('-') {
error!("Unknown option '{}'", arg);
Expand All @@ -69,7 +73,7 @@ pub fn main(args: &[&str]) -> Result<(), ExitCode> {
fn print_hash(path: &str, conf: Config) -> Result<(), ExitCode> {
let color = Style::color("fushia");
let reset = Style::reset();
let n = if conf.full_hash { 32 } else { 16 };
let n = if conf.short { 4 } else { 32 };
if let Some(info) = syscall::info(path) {
if info.is_file() {
if let Ok(bytes) = api::fs::read_to_bytes(path) {
Expand All @@ -79,7 +83,7 @@ fn print_hash(path: &str, conf: Config) -> Result<(), ExitCode> {
let hex = res.iter().map(|byte|
format!("{:02X}", byte)
).take(n).collect::<Vec<String>>().join("");
if conf.colorized {
if conf.color {
println!("{}{}{} {}", color, hex, reset, path);
} else {
println!("{} {}", hex, path);
Expand Down Expand Up @@ -124,7 +128,15 @@ fn help() {
println!();
println!("{}Options:{}", csi_title, csi_reset);
println!(
" {0}-f{1}, {0}--full{1} Show full hash",
" {0}-l{1}, {0}--long{1} Show full hash",
csi_option, csi_reset
);
println!(
" {0}-s{1}, {0}--short{1} Show abbreviated hash",
csi_option, csi_reset
);
println!(
" {0}-c{1}, {0}--color{1} Enable color mode",
csi_option, csi_reset
);
println!(
Expand Down

0 comments on commit 8421c49

Please sign in to comment.