diff --git a/Cargo.lock b/Cargo.lock index b215338..9e19216 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,18 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bytemuck" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cassowary" version = "0.3.0" @@ -50,6 +62,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "compact_str" version = "0.8.0" @@ -130,6 +152,16 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" +[[package]] +name = "drawille" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64e461c3f1e69d99372620640b3fd5f0309eeda2e26e4af69f6760c0e1df845" +dependencies = [ + "colored", + "fnv", +] + [[package]] name = "either" version = "1.13.0" @@ -149,7 +181,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -164,6 +196,17 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hashbrown" version = "0.15.2" @@ -251,6 +294,12 @@ dependencies = [ "syn", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.166" @@ -294,6 +343,12 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "mio" version = "1.0.2" @@ -304,7 +359,17 @@ dependencies = [ "libc", "log", "wasi", - "windows-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", ] [[package]] @@ -317,6 +382,8 @@ checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" name = "orangepunk-tui" version = "0.1.0" dependencies = [ + "rand", + "rust-roller", "tui-realm-stdlib", "tuirealm", ] @@ -341,7 +408,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -350,6 +417,15 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "pretty_assertions" version = "1.4.1" @@ -378,6 +454,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "ratatui" version = "0.28.1" @@ -437,6 +543,24 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rgb" +version = "0.8.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "rust-roller" +version = "0.1.0" +dependencies = [ + "nom", + "rand", + "textplots", +] + [[package]] name = "rustix" version = "0.38.41" @@ -447,7 +571,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -555,6 +679,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "textplots" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f59b64803118dbff62f92842b3154a2c802dfd8e18660132bbcbfb141c637ae3" +dependencies = [ + "drawille", + "rgb", +] + [[package]] name = "textwrap" version = "0.16.1" @@ -691,13 +825,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -706,28 +864,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -740,24 +916,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" @@ -769,3 +969,24 @@ name = "yansi" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index 63489af..d2476cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,6 @@ edition = "2021" [dependencies] tui-realm-stdlib = "2.0.1" tuirealm = "2.0.3" + +rust-roller = { path = "/home/charles/p/rust-roller" } +rand = "0.8.5" diff --git a/src/app/model.rs b/src/app/model.rs index aa9e317..df9b7b4 100644 --- a/src/app/model.rs +++ b/src/app/model.rs @@ -12,7 +12,7 @@ use tuirealm::{ Application, AttrValue, Attribute, EventListenerCfg, Sub, SubClause, SubEventClause, Update, }; -use super::components::{Clock, DigitCounter, Label, LetterCounter}; +use super::components::{Clock, DigitCounter, Label, RollsChart, TextInput}; use super::{Id, Msg}; pub struct Model @@ -54,7 +54,8 @@ where .constraints( [ Constraint::Length(3), // Clock - Constraint::Length(3), // Letter Counter + Constraint::Length(3), // Input + Constraint::Length(30), // Barchart Constraint::Length(3), // Digit Counter Constraint::Length(1), // Label ] @@ -62,9 +63,10 @@ where ) .split(f.area()); self.app.view(&Id::Clock, f, chunks[0]); - self.app.view(&Id::LetterCounter, f, chunks[1]); - self.app.view(&Id::DigitCounter, f, chunks[2]); - self.app.view(&Id::Label, f, chunks[3]); + self.app.view(&Id::PromptInput, f, chunks[1]); + self.app.view(&Id::RollsChart, f, chunks[2]); + self.app.view(&Id::DigitCounter, f, chunks[3]); + self.app.view(&Id::Label, f, chunks[4]); }) .is_ok()); } @@ -96,6 +98,13 @@ where Vec::default(), ) .is_ok()); + assert!(app.mount( + Id::PromptInput, + Box::new( + TextInput::default(), + ), + Vec::default(), + ).is_ok()); // Mount clock, subscribe to tick assert!(app .mount( @@ -113,8 +122,8 @@ where // Mount counters assert!(app .mount( - Id::LetterCounter, - Box::new(LetterCounter::new(0)), + Id::RollsChart, + Box::new(RollsChart::default()), Vec::new() ) .is_ok()); @@ -126,7 +135,7 @@ where ) .is_ok()); // Active letter counter - assert!(app.active(&Id::LetterCounter).is_ok()); + assert!(app.active(&Id::PromptInput).is_ok()); app } } @@ -153,6 +162,7 @@ where assert!(self.app.active(&Id::LetterCounter).is_ok()); None } + Msg::InputUpdated => None, Msg::DigitCounterChanged(v) => { // Update label assert!(self diff --git a/src/components/input.rs b/src/components/input.rs new file mode 100644 index 0000000..566b9b5 --- /dev/null +++ b/src/components/input.rs @@ -0,0 +1,61 @@ +use tui_realm_stdlib::Input; +use tuirealm::{command::{Cmd, CmdResult, Direction, Position}, event::{Key, KeyEvent, KeyModifiers}, props::{Alignment, BorderType, Borders, Color, InputType, Style}, Component, Event, MockComponent, NoUserEvent}; + +use crate::Msg; + +#[derive(MockComponent)] +pub struct TextInput { + component: Input +} + +impl Default for TextInput { + fn default() -> Self { + Self { + component: Input::default() + .borders( + Borders::default() + .modifiers(BorderType::Rounded) + .color(Color::LightYellow), + ) + .foreground(Color::LightYellow) + .input_type(InputType::Text) + .title("Roll", Alignment::Left) + .value("veeso") + .invalid_style(Style::default().fg(Color::Red)), + } + } +} + + +impl Component for TextInput { + fn on(&mut self, ev: Event) -> Option { + let _ = match ev { + Event::Keyboard(KeyEvent { + code: Key::Left, .. + }) => self.perform(Cmd::Move(Direction::Left)), + Event::Keyboard(KeyEvent { + code: Key::Right, .. + }) => self.perform(Cmd::Move(Direction::Right)), + Event::Keyboard(KeyEvent { + code: Key::Home, .. + }) => self.perform(Cmd::GoTo(Position::Begin)), + Event::Keyboard(KeyEvent { code: Key::End, .. }) => { + self.perform(Cmd::GoTo(Position::End)) + } + Event::Keyboard(KeyEvent { + code: Key::Delete, .. + }) => self.perform(Cmd::Cancel), + Event::Keyboard(KeyEvent { + code: Key::Backspace, + .. + }) => self.perform(Cmd::Delete), + Event::Keyboard(KeyEvent { + code: Key::Char(ch), + modifiers: KeyModifiers::NONE, + }) => self.perform(Cmd::Type(ch)), + Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => return Some(Msg::AppClose), + _ => CmdResult::None, + }; + Some(Msg::InputUpdated) + } +} \ No newline at end of file diff --git a/src/components/mod.rs b/src/components/mod.rs index 794c432..8e3411d 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -11,11 +11,15 @@ use super::Msg; mod clock; mod counter; mod label; +mod input; +mod rolls; // -- export pub use clock::Clock; pub use counter::{DigitCounter, LetterCounter}; pub use label::Label; +pub use input::TextInput; +pub use rolls::RollsChart; /// ### get_block /// diff --git a/src/components/rolls.rs b/src/components/rolls.rs new file mode 100644 index 0000000..9a542bd --- /dev/null +++ b/src/components/rolls.rs @@ -0,0 +1,73 @@ +use rand::thread_rng; +use rust_roller::{Buckets, Roller}; +use tui_realm_stdlib::BarChart; +use tuirealm::{ + command::CmdResult, + event::KeyEvent, + props::{Alignment, Borders, Color, Style}, + Component, Event, MockComponent, NoUserEvent, +}; + +use crate::Msg; + +#[derive(MockComponent)] +pub struct RollsChart { + component: BarChart, + states: OwnStates, +} + +impl Default for RollsChart { + fn default() -> Self { + let states = OwnStates::default(); + let bars = states.buckets.labels().len(); + let labels: Vec = states + .buckets + .labels() + .iter() + .map(|l| format!("{l}")) + .collect(); + let values: Vec<(&str, u64)> = labels + .iter() + .zip(states.buckets.buckets()) + .map(|(label, val)| (label.as_str(), *val)) + .collect(); + Self { + component: BarChart::default() + .disabled(false) + .title("my incomes", Alignment::Center) + .label_style(Style::default().fg(Color::Yellow)) + .bar_style(Style::default().fg(Color::LightYellow)) + .borders(Borders::default().color(Color::LightBlue)) + .max_bars(bars) + .value_style(Style::default().fg(Color::LightBlue)) + .data(&values), + states, + } + } +} + +impl Component for RollsChart { + fn on(&mut self, ev: Event) -> Option { + let _ = match ev { + _ => CmdResult::None, + }; + None + } +} + +struct OwnStates { + roller: Roller<128>, + buckets: Buckets<16>, +} + +impl Default for OwnStates { + fn default() -> Self { + let mut roller = Roller::new(); + roller.parse("2d8").unwrap(); + let mut buckets = Buckets::new(&roller); + for _ in 0..1000 { + buckets.insert(roller.roll(&mut thread_rng())); + } + Self { roller, buckets } + } +} diff --git a/src/main.rs b/src/main.rs index 665aba1..80e5787 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate tuirealm; use tuirealm::application::PollStrategy; use tuirealm::{AttrValue, Attribute, Update}; -// -- internal + mod app; mod components; use app::model::Model; @@ -20,6 +20,7 @@ pub enum Msg { DigitCounterBlur, LetterCounterChanged(isize), LetterCounterBlur, + InputUpdated, } // Let's define the component ids for our application @@ -29,6 +30,8 @@ pub enum Id { DigitCounter, LetterCounter, Label, + PromptInput, + RollsChart, } fn main() {