Merge pull request #183 from sharifhsn/owo

Use `owo-colors` instead of `anstyle`
This commit is contained in:
David Peter authored and GitHub committed 2022-12-07 23:42:54 +01:00
commit 4833c2afe4
4 files changed
+91 -92

No files matched your search

Generated
+47 -11
View File
@@ -11,12 +11,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "anstyle"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58bb0ec944fe041c41e32dcd60b49327fcb7ff761433838878cc97ddecf16db7"
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.66" version = "1.0.66"
@@ -37,6 +31,17 @@ dependencies = [
"wait-timeout", "wait-timeout",
] ]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@@ -175,6 +180,15 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@@ -188,15 +202,15 @@ dependencies = [
name = "hexyl" name = "hexyl"
version = "0.11.0" version = "0.11.0"
dependencies = [ dependencies = [
"anstyle",
"anyhow", "anyhow",
"assert_cmd", "assert_cmd",
"clap", "clap",
"const_format", "const_format",
"is-terminal",
"libc", "libc",
"owo-colors",
"predicates", "predicates",
"pretty_assertions", "pretty_assertions",
"supports-color",
"terminal_size", "terminal_size",
"thiserror", "thiserror",
] ]
@@ -217,12 +231,18 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330" checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi 0.2.6",
"io-lifetimes", "io-lifetimes",
"rustix", "rustix",
"windows-sys", "windows-sys",
] ]
[[package]]
name = "is_ci"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.5" version = "0.10.5"
@@ -286,6 +306,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]] [[package]]
name = "predicates" name = "predicates"
version = "2.1.4" version = "2.1.4"
@@ -371,9 +397,9 @@ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.36.4" version = "0.36.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb93e85278e08bb5788653183213d3a60fc242b10cb9be96586f5a73dcb67c23" checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@@ -395,6 +421,16 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "supports-color"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ba6faf2ca7ee42fdd458f4347ae0a9bd6bcc445ad7cb57ad82b383f18870d6f"
dependencies = [
"atty",
"is_ci",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.105" version = "1.0.105"
+2 -2
View File
@@ -11,11 +11,11 @@ version = "0.11.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
anstyle = "0.2"
anyhow = "1.0" anyhow = "1.0"
const_format = "0.2" const_format = "0.2"
is-terminal = "0.4"
libc = "0.2" libc = "0.2"
owo-colors = "3"
supports-color = "1"
thiserror = "1.0" thiserror = "1.0"
terminal_size = "0.2" terminal_size = "0.2"
+5 -5
View File
@@ -9,8 +9,6 @@ use std::num::{NonZeroI64, NonZeroU64, NonZeroU8};
use clap::builder::ArgPredicate; use clap::builder::ArgPredicate;
use clap::{crate_name, crate_version, Arg, ArgAction, ColorChoice, Command}; use clap::{crate_name, crate_version, Arg, ArgAction, ColorChoice, Command};
use is_terminal::IsTerminal;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use const_format::formatcp; use const_format::formatcp;
@@ -296,8 +294,10 @@ fn run() -> Result<()> {
let show_color = match matches.get_one::<String>("color").map(String::as_ref) { let show_color = match matches.get_one::<String>("color").map(String::as_ref) {
Some("never") => false, Some("never") => false,
Some("auto") => std::io::stdout().is_terminal(), Some("always") => true,
_ => true, _ => supports_color::on(supports_color::Stream::Stdout)
.map(|level| level.has_basic)
.unwrap_or(false),
}; };
let border_style = match matches.get_one::<String>("border").map(String::as_ref) { let border_style = match matches.get_one::<String>("border").map(String::as_ref) {
@@ -453,7 +453,7 @@ fn main() {
std::process::exit(0); std::process::exit(0);
} }
} }
eprintln!("Error: {:?}", err); eprintln!("Error: {err:?}");
std::process::exit(1); std::process::exit(1);
} }
} }
+37 -74
View File
@@ -4,7 +4,7 @@ pub use input::*;
use std::io::{self, BufReader, Read, Write}; use std::io::{self, BufReader, Read, Write};
use anstyle::{AnsiColor, Reset}; use owo_colors::{colors, Color};
pub enum Base { pub enum Base {
Binary, Binary,
@@ -13,24 +13,13 @@ pub enum Base {
Hexadecimal, Hexadecimal,
} }
#[derive(PartialEq, Eq)] const COLOR_NULL: &[u8] = colors::BrightBlack::ANSI_FG.as_bytes();
#[repr(usize)] const COLOR_OFFSET: &[u8] = colors::BrightBlack::ANSI_FG.as_bytes();
enum ByteColor { const COLOR_ASCII_PRINTABLE: &[u8] = colors::Cyan::ANSI_FG.as_bytes();
Null = 0, const COLOR_ASCII_WHITESPACE: &[u8] = colors::Green::ANSI_FG.as_bytes();
Offset = 1, const COLOR_ASCII_OTHER: &[u8] = colors::Green::ANSI_FG.as_bytes();
AsciiPrintable = 2, const COLOR_NONASCII: &[u8] = colors::Yellow::ANSI_FG.as_bytes();
AsciiWhitespace = 3, const COLOR_RESET: &[u8] = colors::Default::ANSI_FG.as_bytes();
AsciiOther = 4,
NonAscii = 5,
Reset = 6,
}
const COLOR_NULL: AnsiColor = AnsiColor::BrightBlack;
const COLOR_OFFSET: AnsiColor = AnsiColor::BrightBlack;
const COLOR_ASCII_PRINTABLE: AnsiColor = AnsiColor::Cyan;
const COLOR_ASCII_WHITESPACE: AnsiColor = AnsiColor::Green;
const COLOR_ASCII_OTHER: AnsiColor = AnsiColor::Magenta;
const COLOR_NONASCII: AnsiColor = AnsiColor::Yellow;
pub enum ByteCategory { pub enum ByteCategory {
Null, Null,
@@ -66,15 +55,15 @@ impl Byte {
} }
} }
fn color(self) -> ByteColor { fn color(self) -> &'static [u8] {
use crate::ByteCategory::*; use crate::ByteCategory::*;
match self.category() { match self.category() {
Null => ByteColor::Null, Null => COLOR_NULL,
AsciiPrintable => ByteColor::AsciiPrintable, AsciiPrintable => COLOR_ASCII_PRINTABLE,
AsciiWhitespace => ByteColor::AsciiWhitespace, AsciiWhitespace => COLOR_ASCII_WHITESPACE,
AsciiOther => ByteColor::AsciiOther, AsciiOther => COLOR_ASCII_OTHER,
NonAscii => ByteColor::NonAscii, NonAscii => COLOR_NONASCII,
} }
} }
@@ -250,14 +239,12 @@ pub struct Printer<'a, Writer: Write> {
show_char_panel: bool, show_char_panel: bool,
show_position_panel: bool, show_position_panel: bool,
show_color: bool, show_color: bool,
colors: Vec<String>, curr_color: Option<&'static [u8]>,
curr_color: Option<ByteColor>,
border_style: BorderStyle, border_style: BorderStyle,
byte_hex_panel: Vec<String>, byte_hex_panel: Vec<String>,
byte_char_panel: Vec<String>, byte_char_panel: Vec<String>,
// same as previous but in Fixed(242) gray color, for position panel // same as previous but in Fixed(242) gray color, for position panel
byte_hex_panel_g: Vec<String>, byte_hex_panel_g: Vec<String>,
byte_char_panel_g: Vec<String>,
squeezer: Squeezer, squeezer: Squeezer,
display_offset: u64, display_offset: u64,
/// The number of panels to draw. /// The number of panels to draw.
@@ -289,15 +276,6 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
show_position_panel, show_position_panel,
show_color, show_color,
curr_color: None, curr_color: None,
colors: vec![
COLOR_NULL.render_fg().to_string(),
COLOR_OFFSET.render_fg().to_string(),
COLOR_ASCII_PRINTABLE.render_fg().to_string(),
COLOR_ASCII_WHITESPACE.render_fg().to_string(),
COLOR_ASCII_OTHER.render_fg().to_string(),
COLOR_NONASCII.render_fg().to_string(),
Reset.render().to_string(),
],
border_style, border_style,
byte_hex_panel: (0u8..=u8::MAX) byte_hex_panel: (0u8..=u8::MAX)
.map(|i| match base { .map(|i| match base {
@@ -311,9 +289,6 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
.map(|i| format!("{}", Byte(i).as_char())) .map(|i| format!("{}", Byte(i).as_char()))
.collect(), .collect(),
byte_hex_panel_g: (0u8..=u8::MAX).map(|i| format!("{i:02x}")).collect(), byte_hex_panel_g: (0u8..=u8::MAX).map(|i| format!("{i:02x}")).collect(),
byte_char_panel_g: (0u8..=u8::MAX)
.map(|i| format!("{}", Byte(i).as_char()))
.collect(),
squeezer: if use_squeeze { squeezer: if use_squeeze {
Squeezer::Ignore Squeezer::Ignore
} else { } else {
@@ -354,27 +329,27 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
let h_repeat = h.to_string().repeat(self.panel_sz()); let h_repeat = h.to_string().repeat(self.panel_sz());
if self.show_position_panel { if self.show_position_panel {
write!(self.writer, "{l}{h8}{c}", l = l, c = c, h8 = h8).ok(); write!(self.writer, "{l}{h8}{c}")?;
} else { } else {
write!(self.writer, "{}", l).ok(); write!(self.writer, "{l}")?;
} }
for _ in 0..self.panels - 1 { for _ in 0..self.panels - 1 {
write!(self.writer, "{h_repeat}{c}", h_repeat = h_repeat, c = c).ok(); write!(self.writer, "{h_repeat}{c}")?;
} }
if self.show_char_panel { if self.show_char_panel {
write!(self.writer, "{h_repeat}{c}", h_repeat = h_repeat, c = c).ok(); write!(self.writer, "{h_repeat}{c}")?;
} else { } else {
write!(self.writer, "{h_repeat}", h_repeat = h_repeat).ok(); write!(self.writer, "{h_repeat}")?;
} }
if self.show_char_panel { if self.show_char_panel {
for _ in 0..self.panels - 1 { for _ in 0..self.panels - 1 {
write!(self.writer, "{h8}{c}", h8 = h8, c = c).ok(); write!(self.writer, "{h8}{c}")?;
} }
writeln!(self.writer, "{h8}{r}", h8 = h8, r = r).ok(); writeln!(self.writer, "{h8}{r}")?;
} else { } else {
writeln!(self.writer, "{r}", r = r).ok(); writeln!(self.writer, "{r}")?;
} }
Ok(()) Ok(())
@@ -402,17 +377,15 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
.as_bytes(), .as_bytes(),
)?; )?;
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_OFFSET)?;
.write_all(self.colors[ByteColor::Offset as usize].as_bytes())?;
} }
if self.show_position_panel { if self.show_position_panel {
match self.squeezer { match self.squeezer {
Squeezer::Print => { Squeezer::Print => {
self.writer self.writer
.write_all(self.byte_char_panel_g[b'*' as usize].as_bytes())?; .write_all(self.byte_char_panel[b'*' as usize].as_bytes())?;
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_RESET)?;
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
} }
self.writer.write_all(b" ")?; self.writer.write_all(b" ")?;
} }
@@ -427,8 +400,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
.write_all(self.byte_hex_panel_g[byte as usize].as_bytes())?; .write_all(self.byte_hex_panel_g[byte as usize].as_bytes())?;
} }
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_RESET)?;
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
} }
} }
} }
@@ -448,8 +420,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
Squeezer::Ignore | Squeezer::Disabled => { Squeezer::Ignore | Squeezer::Disabled => {
if let Some(&b) = self.line_buf.get(i as usize) { if let Some(&b) = self.line_buf.get(i as usize) {
if self.show_color && self.curr_color != Some(Byte(b).color()) { if self.show_color && self.curr_color != Some(Byte(b).color()) {
self.writer self.writer.write_all(Byte(b).color())?;
.write_all(self.colors[Byte(b).color() as usize].as_bytes())?;
self.curr_color = Some(Byte(b).color()); self.curr_color = Some(Byte(b).color());
} }
self.writer self.writer
@@ -461,8 +432,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
} }
if i == 8 * self.panels - 1 { if i == 8 * self.panels - 1 {
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_RESET)?;
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
self.curr_color = None; self.curr_color = None;
} }
self.writer.write_all( self.writer.write_all(
@@ -473,8 +443,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
)?; )?;
} else if i % 8 == 7 { } else if i % 8 == 7 {
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_RESET)?;
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
self.curr_color = None; self.curr_color = None;
} }
self.writer.write_all( self.writer.write_all(
@@ -500,19 +469,15 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
Squeezer::Print => { Squeezer::Print => {
if !self.show_position_panel && i == 0 { if !self.show_position_panel && i == 0 {
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_OFFSET)?;
.write_all(self.colors[ByteColor::Offset as usize].as_bytes())?;
} }
self.writer self.writer
.write_all(self.byte_char_panel_g[b'*' as usize].as_bytes())?; .write_all(self.byte_char_panel[b'*' as usize].as_bytes())?;
if self.show_color { if self.show_color {
self.writer self.writer.write_all(COLOR_RESET)?;
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
}
} else {
if i % (self.group_size as usize) == 0 {
self.writer.write_all(b" ")?;
} }
} else if i % (self.group_size as usize) == 0 {
self.writer.write_all(b" ")?;
} }
for _ in 0..self.base_digits { for _ in 0..self.base_digits {
self.writer.write_all(b" ")?; self.writer.write_all(b" ")?;
@@ -524,8 +489,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
self.writer.write_all(b" ")?; self.writer.write_all(b" ")?;
} }
if self.show_color && self.curr_color != Some(Byte(b).color()) { if self.show_color && self.curr_color != Some(Byte(b).color()) {
self.writer self.writer.write_all(Byte(b).color())?;
.write_all(self.colors[Byte(b).color() as usize].as_bytes())?;
self.curr_color = Some(Byte(b).color()); self.curr_color = Some(Byte(b).color());
} }
self.writer self.writer
@@ -536,8 +500,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
if i % 8 == 7 { if i % 8 == 7 {
if self.show_color { if self.show_color {
self.curr_color = None; self.curr_color = None;
self.writer self.writer.write_all(COLOR_RESET)?;
.write_all(self.colors[ByteColor::Reset as usize].as_bytes())?;
} }
self.writer.write_all(b" ")?; self.writer.write_all(b" ")?;
// byte is last in last panel // byte is last in last panel