mod threads; use crate::window::{Area, Window}; use std::io::{Stdin, Write}; use termion::event::Key; use termion::input::TermRead; pub use threads::Threads; pub struct Client { window: Window, buffers: Vec, } impl Client { pub fn new(initial_buffer: Buffer, out: &mut W) -> Self { let size = termion::terminal_size().unwrap(); let mut window = Window::new(); match &initial_buffer { Buffer::Threads(t) => t.fill_window(&mut window), } window.draw(out, Area { x: 1, y: 1, w: size.0, h: size.1, }).unwrap(); Self { window, buffers: vec![initial_buffer], } } pub fn run(mut self, out: &mut W, stdin: Stdin) { for c in stdin.keys() { let c = c.unwrap(); // Global keybinds match c { Key::Char('q') => { self.buffers.pop().unwrap(); if self.buffers.is_empty() { break; } }, _ => () } let next_buffer = match self.buffers.last_mut().unwrap() { Buffer::Threads(s) => s.tick(&mut self.window, c), }; if let Some(next_buffer) = next_buffer { self.buffers.push(next_buffer); } write!(out, "{}", termion::clear::All).unwrap(); let size = termion::terminal_size().unwrap(); self.window.draw(out, Area { x: 1, y: 1, w: size.0, h: size.1, }).unwrap(); } } } pub enum Buffer { Threads(Threads), } impl Buffer { pub fn name(&self) -> String { match self { Buffer::Threads(t) => t.name(), } } }