mirror of
https://github.com/sharkdp/hexyl.git
synced 2026-10-30 09:05:54 +00:00
Use --color=always/auto/never instead of --no-color
This commit is contained in:
1 parent
74861353fa
commit
f6f41e76aa
3 files changed
+20
-7
No files matched your search
Generated
+1
@@ -41,6 +41,7 @@ name = "hexyl"
|
|||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ version = "0.3.1"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ansi_term = "0.11"
|
ansi_term = "0.11"
|
||||||
|
atty = "0.2"
|
||||||
|
|
||||||
[dependencies.clap]
|
[dependencies.clap]
|
||||||
version = "2"
|
version = "2"
|
||||||
|
|||||||
+18
-7
@@ -1,6 +1,7 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate ansi_term;
|
extern crate ansi_term;
|
||||||
|
extern crate atty;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, prelude::*, StdoutLock};
|
use std::io::{self, prelude::*, StdoutLock};
|
||||||
@@ -10,6 +11,8 @@ use clap::{App, AppSettings, Arg};
|
|||||||
use ansi_term::Colour;
|
use ansi_term::Colour;
|
||||||
use ansi_term::Colour::Fixed;
|
use ansi_term::Colour::Fixed;
|
||||||
|
|
||||||
|
use atty::Stream;
|
||||||
|
|
||||||
const BUFFER_SIZE: usize = 256;
|
const BUFFER_SIZE: usize = 256;
|
||||||
|
|
||||||
const COLOR_NULL: Colour = Fixed(242); // grey
|
const COLOR_NULL: Colour = Fixed(242); // grey
|
||||||
@@ -138,7 +141,7 @@ impl<'a> Printer<'a> {
|
|||||||
fn print_byte(&mut self, b: u8) -> io::Result<()> {
|
fn print_byte(&mut self, b: u8) -> io::Result<()> {
|
||||||
if self.idx % 16 == 1 {
|
if self.idx % 16 == 1 {
|
||||||
let style = COLOR_OFFSET.normal();
|
let style = COLOR_OFFSET.normal();
|
||||||
let byte_index = format!("{:08x}", self.idx -1);
|
let byte_index = format!("{:08x}", self.idx - 1);
|
||||||
let formatted_string = if self.show_color {
|
let formatted_string = if self.show_color {
|
||||||
format!("{}", style.paint(byte_index))
|
format!("{}", style.paint(byte_index))
|
||||||
} else {
|
} else {
|
||||||
@@ -226,13 +229,17 @@ fn run() -> Result<(), Box<::std::error::Error>> {
|
|||||||
.long("length")
|
.long("length")
|
||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.value_name("N")
|
.value_name("N")
|
||||||
.help("read only N bytes from the input"),
|
.help("Read only N bytes from the input"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("no-color")
|
Arg::with_name("color")
|
||||||
.long("no-color")
|
.long("color")
|
||||||
.takes_value(false)
|
.takes_value(true)
|
||||||
.help("Don't output ANSI color characters")
|
.value_name("when")
|
||||||
|
.possible_values(&["always", "auto", "never"])
|
||||||
|
.default_value("always")
|
||||||
|
.help("When to use colors. The auto-mode only displays colors if the output \
|
||||||
|
goes to an interactive terminal"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let matches = app.get_matches_safe()?;
|
let matches = app.get_matches_safe()?;
|
||||||
@@ -251,7 +258,11 @@ fn run() -> Result<(), Box<::std::error::Error>> {
|
|||||||
reader = Box::new(reader.take(length));
|
reader = Box::new(reader.take(length));
|
||||||
}
|
}
|
||||||
|
|
||||||
let show_color = !matches.is_present("no-color");
|
let show_color = match matches.value_of("color") {
|
||||||
|
Some("never") => false,
|
||||||
|
Some("auto") => atty::is(Stream::Stdout),
|
||||||
|
_ => true,
|
||||||
|
};
|
||||||
|
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut printer = Printer::new(stdout.lock(), show_color);
|
let mut printer = Printer::new(stdout.lock(), show_color);
|
||||||
|
|||||||
Reference in new issue
Block a user