From 882dacc3e7a7d517d1a317a9fef3514236c6e89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 4 May 2021 00:35:37 +0200 Subject: basic statusbar and inner output buffer Closes #2 --- src/buffer/mod.rs | 32 ++++++++++++++++++++++++++++---- src/buffer/threads.rs | 1 - src/window.rs | 25 ++++++++++++++++++------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 3cff6f5..1d7d22e 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -1,6 +1,6 @@ mod threads; -use crate::window::{Area, Window}; +use crate::window::{Area, Statusbar, Window}; use std::io::{Stdin, Write}; use termion::event::Key; @@ -12,28 +12,41 @@ pub use threads::Threads; pub struct Client { window: Window, buffers: Vec, + statusbar: Statusbar, } impl Client { pub fn new(initial_buffer: Buffer, out: &mut W) -> Self { let size = termion::terminal_size().unwrap(); let mut window = Window::new(); + let statusbar = Statusbar::new(String::from("Hello world!")); + match &initial_buffer { Buffer::Threads(t) => t.fill_window(&mut window), } + window.draw(out, Area { x: 1, y: 1, w: size.0, - h: size.1, + h: size.1 - 2, + }).unwrap(); + statusbar.draw(out, Area { + x: 1, + y: size.1, + w: size.0, + h: 1, }).unwrap(); + Self { window, buffers: vec![initial_buffer], + statusbar, } } pub fn run(mut self, out: &mut W, stdin: Stdin) { + let mut buffer = Vec::new(); for c in stdin.keys() { let c = c.unwrap(); // Global keybinds @@ -58,12 +71,23 @@ impl Client { write!(out, "{}", termion::clear::All).unwrap(); let size = termion::terminal_size().unwrap(); - self.window.draw(out, Area { + self.window.draw(&mut buffer, Area { x: 1, y: 1, w: size.0, - h: size.1, + h: size.1 - 2, }).unwrap(); + + self.statusbar.draw(&mut buffer, Area { + x: 1, + y: size.1, + w: size.0, + h: 1, + }).unwrap(); + + out.write(&buffer).unwrap(); + out.flush().unwrap(); + buffer.clear(); } } } diff --git a/src/buffer/threads.rs b/src/buffer/threads.rs index 5a87388..fa12110 100644 --- a/src/buffer/threads.rs +++ b/src/buffer/threads.rs @@ -67,7 +67,6 @@ impl Threads { .unwrap() .map(Thread::new) .collect(); - self.threads.append(&mut self.threads.clone()); } pub fn fill_window(&self, window: &mut Window) { diff --git a/src/window.rs b/src/window.rs index 809ac4d..66d25bf 100644 --- a/src/window.rs +++ b/src/window.rs @@ -27,6 +27,20 @@ pub fn truncate_dots(s: &str, to: usize) -> String { } } +pub struct Statusbar(String); + +impl Statusbar { + pub fn new(s: String) -> Self { + Self(s) + } + + pub fn draw(&self, buf: &mut W, area: Area) -> Result<(), std::io::Error> { + write!(buf, "{}", cursor::Goto(area.x, area.y))?; + write!(buf, "{}", truncate_dots(&self.0, area.w as usize))?; + Ok(()) + } +} + pub struct Window { pub lines: Vec, scroll: usize, @@ -40,18 +54,17 @@ impl Window { } } - pub fn draw(&mut self, out: &mut W, area: Area) -> Result<(), std::io::Error> { - let mut buffer = Vec::new(); + pub fn draw(&mut self, buf: &mut W, area: Area) -> Result<(), std::io::Error> { let mut y = area.y; let mut lines = self.lines.iter().skip(self.scroll); while y < area.h + 1 { match lines.next() { Some(line) => { - write!(buffer, "{}", cursor::Goto(area.x, y))?; + write!(buf, "{}", cursor::Goto(area.x, y))?; match line { - Line::Normal(s) => write!(buffer, "{}", truncate_dots(s, area.w as usize))?, + Line::Normal(s) => write!(buf, "{}", truncate_dots(s, area.w as usize))?, Line::Highlight(s) => write!( - buffer, + buf, "{}{}{}", color::Fg(color::Red), truncate_dots(s, area.w as usize), @@ -63,8 +76,6 @@ impl Window { None => break, } } - out.write(&buffer)?; - out.flush()?; self.lines.clear(); Ok(()) -- cgit v1.2.1