From 568f38ae7733ac3580fa6d300ac6c51452d7c8e9 Mon Sep 17 00:00:00 2001 From: Taylan Sahin Date: Fri, 13 Dec 2024 00:11:15 +0000 Subject: [PATCH 1/3] =?UTF-8?q?PR=20#229=20=E2=80=A2=20Add=20option=20to?= =?UTF-8?q?=20print=20color=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 7 ++++++- src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bf2ea9..892586d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,14 @@ -# unreleased +# unreleased + +## Features + +* New `--print-color-table` option, see #229 (@sahinfalcon) ## Bugfixes - Throw an error when try to view a directory, see #234 (@Integral-Tech) + # v0.15.0 ## Features diff --git a/src/main.rs b/src/main.rs index 4c257bb..431e05a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,8 @@ use terminal_size::terminal_size; use hexyl::{Base, BorderStyle, CharacterTable, Endianness, Input, PrinterBuilder}; +use hexyl::{COLOR_NULL, COLOR_RESET, COLOR_ASCII_PRINTABLE, COLOR_ASCII_WHITESPACE, COLOR_ASCII_OTHER, COLOR_NONASCII}; + #[cfg(test)] mod tests; @@ -179,6 +181,10 @@ struct Opt { conflicts_with("panels") )] terminal_width: Option, + + /// Print a table showing how different types of bytes are colored. + #[arg(short('t'), long)] + print_color_table: bool, } #[derive(Clone, Debug, Default, ValueEnum)] @@ -231,6 +237,10 @@ impl From for u8 { fn run() -> Result<()> { let opt = Opt::parse(); + if opt.print_color_table { + return print_color_table().map_err(|e| anyhow!(e)); + } + let stdin = io::stdin(); let mut reader = match opt.file { @@ -475,6 +485,40 @@ impl From for u64 { } } +fn print_color_table() -> io::Result<()> { + let stdout = io::stdout(); + let mut stdout_lock = BufWriter::new(stdout.lock()); + + writeln!(stdout_lock, "hexyl color reference:\n")?; + + // NULL bytes + stdout_lock.write_all(COLOR_NULL)?; + writeln!(stdout_lock, "⋄ NULL bytes (0x00)")?; + stdout_lock.write_all(COLOR_RESET)?; + + // ASCII printable + stdout_lock.write_all(COLOR_ASCII_PRINTABLE)?; + writeln!(stdout_lock, "a ASCII printable characters (0x20 - 0x7E)")?; + stdout_lock.write_all(COLOR_RESET)?; + + // ASCII whitespace + stdout_lock.write_all(COLOR_ASCII_WHITESPACE)?; + writeln!(stdout_lock, "_ ASCII whitespace (0x09 - 0x0D, 0x20)")?; + stdout_lock.write_all(COLOR_RESET)?; + + // ASCII other + stdout_lock.write_all(COLOR_ASCII_OTHER)?; + writeln!(stdout_lock, "• ASCII control characters (except NULL and whitespace)")?; + stdout_lock.write_all(COLOR_RESET)?; + + // Non-ASCII + stdout_lock.write_all(COLOR_NONASCII)?; + writeln!(stdout_lock, "× Non-ASCII bytes (0x80 - 0xFF)")?; + stdout_lock.write_all(COLOR_RESET)?; + + Ok(()) +} + #[derive(Clone, Copy, Debug, Default, Hash, Eq, Ord, PartialEq, PartialOrd)] pub struct PositiveI64(i64); From 37e9ef6a64e3be37b079fd77057c580fa061a3ae Mon Sep 17 00:00:00 2001 From: Taylan Sahin Date: Fri, 13 Dec 2024 00:38:14 +0000 Subject: [PATCH 2/3] cargo fmt --- src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 431e05a..1b18d7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,10 @@ use terminal_size::terminal_size; use hexyl::{Base, BorderStyle, CharacterTable, Endianness, Input, PrinterBuilder}; -use hexyl::{COLOR_NULL, COLOR_RESET, COLOR_ASCII_PRINTABLE, COLOR_ASCII_WHITESPACE, COLOR_ASCII_OTHER, COLOR_NONASCII}; +use hexyl::{ + COLOR_ASCII_OTHER, COLOR_ASCII_PRINTABLE, COLOR_ASCII_WHITESPACE, COLOR_NONASCII, COLOR_NULL, + COLOR_RESET, +}; #[cfg(test)] mod tests; @@ -508,7 +511,10 @@ fn print_color_table() -> io::Result<()> { // ASCII other stdout_lock.write_all(COLOR_ASCII_OTHER)?; - writeln!(stdout_lock, "• ASCII control characters (except NULL and whitespace)")?; + writeln!( + stdout_lock, + "• ASCII control characters (except NULL and whitespace)" + )?; stdout_lock.write_all(COLOR_RESET)?; // Non-ASCII From 73cfd05a10dd0f9a853b69f02a18a90a1a8a33fd Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 27 Dec 2024 14:37:04 +0100 Subject: [PATCH 3/3] Minor updates --- src/main.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1b18d7d..2e32cff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -186,7 +186,7 @@ struct Opt { terminal_width: Option, /// Print a table showing how different types of bytes are colored. - #[arg(short('t'), long)] + #[arg(long)] print_color_table: bool, } @@ -429,10 +429,9 @@ fn run() -> Result<()> { let character_table = opt.character_table; - let stdout = io::stdout(); - let mut stdout_lock = BufWriter::new(stdout.lock()); + let mut stdout = BufWriter::new(io::stdout().lock()); - let mut printer = PrinterBuilder::new(&mut stdout_lock) + let mut printer = PrinterBuilder::new(&mut stdout) .show_color(show_color) .show_char_panel(show_char_panel) .show_position_panel(show_position_panel) @@ -489,38 +488,37 @@ impl From for u64 { } fn print_color_table() -> io::Result<()> { - let stdout = io::stdout(); - let mut stdout_lock = BufWriter::new(stdout.lock()); + let mut stdout = BufWriter::new(io::stdout().lock()); - writeln!(stdout_lock, "hexyl color reference:\n")?; + writeln!(stdout, "hexyl color reference:\n")?; // NULL bytes - stdout_lock.write_all(COLOR_NULL)?; - writeln!(stdout_lock, "⋄ NULL bytes (0x00)")?; - stdout_lock.write_all(COLOR_RESET)?; + stdout.write_all(COLOR_NULL)?; + writeln!(stdout, "⋄ NULL bytes (0x00)")?; + stdout.write_all(COLOR_RESET)?; // ASCII printable - stdout_lock.write_all(COLOR_ASCII_PRINTABLE)?; - writeln!(stdout_lock, "a ASCII printable characters (0x20 - 0x7E)")?; - stdout_lock.write_all(COLOR_RESET)?; + stdout.write_all(COLOR_ASCII_PRINTABLE)?; + writeln!(stdout, "a ASCII printable characters (0x20 - 0x7E)")?; + stdout.write_all(COLOR_RESET)?; // ASCII whitespace - stdout_lock.write_all(COLOR_ASCII_WHITESPACE)?; - writeln!(stdout_lock, "_ ASCII whitespace (0x09 - 0x0D, 0x20)")?; - stdout_lock.write_all(COLOR_RESET)?; + stdout.write_all(COLOR_ASCII_WHITESPACE)?; + writeln!(stdout, "_ ASCII whitespace (0x09 - 0x0D, 0x20)")?; + stdout.write_all(COLOR_RESET)?; // ASCII other - stdout_lock.write_all(COLOR_ASCII_OTHER)?; + stdout.write_all(COLOR_ASCII_OTHER)?; writeln!( - stdout_lock, + stdout, "• ASCII control characters (except NULL and whitespace)" )?; - stdout_lock.write_all(COLOR_RESET)?; + stdout.write_all(COLOR_RESET)?; // Non-ASCII - stdout_lock.write_all(COLOR_NONASCII)?; - writeln!(stdout_lock, "× Non-ASCII bytes (0x80 - 0xFF)")?; - stdout_lock.write_all(COLOR_RESET)?; + stdout.write_all(COLOR_NONASCII)?; + writeln!(stdout, "× Non-ASCII bytes (0x80 - 0xFF)")?; + stdout.write_all(COLOR_RESET)?; Ok(()) }