hexdump/
hexdump.rs

1extern crate cursive;
2extern crate cursive_hexview;
3
4use cursive::views::{Dialog, DummyView, LinearLayout, TextView};
5use cursive_hexview::{DisplayState, HexView};
6use std::env;
7use std::fs::File;
8use std::io::{self, Read};
9use std::path::Path;
10
11fn read_file(path: &Path) -> Result<Vec<u8>, io::Error> {
12    let mut file = File::open(path)?;
13    let mut buf = Vec::new();
14    file.read_to_end(&mut buf)?;
15    Ok(buf)
16}
17
18fn main() {
19    let arg = env::args()
20        .nth(1)
21        .expect("Please provide the file to read from as first argument");
22    let path = Path::new(&arg);
23
24    let mut cur = cursive::default();
25    let explanation = TextView::new("Use the keys ↑ ↓ ← → to navigate around.\nUse q to exit.");
26    let view = HexView::new_from_iter(read_file(path).expect("Cannot read file")).display_state(DisplayState::Enabled);
27
28    cur.add_layer(
29        Dialog::around(LinearLayout::vertical().child(explanation).child(DummyView).child(view)).title("HexView"),
30    );
31    cur.add_global_callback('q', |cur| cur.quit());
32    cur.run();
33}