aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-04-28 23:43:51 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-04-28 23:43:51 +0200
commit78fc967b36d83c1a29bf15f3e8686feaf842b68b (patch)
tree865183bf10d94680d686f8d78aff1ed0f65fad2b
parent8787162d7c973fad5a737172be81d7cafe9348ba (diff)
downloadmail-78fc967b36d83c1a29bf15f3e8686feaf842b68b.tar.gz
state stack
-rw-r--r--src/main.rs6
-rw-r--r--src/state/mod.rs30
-rw-r--r--src/state/threads.rs4
3 files changed, 29 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index 21b6465..3f71050 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
mod db;
mod state;
-use crate::state::State;
+use crate::state::{Client, State};
use crate::state::Threads;
use std::io::{stdin, stdout};
@@ -28,6 +28,6 @@ fn main() {
threads.init(&mut screen);
- let state = State::Threads(threads);
- state.run(screen, stdin);
+ let client = Client::new(State::Threads(threads));
+ client.run(screen, stdin);
}
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),
+}
diff --git a/src/state/threads.rs b/src/state/threads.rs
index 6ffd7e7..b6fddbb 100644
--- a/src/state/threads.rs
+++ b/src/state/threads.rs
@@ -68,7 +68,7 @@ impl<'d, 'q> Threads {
draw(&self, out);
}
- pub fn tick<W: Write>(mut self, out: &mut W, key: Key) -> State {
+ pub fn tick<W: Write>(&mut self, out: &mut W, key: Key) -> Option<State> {
match key {
Key::Char('j') => self.i += 1,
Key::Char('k') => self.i =
@@ -87,7 +87,7 @@ impl<'d, 'q> Threads {
self.i = self.i.rem_euclid(self.threads.len());
}
draw(&self, out);
- State::Threads(self)
+ None
}
}