diff --git a/candid_tui/src/event.rs b/candid_tui/src/event.rs index c50b35e..d69792e 100644 --- a/candid_tui/src/event.rs +++ b/candid_tui/src/event.rs @@ -5,6 +5,8 @@ //! All code in this module is *heavily* inspired by [`tui-rs`'s demo //! code](https://github.com/fdehau/tui-rs/blob/master/examples/util/event.rs) +use crate::frame::Frame; + use std::sync::mpsc; use std::{io, thread}; @@ -13,25 +15,33 @@ use termion::input::TermRead; pub enum Event { Input(I), + Frame(Frame), } /// An event handler that wraps termion input and tick events. /// Each event thype is handled in its own thread and returned to a common `Receiver` pub struct Events { + /// The receiver that aggregates all other events rx: mpsc::Receiver>, - input_handle: thread::JoinHandle<()>, - // TODO: Add thread for reading from server + + /// Thread that reads input from the user + _input_handle: thread::JoinHandle<()>, + + /// Thread that reads frames from the remote CANdid server + _frame_handle: thread::JoinHandle<()>, } impl Events { + /// Creates a new event with default configuration ('quit' key is `q`) pub fn new() -> Events { Events::with_config(Config::default()) } + /// Creates a new event with custom configuration pub fn with_config(config: Config) -> Events { let (tx, rx) = mpsc::channel(); - let input_handle = { + let _input_handle = { let tx = tx.clone(); thread::spawn(move || { let stdin = io::stdin(); @@ -51,7 +61,19 @@ impl Events { }) }; - Events { rx, input_handle } + let _frame_handle = { + let tx = tx.clone(); + thread::spawn(move || loop { + tx.send(Event::Frame(Frame { msg: "A frame" })).unwrap(); + thread::sleep(std::time::Duration::from_millis(1000)); + }) + }; + + Events { + rx, + _input_handle, + _frame_handle, + } } pub fn next(&self) -> Result, mpsc::RecvError> { diff --git a/candid_tui/src/frame.rs b/candid_tui/src/frame.rs new file mode 100644 index 0000000..e518b20 --- /dev/null +++ b/candid_tui/src/frame.rs @@ -0,0 +1,3 @@ +pub struct Frame { + pub msg: &'static str, +} diff --git a/candid_tui/src/lib.rs b/candid_tui/src/lib.rs new file mode 100644 index 0000000..6a0e163 --- /dev/null +++ b/candid_tui/src/lib.rs @@ -0,0 +1,9 @@ +pub mod app; +pub mod event; +pub mod frame; +pub mod ui; +pub mod util; + +pub use app::AppState; +pub use event::{Event, Events}; +pub use ui::draw; diff --git a/candid_tui/src/main.rs b/candid_tui/src/main.rs index ab9fabd..9e17b60 100644 --- a/candid_tui/src/main.rs +++ b/candid_tui/src/main.rs @@ -1,12 +1,12 @@ -mod app; -mod event; -mod ui; -mod util; +/* +use candid_tui::app::AppState; +use candid_tui::event::{Event, Events}; +use candid_tui::ui; +*/ -use candid_client::*; +use candid_tui::*; -use crate::app::AppState; -use crate::event::{Event, Events}; +use candid_client::*; use std::io; @@ -57,7 +57,7 @@ fn main() { loop { // Refresh the ui - ui::draw(&mut terminal, &app).unwrap(); + candid_tui::draw(&mut terminal, &app).unwrap(); // Handle an incoming event match events.next().unwrap() { @@ -67,6 +67,7 @@ fn main() { Key::Down => app.on_down(), _ => {} }, + Event::Frame(frame) => app.frame_history.push(frame.msg), } // Exit if necessary, set in app.on_key(q) @@ -74,5 +75,4 @@ fn main() { break; } } - std::thread::sleep(std::time::Duration::from_millis(1000)); }