diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-04-28 23:43:51 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-04-28 23:43:51 +0200 |
| commit | 78fc967b36d83c1a29bf15f3e8686feaf842b68b (patch) | |
| tree | 865183bf10d94680d686f8d78aff1ed0f65fad2b /src/state/mod.rs | |
| parent | 8787162d7c973fad5a737172be81d7cafe9348ba (diff) | |
| download | mail-78fc967b36d83c1a29bf15f3e8686feaf842b68b.tar.gz | |
state stack
Diffstat (limited to 'src/state/mod.rs')
| -rw-r--r-- | src/state/mod.rs | 30 |
1 files changed, 24 insertions, 6 deletions
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<State>, } -impl State { +impl Client { + pub fn new(initial_state: State) -> Self { + Self { + states: vec![initial_state], + } + } + pub fn run<W: Write>(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), +} |
