Fully-fledged HEX hexdump clone

This commit is contained in:
sharkdp committed 2018-11-05 22:00:11 +01:00
1 parent 6d6ee6fb24
commit 1443d2d7bb
1 file changed
+23 -2
+23 -2
View File
@@ -14,6 +14,10 @@ struct Printer<'a> {
stdout: StdoutLock<'a>,
}
fn byte_is_printable(b: u8) -> bool {
b.is_ascii_alphanumeric() || b.is_ascii_punctuation() || b.is_ascii_graphic()
}
impl<'a> Printer<'a> {
fn new(stdout: StdoutLock) -> Printer {
Printer {
@@ -24,6 +28,10 @@ impl<'a> Printer<'a> {
}
fn print_byte(&mut self, b: u8) -> io::Result<()> {
if self.idx % 16 == 1 {
write!(self.stdout, " ");
}
write!(self.stdout, "{:02x} ", b)?;
self.line.push(b);
@@ -41,8 +49,21 @@ impl<'a> Printer<'a> {
}
fn print_textline(&mut self) -> io::Result<()> {
write!(self.stdout, "")?;
writeln!(self.stdout);
let fill_spaces = match self.line.len() {
n if n < 8 => 1 + 3 * (16 - n),
n => 3 * (16 - n),
};
write!(self.stdout, "{} │", " ".repeat(fill_spaces))?;
for b in self.line.iter().cloned() {
let chr = if byte_is_printable(b) { b as char } else { '.' };
write!(self.stdout, "{}", chr).ok();
}
writeln!(self.stdout, "│");
self.line.clear();
Ok(())
}