aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-05-04 00:35:37 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-05-04 00:35:37 +0200
commit882dacc3e7a7d517d1a317a9fef3514236c6e89c (patch)
tree206d8a8d95c68eb9d6975f9c7eb8f27ad704c0cf
parentff52420876fb4e9a2d205d5a819db0234fc36b89 (diff)
downloadmail-882dacc3e7a7d517d1a317a9fef3514236c6e89c.tar.gz
basic statusbar and inner output buffer
Closes #2
-rw-r--r--src/buffer/mod.rs32
-rw-r--r--src/buffer/threads.rs1
-rw-r--r--src/window.rs25
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<Buffer>,
+ statusbar: Statusbar,
}
impl Client {
pub fn new<W: Write>(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<W: Write>(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<W: Write>(&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<Line>,
scroll: usize,
@@ -40,18 +54,17 @@ impl Window {
}
}
- pub fn draw<W: Write>(&mut self, out: &mut W, area: Area) -> Result<(), std::io::Error> {
- let mut buffer = Vec::new();
+ pub fn draw<W: Write>(&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(())