From 9e393263e820c328355ac58584ac635540ef2e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 2 May 2021 22:53:30 +0200 Subject: add basic window abstraction --- src/buffer/mod.rs | 18 +++++++++++++++--- src/buffer/threads.rs | 41 +++++++++++++++++------------------------ 2 files changed, 32 insertions(+), 27 deletions(-) (limited to 'src/buffer') diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 10b49c1..0a7e9ba 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -1,23 +1,33 @@ mod threads; +use crate::window::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) -> Self { + pub fn new(initial_buffer: Buffer, out: &mut W) -> Self { + let mut window = Window::new(); + match &initial_buffer { + Buffer::Threads(t) => t.fill_window(&mut window), + } + window.draw(out).unwrap(); Self { + window, buffers: vec![initial_buffer], } } - pub fn run(mut self, mut screen: W, stdin: Stdin) { + pub fn run(mut self, out: &mut W, stdin: Stdin) { for c in stdin.keys() { let c = c.unwrap(); // Global keybinds @@ -32,12 +42,14 @@ impl Client { } let next_buffer = match self.buffers.last_mut().unwrap() { - Buffer::Threads(s) => s.tick(&mut screen, c), + Buffer::Threads(s) => s.tick(&mut self.window, c), }; if let Some(next_buffer) = next_buffer { self.buffers.push(next_buffer); } + + self.window.draw(out).unwrap(); } } } diff --git a/src/buffer/threads.rs b/src/buffer/threads.rs index 85ffb0f..3525c2a 100644 --- a/src/buffer/threads.rs +++ b/src/buffer/threads.rs @@ -1,9 +1,9 @@ use crate::db; +use crate::window::{Line, Window}; use super::Buffer; use notmuch::DatabaseMode; -use std::io::Write; -use termion::{color, event::Key}; +use termion::event::Key; pub struct Threads { threads: Vec, @@ -68,11 +68,21 @@ impl Threads { .collect(); } - pub fn init(&self, out: &mut W) { - self.draw(out); + pub fn fill_window(&self, window: &mut Window) { + println!("filling"); + for (i, thread) in self.threads.iter().enumerate() { + let s = format!("thread: \'{}\' {:?}", thread.subject, thread.authors); + window.lines.push( + if self.i == i { + Line::Highlight(s) + } else { + Line::Normal(s) + } + ); + } } - pub fn tick(&mut self, out: &mut W, key: Key) -> Option { + pub fn tick(&mut self, window: &mut Window, key: Key) -> Option { match key { Key::Char('j') => self.i += 1, Key::Char('k') => self.i = @@ -87,7 +97,7 @@ impl Threads { } Key::Char('s') => { let sent = Threads::from_query(String::from("tag:sent")); - sent.init(out); + sent.fill_window(window); return Some(Buffer::Threads(sent)); } Key::Char('r') => self.reload(), @@ -96,24 +106,7 @@ impl Threads { if !self.threads.is_empty() { self.i = self.i.rem_euclid(self.threads.len()); } - self.draw(out); + self.fill_window(window); None } - - fn draw(&self, out: &mut W) { - write!(out, "{}", termion::clear::All).unwrap(); - - for (i, thread) in self.threads.iter().enumerate() { - write!(out, "{}", termion::cursor::Goto(1, (i + 1) as u16)).unwrap(); - let highlight = i == self.i; - 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