Skip to content

Enable custom colors with environment variables (PoC) #248

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
repository = "https://github.com/sharkdp/hexyl"
version = "0.16.0"
edition = "2021"
rust-version = "1.74"
rust-version = "1.80"

[dependencies]
anyhow = "1.0"
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ scoop install hexyl
x env use hexyl
```

## Configuration

`hexyl` colors can be configured via environment variables. The variables used are as follows:

* `HEXYL_ASCII_PRINTABLE`: Any non-whitespace printable ASCII character
* `HEXYL_ASCII_WHITESPACE`: Whitespace such as space or newline (only visible in middle panel with byte values)
* `HEXYL_ASCII_OTHER`: Any other ASCII character (< `0x80`) besides null
* `HEXYL_NULL`: The null byte (`0x00`)
* `HEXYL_NONASCII`: Any non-ASCII byte (> `0x7F`)
* `HEXYL_OFFSET`: The lefthand file offset

The colors can be any of the 8 standard terminal colors: `black`, `blue`, `cyan`, `green`, `magenta`, `red`,
`yellow` and `white`. The "bright" variants are also supported (e.g., `bright blue`). Additionally, you can use
the RGB hex format, `#abcdef`. For example, `HEXYL_ASCII_PRINTABLE=blue HEXYL_ASCII_WHITESPACE="bright green" HEXYL_ASCII_OTHER="#ff7f99"`.

## License

Licensed under either of
Expand Down
15 changes: 15 additions & 0 deletions doc/hexyl.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ _FILE_
**-V**, **\--version**
: Prints version information.

# ENVIRONMENT VARIABLES

**hexyl** colors can be configured via environment variables. The variables used are as follows:

: - **HEXYL_ASCII_PRINTABLE**: Any non-whitespace printable ASCII character
- **HEXYL_ASCII_WHITESPACE**: Whitespace such as space or newline (only visible in middle panel with byte values)
- **HEXYL_ASCII_OTHER**: Any other ASCII character (< **0x80**) besides null
- **HEXYL_NULL**: The null byte (**0x00**)
- **HEXYL_NONASCII**: Any non-ASCII byte (> **0x7F**)
- **HEXYL_OFFSET**: The lefthand file offset

The colors can be any of the 8 standard terminal colors: **black**, **blue**, **cyan**, **green**, **magenta**, **red**,
**yellow** and **white**. The "bright" variants are also supported (e.g., **bright blue**). Additionally, you can use
the RGB hex format, **#abcdef**. For example, **HEXYL_ASCII_PRINTABLE=blue HEXYL_ASCII_WHITESPACE="bright green" HEXYL_ASCII_OTHER="#ff7f99"**.

# NOTES

Source repository:
Expand Down
43 changes: 35 additions & 8 deletions src/colors.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
use owo_colors::{colors, Color};
use owo_colors::{colors, AnsiColors, Color, DynColors, OwoColorize};
use std::str::FromStr;
use std::sync::LazyLock;

pub const COLOR_NULL: &[u8] = colors::BrightBlack::ANSI_FG.as_bytes();
pub const COLOR_OFFSET: &[u8] = colors::BrightBlack::ANSI_FG.as_bytes();
pub const COLOR_ASCII_PRINTABLE: &[u8] = colors::Cyan::ANSI_FG.as_bytes();
pub const COLOR_ASCII_WHITESPACE: &[u8] = colors::Green::ANSI_FG.as_bytes();
pub const COLOR_ASCII_OTHER: &[u8] = colors::Green::ANSI_FG.as_bytes();
pub const COLOR_NONASCII: &[u8] = colors::Yellow::ANSI_FG.as_bytes();
pub const COLOR_RESET: &[u8] = colors::Default::ANSI_FG.as_bytes();
pub static COLOR_NULL: LazyLock<String> =
LazyLock::new(|| init_color("NULL", AnsiColors::BrightBlack));
pub static COLOR_OFFSET: LazyLock<String> =
LazyLock::new(|| init_color("OFFSET", AnsiColors::BrightBlack));
pub static COLOR_ASCII_PRINTABLE: LazyLock<String> =
LazyLock::new(|| init_color("ASCII_PRINTABLE", AnsiColors::Cyan));
pub static COLOR_ASCII_WHITESPACE: LazyLock<String> =
LazyLock::new(|| init_color("ASCII_WHITESPACE", AnsiColors::Green));
pub static COLOR_ASCII_OTHER: LazyLock<String> =
LazyLock::new(|| init_color("ASCII_OTHER", AnsiColors::Green));
pub static COLOR_NONASCII: LazyLock<String> =
LazyLock::new(|| init_color("NONASCII", AnsiColors::Yellow));
pub const COLOR_RESET: &str = colors::Default::ANSI_FG;

fn init_color(name: &str, default_ansi: AnsiColors) -> String {
let default = DynColors::Ansi(default_ansi);
let env_var = format!("HEXYL_{}", name);
let color = match std::env::var(env_var).as_deref() {
Ok(color) => match DynColors::from_str(color) {
Ok(color) => color,
_ => default,
},
_ => default,
};
// owo_colors' API isn't designed to get the terminal codes directly for
// dynamic colors, so we use this hack to get them from the LHS of some text.
format!("{}", "|".color(color))
.split_once("|")
.unwrap()
.0
.to_owned()
}

#[rustfmt::skip]
pub const CP437: [char; 256] = [
Expand Down
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ impl Byte {
fn color(self) -> &'static [u8] {
use crate::ByteCategory::*;
match self.category() {
Null => COLOR_NULL,
AsciiPrintable => COLOR_ASCII_PRINTABLE,
AsciiWhitespace => COLOR_ASCII_WHITESPACE,
AsciiOther => COLOR_ASCII_OTHER,
NonAscii => COLOR_NONASCII,
Null => COLOR_NULL.as_bytes(),
AsciiPrintable => COLOR_ASCII_PRINTABLE.as_bytes(),
AsciiWhitespace => COLOR_ASCII_WHITESPACE.as_bytes(),
AsciiOther => COLOR_ASCII_OTHER.as_bytes(),
NonAscii => COLOR_NONASCII.as_bytes(),
}
}

Expand Down Expand Up @@ -440,14 +440,14 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
.as_bytes(),
)?;
if self.show_color {
self.writer.write_all(COLOR_OFFSET)?;
self.writer.write_all(COLOR_OFFSET.as_bytes())?;
}
if self.show_position_panel {
match self.squeezer {
Squeezer::Print => {
self.writer.write_all(b"*")?;
if self.show_color {
self.writer.write_all(COLOR_RESET)?;
self.writer.write_all(COLOR_RESET.as_bytes())?;
}
self.writer.write_all(b" ")?;
}
Expand All @@ -462,7 +462,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
.write_all(self.byte_hex_panel_g[byte as usize].as_bytes())?;
}
if self.show_color {
self.writer.write_all(COLOR_RESET)?;
self.writer.write_all(COLOR_RESET.as_bytes())?;
}
}
}
Expand Down Expand Up @@ -494,7 +494,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
}
if i == 8 * self.panels - 1 {
if self.show_color {
self.writer.write_all(COLOR_RESET)?;
self.writer.write_all(COLOR_RESET.as_bytes())?;
self.curr_color = None;
}
self.writer.write_all(
Expand All @@ -505,7 +505,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
)?;
} else if i % 8 == 7 {
if self.show_color {
self.writer.write_all(COLOR_RESET)?;
self.writer.write_all(COLOR_RESET.as_bytes())?;
self.curr_color = None;
}
self.writer.write_all(
Expand All @@ -531,12 +531,12 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
Squeezer::Print => {
if !self.show_position_panel && i == 0 {
if self.show_color {
self.writer.write_all(COLOR_OFFSET)?;
self.writer.write_all(COLOR_OFFSET.as_bytes())?;
}
self.writer
.write_all(self.byte_char_panel[b'*' as usize].as_bytes())?;
if self.show_color {
self.writer.write_all(COLOR_RESET)?;
self.writer.write_all(COLOR_RESET.as_bytes())?;
}
} else if i % (self.group_size as usize) == 0 {
self.writer.write_all(b" ")?;
Expand All @@ -562,7 +562,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
if i % 8 == 7 {
if self.show_color {
self.curr_color = None;
self.writer.write_all(COLOR_RESET)?;
self.writer.write_all(COLOR_RESET.as_bytes())?;
}
self.writer.write_all(b" ")?;
// byte is last in last panel
Expand Down
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,32 +493,32 @@ fn print_color_table() -> io::Result<()> {
writeln!(stdout, "hexyl color reference:\n")?;

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

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

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

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

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

Ok(())
}
Expand Down
Loading
Loading