@@ -16,6 +16,11 @@ use terminal_size::terminal_size;
16
16
17
17
use hexyl:: { Base , BorderStyle , CharacterTable , Endianness , Input , PrinterBuilder } ;
18
18
19
+ use hexyl:: {
20
+ COLOR_ASCII_OTHER , COLOR_ASCII_PRINTABLE , COLOR_ASCII_WHITESPACE , COLOR_NONASCII , COLOR_NULL ,
21
+ COLOR_RESET ,
22
+ } ;
23
+
19
24
#[ cfg( test) ]
20
25
mod tests;
21
26
@@ -179,6 +184,10 @@ struct Opt {
179
184
conflicts_with( "panels" )
180
185
) ]
181
186
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 ,
182
191
}
183
192
184
193
#[ derive( Clone , Debug , Default , ValueEnum ) ]
@@ -231,6 +240,10 @@ impl From<GroupSize> for u8 {
231
240
fn run ( ) -> Result < ( ) > {
232
241
let opt = Opt :: parse ( ) ;
233
242
243
+ if opt. print_color_table {
244
+ return print_color_table ( ) . map_err ( |e| anyhow ! ( e) ) ;
245
+ }
246
+
234
247
let stdin = io:: stdin ( ) ;
235
248
236
249
let mut reader = match opt. file {
@@ -416,10 +429,9 @@ fn run() -> Result<()> {
416
429
417
430
let character_table = opt. character_table ;
418
431
419
- let stdout = io:: stdout ( ) ;
420
- let mut stdout_lock = BufWriter :: new ( stdout. lock ( ) ) ;
432
+ let mut stdout = BufWriter :: new ( io:: stdout ( ) . lock ( ) ) ;
421
433
422
- let mut printer = PrinterBuilder :: new ( & mut stdout_lock )
434
+ let mut printer = PrinterBuilder :: new ( & mut stdout )
423
435
. show_color ( show_color)
424
436
. show_char_panel ( show_char_panel)
425
437
. show_position_panel ( show_position_panel)
@@ -475,6 +487,42 @@ impl From<NonNegativeI64> for u64 {
475
487
}
476
488
}
477
489
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
+
478
526
#[ derive( Clone , Copy , Debug , Default , Hash , Eq , Ord , PartialEq , PartialOrd ) ]
479
527
pub struct PositiveI64 ( i64 ) ;
480
528
0 commit comments