1
5
mirror of https://github.com/vinc/moros.git synced 2024-06-15 13:46:41 +00:00
moros/src/usr/hex.rs
Vincent Ollivier e4ce2ab59c
Use exit code to set status var (#360)
* Use exit code

* Add macro_use to sys module

* Replace ExitCode with Result<usize, usize>

* Add status var

* Fix status code

* Replace status with ?

* Fix regex for var substitution

* Remove warnings

* Add temporary fix for failing tests

* Add q shortcut to calc

* Use usize for exit code

* Use process exit codes

* Reintroduce ExitCode enum

* Use ExitCode::UsageError where needed

* Display usage error in find command

* Add doc
2022-06-29 19:23:01 +02:00

50 lines
1.4 KiB
Rust

use crate::api::fs;
use crate::api::console::Style;
use crate::api::process::ExitCode;
// TODO: add `--skip` and `--length` params
pub fn main(args: &[&str]) -> Result<(), ExitCode> {
if args.len() != 2 {
return Err(ExitCode::UsageError);
}
let pathname = args[1];
if let Ok(buf) = fs::read_to_bytes(pathname) { // TODO: read chunks
print_hex(&buf);
Ok(())
} else {
error!("File not found '{}'", pathname);
Err(ExitCode::Failure)
}
}
// TODO: move this to api::hex::print_hex
pub fn print_hex(buf: &[u8]) {
let n = buf.len() / 2;
for i in 0..n {
print!("{}", Style::color("LightCyan"));
if i % 8 == 0 {
print!("{:08X}: ", i * 2);
}
print!("{}", Style::color("Pink"));
print!("{:02X}{:02X} ", buf[i * 2], buf[i * 2 + 1]);
print!("{}", Style::reset());
if i % 8 == 7 || i == n - 1 {
for _ in 0..(7 - (i % 8)) {
print!(" ");
}
let m = ((i % 8) + 1) * 2;
for j in 0..m {
let c = buf[(i * 2 + 1) - (m - 1) + j] as char;
if c.is_ascii_graphic() {
print!("{}", c);
} else if c.is_ascii_whitespace() {
print!(" ");
} else {
print!(".");
}
}
println!();
}
}
}