mirror of
https://github.com/sharkdp/hexyl.git
synced 2026-10-30 09:05:54 +00:00
1 parent
6f73f4c0f0
commit
244f71691d
3 files changed
+67
-9
No files matched your search
+25
-8
@@ -3,14 +3,15 @@ extern crate clap;
|
|||||||
|
|
||||||
use atty;
|
use atty;
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, prelude::*};
|
use std::io::{self, prelude::*, SeekFrom};
|
||||||
|
|
||||||
use clap::{App, AppSettings, Arg};
|
use clap::{App, AppSettings, Arg};
|
||||||
|
|
||||||
use atty::Stream;
|
use atty::Stream;
|
||||||
|
|
||||||
use hexyl::{BorderStyle, Printer};
|
use hexyl::{BorderStyle, Input, Printer};
|
||||||
|
|
||||||
fn run() -> Result<(), Box<dyn std::error::Error>> {
|
fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let app = App::new(crate_name!())
|
let app = App::new(crate_name!())
|
||||||
@@ -37,6 +38,14 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.value_name("N")
|
.value_name("N")
|
||||||
.help("An alias for -n/--length"),
|
.help("An alias for -n/--length"),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("skip")
|
||||||
|
.short("s")
|
||||||
|
.long("skip")
|
||||||
|
.takes_value(true)
|
||||||
|
.value_name("N")
|
||||||
|
.help("Skip first N bytes"),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("nosqueezing")
|
Arg::with_name("nosqueezing")
|
||||||
.short("v")
|
.short("v")
|
||||||
@@ -80,16 +89,24 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
let stdin = io::stdin();
|
let stdin = io::stdin();
|
||||||
|
|
||||||
let mut reader: Box<dyn Read> = match matches.value_of("file") {
|
let mut reader: Input = match matches.value_of("file") {
|
||||||
Some(filename) => Box::new(File::open(filename)?),
|
Some(filename) => Input::File(File::open(filename)?),
|
||||||
None => Box::new(stdin.lock()),
|
None => Input::Stdin(RefCell::new(stdin.lock())),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let skip_arg = matches.value_of("skip");
|
||||||
|
|
||||||
|
if let Some(length) = skip_arg.and_then(parse_hex_or_int) {
|
||||||
|
reader.seek(SeekFrom::Start(length))?;
|
||||||
|
}
|
||||||
|
|
||||||
let length_arg = matches.value_of("length").or(matches.value_of("bytes"));
|
let length_arg = matches.value_of("length").or(matches.value_of("bytes"));
|
||||||
|
|
||||||
if let Some(length) = length_arg.and_then(parse_hex_or_int) {
|
let mut reader = if let Some(length) = length_arg.and_then(parse_hex_or_int) {
|
||||||
reader = Box::new(reader.take(length));
|
Box::new(reader.take(length))
|
||||||
}
|
} else {
|
||||||
|
reader.into_inner()
|
||||||
|
};
|
||||||
|
|
||||||
let show_color = match matches.value_of("color") {
|
let show_color = match matches.value_of("color") {
|
||||||
Some("never") => false,
|
Some("never") => false,
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
|
use std::fs;
|
||||||
|
use std::io::{self, Read, Seek, SeekFrom};
|
||||||
|
|
||||||
|
pub enum Input<'a> {
|
||||||
|
File(fs::File),
|
||||||
|
Stdin(RefCell<io::StdinLock<'a>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Read for Input<'a> {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
match *self {
|
||||||
|
Input::File(ref mut file) => file.read(buf),
|
||||||
|
Input::Stdin(ref mut stdin) => stdin.borrow_mut().read(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Seek for Input<'a> {
|
||||||
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||||
|
match *self {
|
||||||
|
Input::File(ref mut file) => file.seek(pos),
|
||||||
|
Input::Stdin(_) => Err(io::Error::new(
|
||||||
|
io::ErrorKind::Other,
|
||||||
|
"not supported by stdin-input",
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Input<'a> {
|
||||||
|
pub fn into_inner(self) -> Box<dyn Read + 'a> {
|
||||||
|
match self {
|
||||||
|
Self::File(file) => Box::new(file),
|
||||||
|
Self::Stdin(stdin) => Box::new(stdin.into_inner()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-1
@@ -1,5 +1,8 @@
|
|||||||
|
pub(crate) mod input;
|
||||||
pub mod squeezer;
|
pub mod squeezer;
|
||||||
|
|
||||||
|
pub use input::*;
|
||||||
|
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
|
|
||||||
use ansi_term::Color;
|
use ansi_term::Color;
|
||||||
@@ -407,7 +410,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
|
|||||||
/// is exhausted.
|
/// is exhausted.
|
||||||
pub fn print_all<Reader: Read>(
|
pub fn print_all<Reader: Read>(
|
||||||
&mut self,
|
&mut self,
|
||||||
mut reader: Reader
|
mut reader: Reader,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut buffer = [0; BUFFER_SIZE];
|
let mut buffer = [0; BUFFER_SIZE];
|
||||||
'mainloop: loop {
|
'mainloop: loop {
|
||||||
|
|||||||
Reference in new issue
Block a user