Skip to content

Commit 45a557e

Browse files
authored
Merge pull request #235 from sahinfalcon/print-colour-table
PR #229 • Add option to print color table
2 parents 6e0bff2 + 73cfd05 commit 45a557e

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
# unreleased
1+
# unreleased
2+
3+
## Features
4+
5+
* New `--print-color-table` option, see #229 (@sahinfalcon)
26

37
## Bugfixes
48

59
- Throw an error when try to view a directory, see #234 (@Integral-Tech)
610

11+
712
# v0.15.0
813

914
## Features

src/main.rs

+51-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ use terminal_size::terminal_size;
1616

1717
use hexyl::{Base, BorderStyle, CharacterTable, Endianness, Input, PrinterBuilder};
1818

19+
use hexyl::{
20+
COLOR_ASCII_OTHER, COLOR_ASCII_PRINTABLE, COLOR_ASCII_WHITESPACE, COLOR_NONASCII, COLOR_NULL,
21+
COLOR_RESET,
22+
};
23+
1924
#[cfg(test)]
2025
mod tests;
2126

@@ -179,6 +184,10 @@ struct Opt {
179184
conflicts_with("panels")
180185
)]
181186
terminal_width: Option<NonZeroU64>,
187+
188+
/// Print a table showing how different types of bytes are colored.
189+
#[arg(long)]
190+
print_color_table: bool,
182191
}
183192

184193
#[derive(Clone, Debug, Default, ValueEnum)]
@@ -231,6 +240,10 @@ impl From<GroupSize> for u8 {
231240
fn run() -> Result<()> {
232241
let opt = Opt::parse();
233242

243+
if opt.print_color_table {
244+
return print_color_table().map_err(|e| anyhow!(e));
245+
}
246+
234247
let stdin = io::stdin();
235248

236249
let mut reader = match opt.file {
@@ -416,10 +429,9 @@ fn run() -> Result<()> {
416429

417430
let character_table = opt.character_table;
418431

419-
let stdout = io::stdout();
420-
let mut stdout_lock = BufWriter::new(stdout.lock());
432+
let mut stdout = BufWriter::new(io::stdout().lock());
421433

422-
let mut printer = PrinterBuilder::new(&mut stdout_lock)
434+
let mut printer = PrinterBuilder::new(&mut stdout)
423435
.show_color(show_color)
424436
.show_char_panel(show_char_panel)
425437
.show_position_panel(show_position_panel)
@@ -475,6 +487,42 @@ impl From<NonNegativeI64> for u64 {
475487
}
476488
}
477489

490+
fn print_color_table() -> io::Result<()> {
491+
let mut stdout = BufWriter::new(io::stdout().lock());
492+
493+
writeln!(stdout, "hexyl color reference:\n")?;
494+
495+
// NULL bytes
496+
stdout.write_all(COLOR_NULL)?;
497+
writeln!(stdout, "⋄ NULL bytes (0x00)")?;
498+
stdout.write_all(COLOR_RESET)?;
499+
500+
// ASCII printable
501+
stdout.write_all(COLOR_ASCII_PRINTABLE)?;
502+
writeln!(stdout, "a ASCII printable characters (0x20 - 0x7E)")?;
503+
stdout.write_all(COLOR_RESET)?;
504+
505+
// ASCII whitespace
506+
stdout.write_all(COLOR_ASCII_WHITESPACE)?;
507+
writeln!(stdout, "_ ASCII whitespace (0x09 - 0x0D, 0x20)")?;
508+
stdout.write_all(COLOR_RESET)?;
509+
510+
// ASCII other
511+
stdout.write_all(COLOR_ASCII_OTHER)?;
512+
writeln!(
513+
stdout,
514+
"• ASCII control characters (except NULL and whitespace)"
515+
)?;
516+
stdout.write_all(COLOR_RESET)?;
517+
518+
// Non-ASCII
519+
stdout.write_all(COLOR_NONASCII)?;
520+
writeln!(stdout, "× Non-ASCII bytes (0x80 - 0xFF)")?;
521+
stdout.write_all(COLOR_RESET)?;
522+
523+
Ok(())
524+
}
525+
478526
#[derive(Clone, Copy, Debug, Default, Hash, Eq, Ord, PartialEq, PartialOrd)]
479527
pub struct PositiveI64(i64);
480528

0 commit comments

Comments
 (0)