From 78fc967b36d83c1a29bf15f3e8686feaf842b68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 28 Apr 2021 23:43:51 +0200 Subject: state stack --- src/state/mod.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'src/state/mod.rs') diff --git a/src/state/mod.rs b/src/state/mod.rs index 7b498f2..0fe9f40 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -6,24 +6,42 @@ use termion::input::TermRead; pub use threads::Threads; -pub enum State { - Threads(Threads), +pub struct Client { + states: Vec, } -impl State { +impl Client { + pub fn new(initial_state: State) -> Self { + Self { + states: vec![initial_state], + } + } + pub fn run(mut self, mut screen: W, stdin: Stdin) { for c in stdin.keys() { let c = c.unwrap(); // Global keybinds match c { - Key::Char('q') => break, + Key::Char('q') => { + self.states.pop().unwrap(); + if self.states.is_empty() { + break; + } + }, _ => () } - // Pass to current state - self = match self { + let next_state = match self.states.last_mut().unwrap() { State::Threads(s) => s.tick(&mut screen, c), }; + + if let Some(next_state) = next_state { + self.states.push(next_state); + } } } } + +pub enum State { + Threads(Threads), +} -- cgit v1.2.1