sharkdp / hexyl

A command-line hex viewer
Apache License 2.0
9.07k stars 227 forks source link

Option for reading between two offsets #75

Closed MaxJohansen closed 4 years ago

MaxJohansen commented 4 years ago

I have tried to implement the syntax suggested in the discussion on issue #16.

$ hexyl --range 512:1024 # Means "start at 512th byte, read until byte 1024 (512 bytes)"
$ hexyl --range 512:+512 # Means "start at 512th byte, read 512 bytes"

$ hexyl --range +512:    # Means "start at 512th byte".

I did not add support for negative offsets simply because I do not see a simple way to implement it for Stdin. If required, I suppose it could be implemented with a buffering solution that reads M - N bytes into a buffer until it reaches the end of Stdin before passing it to a reader.

My solution differs from #43 by not modifying the Printer itself, merely the Reader that it receives. This means that it works just as well for Stdin as for a file. A consequence of this is that the bytes are no longer automatically "aligned" in the output, but that just might be what you want in this situation.

$ hexyl -r :+0x20 hexyl
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000000│ 7f 45 4c 46 02 01 01 00 ┊ 00 00 00 00 00 00 00 00 │•ELF•••0┊00000000│
│00000010│ 03 00 3e 00 01 00 00 00 ┊ 30 d1 02 00 00 00 00 00 │•0>0•000┊0ו00000│
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘

$ hexyl -r 0x3:+0x20 hexyl
┌────────┬─────────────────────────┬─────────────────────────┬────────┬────────┐
│00000003│ 46 02 01 01 00 00 00 00 ┊ 00 00 00 00 00 03 00 3e │F•••0000┊00000•0>│
│00000013│ 00 01 00 00 00 30 d1 02 ┊ 00 00 00 00 00 40 00 00 │0•0000ו┊00000@00│
└────────┴─────────────────────────┴─────────────────────────┴────────┴────────┘

This is my first contribution of Rust code and I welcome suggestions that can help me improve! :)

sharkdp commented 4 years ago

Thank you very much for your contribution!

From a first glance, the changes look great, but I didn't have the time for a full review yet. Could be a few more days.

sharkdp commented 4 years ago

I'm going to close this for now, with #88 merged. Happy to reopen it if there is any further comments/progress.