aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-05-03 22:47:58 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-05-03 22:47:58 +0200
commitc36df73b1e3d3c7f2feeda16e79bbdb2e1a4f251 (patch)
tree6cf577506aa96a4410cd7cfeb966f2a1f7a8cd82
parent73807bf83e30aa14f3aa1a75afc8ade4ff4b929c (diff)
downloadmail-c36df73b1e3d3c7f2feeda16e79bbdb2e1a4f251.tar.gz
draw inside terminal size
-rw-r--r--src/buffer/mod.rs20
-rw-r--r--src/buffer/threads.rs2
-rw-r--r--src/window.rs42
3 files changed, 47 insertions, 17 deletions
diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs
index 0a7e9ba..3cff6f5 100644
--- a/src/buffer/mod.rs
+++ b/src/buffer/mod.rs
@@ -1,6 +1,6 @@
mod threads;
-use crate::window::Window;
+use crate::window::{Area, Window};
use std::io::{Stdin, Write};
use termion::event::Key;
@@ -16,11 +16,17 @@ pub struct Client {
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();
match &initial_buffer {
Buffer::Threads(t) => t.fill_window(&mut window),
}
- window.draw(out).unwrap();
+ window.draw(out, Area {
+ x: 1,
+ y: 1,
+ w: size.0,
+ h: size.1,
+ }).unwrap();
Self {
window,
buffers: vec![initial_buffer],
@@ -49,7 +55,15 @@ impl Client {
self.buffers.push(next_buffer);
}
- self.window.draw(out).unwrap();
+ write!(out, "{}", termion::clear::All).unwrap();
+
+ let size = termion::terminal_size().unwrap();
+ self.window.draw(out, Area {
+ x: 1,
+ y: 1,
+ w: size.0,
+ h: size.1,
+ }).unwrap();
}
}
}
diff --git a/src/buffer/threads.rs b/src/buffer/threads.rs
index edc2339..49d9924 100644
--- a/src/buffer/threads.rs
+++ b/src/buffer/threads.rs
@@ -12,6 +12,7 @@ pub struct Threads {
query: String,
}
+#[derive(Clone)]
pub struct Thread {
subject: String,
authors: Vec<String>,
@@ -66,6 +67,7 @@ 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 178c484..f01cfe9 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -3,6 +3,14 @@
use std::io::Write;
use termion::{clear, color, cursor};
+#[derive(Clone, Copy)]
+pub struct Area {
+ pub x: u16,
+ pub y: u16,
+ pub w: u16,
+ pub h: u16,
+}
+
pub enum Line {
Normal(String),
Highlight(String),
@@ -19,20 +27,26 @@ impl Window {
}
}
- pub fn draw<W: Write>(&mut self, out: &mut W) -> Result<(), std::io::Error> {
- write!(out, "{}", clear::All)?;
-
- for (i, line) in self.lines.iter().enumerate() {
- write!(out, "{}", cursor::Goto(1, (i + 1) as u16))?;
- match line {
- Line::Normal(s) => write!(out, "{}", s)?,
- Line::Highlight(s) => write!(
- out,
- "{}{}{}",
- color::Fg(color::Red),
- s,
- color::Fg(color::Reset),
- )?,
+ pub fn draw<W: Write>(&mut self, out: &mut W, area: Area) -> Result<(), std::io::Error> {
+ let mut y = area.y;
+ let mut lines = self.lines.iter();
+ while y < area.h + 1 {
+ match lines.next() {
+ Some(line) => {
+ write!(out, "{}", cursor::Goto(area.x, y))?;
+ match line {
+ Line::Normal(s) => write!(out, "{}", s)?,
+ Line::Highlight(s) => write!(
+ out,
+ "{}{}{}",
+ color::Fg(color::Red),
+ s,
+ color::Fg(color::Reset),
+ )?,
+ }
+ y += 1;
+ }
+ None => break,
}
}
out.flush()?;