From 6c27b4ad46653f2210dd908f2e3a6fd0f51d9afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sun, 25 Aug 2024 23:49:12 +0300 Subject: [PATCH] docs(example): add demo example and update documentation (#147) --- Cargo.lock | 4 ++-- Cargo.toml | 4 ++-- README.md | 13 +++++++------ examples/demo.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 examples/demo.rs diff --git a/Cargo.lock b/Cargo.lock index d449a06..ded2361 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -556,9 +556,9 @@ dependencies = [ [[package]] name = "ratatui" -version = "0.28.0" +version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ba6a365afbe5615999275bea2446b970b10a41102500e27ce7678d50d978303" +checksum = "fdef7f9be5c0122f890d58bdf4d964349ba6a6161f705907526d891efabba57d" dependencies = [ "bitflags 2.5.0", "cassowary", diff --git a/Cargo.toml b/Cargo.toml index d77f728..60cf00b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,8 @@ categories = ["command-line-utilities", "text-editors"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ratatui = "0.28.0" -clap = { version = "4.5.16", features = ["derive"] } +ratatui = "0.28.1" +clap = { version = "4.5.15", features = ["derive"] } arboard = { version = "3.4.0", default-features = false } memmap2 = "0.9.4" crossbeam = "0.8.4" diff --git a/README.md b/README.md index 1512064..e9a9d7f 100644 --- a/README.md +++ b/README.md @@ -92,9 +92,8 @@ Add `heh` to your dependencies in `Cargo.toml`: ```toml [dependencies] -ratatui = "0.24" -crossterm = "0.27" -heh = "0.4" +ratatui = "0.28" +heh = "0.6" ``` Create the application: @@ -104,14 +103,14 @@ use heh::app::Application as Heh; use heh::decoder::Encoding; let file = std::fs::OpenOptions::new().read(true).write(true).open(path).unwrap(); -let heh = Heh::new(file, Encoding::Ascii, 0).unwrap(); +let mut heh = Heh::new(file, Encoding::Ascii, 0).unwrap(); ``` Then you can render a frame as follows: ```rust terminal.draw(|frame| { - heh.render_frame(frame, frame.size()); + heh.render_frame(frame, frame.area()); }); ``` @@ -121,7 +120,9 @@ To handle key events: heh.handle_input(&ratatui::crossterm::event::Event::Key(/* */)).unwrap(); ``` -See the [binsider](https://github.com/orhun/binsider) project for an example use case. +See the [demo example](examples/demo.rs) for full code. + +See the [binsider](https://github.com/orhun/binsider) project for an example application that uses `heh`. # Contributing diff --git a/examples/demo.rs b/examples/demo.rs new file mode 100644 index 0000000..03de71d --- /dev/null +++ b/examples/demo.rs @@ -0,0 +1,31 @@ +use std::path::PathBuf; + +use heh::app::Application as Heh; +use heh::decoder::Encoding; + +use ratatui::{ + crossterm::event::{self, Event, KeyCode}, + Frame, +}; + +fn main() { + let path = PathBuf::from("Cargo.toml"); + let file = std::fs::OpenOptions::new().read(true).write(true).open(path).unwrap(); + let mut heh = Heh::new(file, Encoding::Ascii, 0).unwrap(); + + let mut terminal = ratatui::init(); + loop { + terminal + .draw(|frame: &mut Frame| { + heh.render_frame(frame, frame.area()); + }) + .expect("failed to draw frame"); + if let Event::Key(key) = event::read().expect("failed to read event") { + if key.code == KeyCode::Char('q') { + break; + } + heh.handle_input(&ratatui::crossterm::event::Event::Key(key)).unwrap(); + } + } + ratatui::restore(); +}