From e9f42d8574d936eb3eb11699f194f2d74ae7ae06 Mon Sep 17 00:00:00 2001 From: dvkt Date: Sat, 23 Nov 2019 20:01:17 -0800 Subject: [PATCH] fit content to terminal --- src/main.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index ee47b97..99963a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,14 +58,10 @@ fn main() { impl App { fn new() -> App { - let (cols, rows) = termion::terminal_size().expect("can't get terminal size"); App { pages: HashMap::new(), pos: 0, history: Vec::new(), - status: String::new(), - height: rows, - width: cols, } } @@ -274,12 +270,17 @@ impl Page { } fn draw(&self) -> String { + let (_cols, rows) = termion::terminal_size().expect("can't get terminal size"); + let mut line = 0; let mut start = true; let mut skip_to_end = false; let mut links = 0; let mut out = String::with_capacity(self.body.len() * 2); let mut prefix = ""; for (i, c) in self.body.chars().enumerate() { + if line >= rows as usize { + return out; + } let mut is_link = false; if start { match c { @@ -311,7 +312,10 @@ impl Page { } } '\r' => continue, - '\n' => continue, + '\n' => { + line += 1; + continue; + } _ => prefix = "", } if is_link && self.link > 0 && self.link == links { @@ -338,6 +342,7 @@ impl Page { if c == '\n' { out.push_str("\r\n\x1B[0m"); start = true; + line += 1; skip_to_end = false; } } else if c == '\t' { @@ -346,6 +351,7 @@ impl Page { out.push(c); if c == '\n' { out.push_str("\x1B[0m"); + line += 1; start = true; } }