From abecc3b586a1d955fb0b1fe8c03132f755c559c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Mon, 26 Apr 2021 22:54:32 +0200 Subject: move out thread state --- src/state/threads.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/state/threads.rs (limited to 'src/state/threads.rs') diff --git a/src/state/threads.rs b/src/state/threads.rs new file mode 100644 index 0000000..20b365e --- /dev/null +++ b/src/state/threads.rs @@ -0,0 +1,43 @@ +use super::State; + +use std::io::Write; +use termion::{color, event::Key}; + +pub struct Threads<'d, 'q> { + pub threads: Vec>, + pub i: isize, +} + +impl<'d, 'q> Threads<'d, 'q> { + pub fn init(&mut self, out: &mut W) { + draw(&self, out); + } + + pub fn tick(mut self, out: &mut W, key: Key) -> State<'d, 'q> { + match key { + Key::Char('j') => self.i += 1, + Key::Char('k') => self.i -= 1, + _ => (), + } + self.i = self.i.rem_euclid(self.threads.len() as isize); + draw(&self, out); + State::Threads(self) + } +} + +fn draw(state: &Threads, out: &mut W) { + write!(out, "{}", termion::clear::All).unwrap(); + + for (i, thread) in state.threads.iter().enumerate() { + write!(out, "{}", termion::cursor::Goto(1, (i + 1) as u16)).unwrap(); + let highlight = i == state.i as usize; + if highlight { + write!(out, "{}", color::Fg(color::Red)).unwrap(); + } + write!(out, "thread {:?}, {:?}", thread.subject(), thread.authors()).unwrap(); + if highlight { + write!(out, "{}", color::Fg(color::Reset)).unwrap(); + } + } + out.flush().unwrap(); +} -- cgit v1.2.1