Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR #229 • Add option to print color table #235

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
54 changes: 51 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ use terminal_size::terminal_size;

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

use hexyl::{
COLOR_ASCII_OTHER, COLOR_ASCII_PRINTABLE, COLOR_ASCII_WHITESPACE, COLOR_NONASCII, COLOR_NULL,
COLOR_RESET,
};

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -179,6 +184,10 @@ struct Opt {
conflicts_with("panels")
)]
terminal_width: Option<NonZeroU64>,

/// Print a table showing how different types of bytes are colored.
#[arg(long)]
print_color_table: bool,
}

#[derive(Clone, Debug, Default, ValueEnum)]
Expand Down Expand Up @@ -231,6 +240,10 @@ impl From<GroupSize> 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 {
Expand Down Expand Up @@ -416,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)
Expand Down Expand Up @@ -475,6 +487,42 @@ impl From<NonNegativeI64> for u64 {
}
}

fn print_color_table() -> io::Result<()> {
let mut stdout = BufWriter::new(io::stdout().lock());

writeln!(stdout, "hexyl color reference:\n")?;

// NULL bytes
stdout.write_all(COLOR_NULL)?;
writeln!(stdout, "⋄ NULL bytes (0x00)")?;
stdout.write_all(COLOR_RESET)?;

// ASCII printable
stdout.write_all(COLOR_ASCII_PRINTABLE)?;
writeln!(stdout, "a ASCII printable characters (0x20 - 0x7E)")?;
stdout.write_all(COLOR_RESET)?;

// ASCII whitespace
stdout.write_all(COLOR_ASCII_WHITESPACE)?;
writeln!(stdout, "_ ASCII whitespace (0x09 - 0x0D, 0x20)")?;
stdout.write_all(COLOR_RESET)?;

// ASCII other
stdout.write_all(COLOR_ASCII_OTHER)?;
writeln!(
stdout,
"• ASCII control characters (except NULL and whitespace)"
)?;
stdout.write_all(COLOR_RESET)?;

// Non-ASCII
stdout.write_all(COLOR_NONASCII)?;
writeln!(stdout, "× Non-ASCII bytes (0x80 - 0xFF)")?;
stdout.write_all(COLOR_RESET)?;

Ok(())
}

#[derive(Clone, Copy, Debug, Default, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct PositiveI64(i64);

Expand Down
Loading