diff --git a/candid_tui/src/app.rs b/candid_tui/src/app.rs index b9881fa..17b8208 100644 --- a/candid_tui/src/app.rs +++ b/candid_tui/src/app.rs @@ -12,20 +12,16 @@ pub struct AppState<'a> { /// Whether or not the app should terminate pub should_quit: bool, - /// The connection to the CANdid server, from which frames are read - pub server: CandidConnection, - /// The frames that have been received from the server to this point pub frame_history: ListState, } impl<'a> AppState<'a> { /// Initialize a new app - pub fn new(title: &'a str, server: CandidConnection) -> AppState<'a> { + pub fn new(title: &'a str) -> AppState<'a> { AppState { title, should_quit: false, - server, frame_history: ListState::new(), } } diff --git a/candid_tui/src/event.rs b/candid_tui/src/event.rs index 340ee1c..4b5cb0a 100644 --- a/candid_tui/src/event.rs +++ b/candid_tui/src/event.rs @@ -10,7 +10,7 @@ use crate::util::FrameInfo; use std::sync::mpsc; use std::{io, thread}; -use candid_client::Frame; +use candid_client::*; use termion::event::Key; use termion::input::TermRead; @@ -34,13 +34,8 @@ pub struct Events { } 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 { + /// Creates a new event handle + pub fn new(mut server: CandidConnection) -> Events { let (tx, rx) = mpsc::channel(); let _input_handle = { @@ -53,7 +48,7 @@ impl Events { if let Err(_) = tx.send(Event::Input(key)) { return; } - if key == config.exit_key { + if key == Key::Char('q') { return; } } @@ -66,11 +61,10 @@ impl Events { let _frame_handle = { let tx = tx.clone(); thread::spawn(move || loop { - let frame = Frame::new(0, [0 as u8; 8]); + let frame = server.read_frame().unwrap(); let frame = FrameInfo(frame, format!("{:?}", frame).to_string()); tx.send(Event::Frame(frame)).unwrap(); - thread::sleep(std::time::Duration::from_millis(1000)); }) }; @@ -85,16 +79,3 @@ impl Events { self.rx.recv() } } - -#[derive(Debug, Clone, Copy)] -pub struct Config { - pub exit_key: Key, -} - -impl Default for Config { - fn default() -> Config { - Config { - exit_key: Key::Char('q'), - } - } -} diff --git a/candid_tui/src/main.rs b/candid_tui/src/main.rs index fa13344..d9a8430 100644 --- a/candid_tui/src/main.rs +++ b/candid_tui/src/main.rs @@ -39,10 +39,10 @@ fn main() { terminal.hide_cursor().unwrap(); // Initialize the app state - let mut app = AppState::new("CANdid TUI", server); + let mut app = AppState::new("CANdid TUI"); // Initialize the event aggregator - let events = Events::new(); + let events = Events::new(server); loop { // Refresh the ui