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

feat: add skip option #88

Merged
merged 4 commits into from
May 13, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ matrix:

# Minimum Rust supported channel.
- os: linux
rust: 1.31.0
rust: 1.36.0
env: TARGET=x86_64-unknown-linux-gnu


Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition = "2018"
[dependencies]
ansi_term = "0.12"
atty = "0.2"
libc = "0.2"

[dependencies.clap]
version = "2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ or Windows Terminal since Windows 10 1903).

### Via cargo

If you have Rust 1.31 or higher, you can install `hexyl` from source via `cargo`:
If you have Rust 1.36 or higher, you can install `hexyl` from source via `cargo`:
```
cargo install hexyl
```
Expand Down
34 changes: 25 additions & 9 deletions src/bin/hexyl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ extern crate clap;
use atty;

use std::fs::File;
use std::io::{self, prelude::*};
use std::io::{self, prelude::*, SeekFrom};

use clap::{App, AppSettings, Arg};

use atty::Stream;

use hexyl::{BorderStyle, Printer};
use hexyl::{BorderStyle, Input, Printer};

fn run() -> Result<(), Box<dyn std::error::Error>> {
let app = App::new(crate_name!())
Expand All @@ -37,6 +37,14 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
.value_name("N")
.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::with_name("nosqueezing")
.short("v")
Expand Down Expand Up @@ -80,17 +88,25 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {

let stdin = io::stdin();

let mut reader: Box<dyn Read> = match matches.value_of("file") {
Some(filename) => Box::new(File::open(filename)?),
None => Box::new(stdin.lock()),
let mut reader: Input = match matches.value_of("file") {
Some(filename) => Input::File(File::open(filename)?),
None => Input::Stdin(stdin.lock()),
};

let length_arg = matches.value_of("length").or(matches.value_of("bytes"));
let skip_arg = matches.value_of("skip").and_then(parse_hex_or_int);

if let Some(length) = length_arg.and_then(parse_hex_or_int) {
reader = Box::new(reader.take(length));
if let Some(skip) = skip_arg {
reader.seek(SeekFrom::Start(skip))?;
}

let length_arg = matches.value_of("length").or(matches.value_of("bytes"));

let mut reader = if let Some(length) = length_arg.and_then(parse_hex_or_int) {
Box::new(reader.take(length))
} else {
reader.into_inner()
};

let show_color = match matches.value_of("color") {
Some("never") => false,
Some("auto") => atty::is(Stream::Stdout),
Expand All @@ -108,7 +124,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
let display_offset = matches
.value_of("display_offset")
.and_then(parse_hex_or_int)
.unwrap_or(0);
.unwrap_or(skip_arg.unwrap_or(0));

let stdout = io::stdout();
let mut stdout_lock = stdout.lock();
Expand Down
48 changes: 48 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::fs;
use std::io::{self, Read, Seek, SeekFrom};

pub enum Input<'a> {
File(fs::File),
Stdin(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.read(buf),
}
}
}

impl<'a> Seek for Input<'a> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
match *self {
Input::File(ref mut file) => {
let seek_res = file.seek(pos);
if let Err(Some(libc::ESPIPE)) = seek_res.as_ref().map_err(|err| err.raw_os_error())
{
return Err(io::Error::new(
io::ErrorKind::Other,
"Using '--seek' is not supported when using a pipe",
)
.into());
};
seek_res
}
Input::Stdin(_) => Err(io::Error::new(
io::ErrorKind::Other,
"Using '--seek' is not supported when reading from STDIN",
)),
}
}
}

impl<'a> Input<'a> {
pub fn into_inner(self) -> Box<dyn Read + 'a> {
match self {
Input::File(file) => Box::new(file),
Input::Stdin(stdin) => Box::new(stdin),
}
}
}
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub(crate) mod input;
pub mod squeezer;

pub use input::*;

use std::io::{self, Read, Write};

use ansi_term::Color;
Expand Down Expand Up @@ -407,7 +410,7 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
/// is exhausted.
pub fn print_all<Reader: Read>(
&mut self,
mut reader: Reader
mut reader: Reader,
) -> Result<(), Box<dyn std::error::Error>> {
let mut buffer = [0; BUFFER_SIZE];
'mainloop: loop {
Expand Down