use terminal codes to clear lines, fix a few text bugs

pull/14/head
dvkt 5 years ago
parent 549ed5e481
commit d307d79fe6

@ -1,7 +1,7 @@
use crate::gopher::{self, Type}; use crate::gopher::{self, Type};
use crate::ui::{Action, Key, View, MAX_COLS, SCROLL_LINES}; use crate::ui::{Action, Key, View, MAX_COLS, SCROLL_LINES};
use std::fmt; use std::fmt;
use termion::cursor; use termion::{clear, cursor};
pub struct Menu { pub struct Menu {
pub url: String, // gopher url pub url: String, // gopher url
@ -150,36 +150,26 @@ impl Menu {
let indent = self.indent(); let indent = self.indent();
let left_margin = " ".repeat(indent); let left_margin = " ".repeat(indent);
let mut line_count = 0;
for line in iter { for line in iter {
line_count += 1;
let mut line_size = 0;
if !self.wide { if !self.wide {
out.push_str(&left_margin); out.push_str(&left_margin);
line_size += indent;
} }
if line.typ == Type::Info { if line.typ == Type::Info {
out.push_str(" "); out.push_str(" ");
line_size += 6;
} else { } else {
if line.link == self.link { if line.link == self.link {
out.push_str("\x1b[97;1m*\x1b[0m") out.push_str("\x1b[97;1m*\x1b[0m")
} else { } else {
out.push(' '); out.push(' ');
} }
line_size += 1;
out.push(' '); out.push(' ');
line_size += 1;
out.push_str("\x1b[95m"); out.push_str("\x1b[95m");
if line.link < 9 { if line.link < 9 {
out.push(' '); out.push(' ');
line_size += 1;
} }
let num = (line.link + 1).to_string(); let num = (line.link + 1).to_string();
out.push_str(&num); out.push_str(&num);
line_size += num.len();
out.push_str(".\x1b[0m "); out.push_str(".\x1b[0m ");
line_size += 2;
} }
// truncate long lines, instead of wrapping // truncate long lines, instead of wrapping
@ -201,18 +191,12 @@ impl Menu {
} }
// clear rest of line // clear rest of line
line_size += name.len(); out.push_str(&format!("{}", clear::UntilNewline));
out.push_str(&" ".repeat(cols - line_size)); // fill line
out.push_str("\r\n"); out.push_str("\r\n");
} }
// clear remainder of screen // clear remainder of screen
let blank_line = " ".repeat(cols); out.push_str(&format!("{}", clear::AfterCursor));
for _ in 0..rows - line_count - 1 {
out.push_str(&blank_line);
out.push_str(&"\r\n");
}
out out
} }

@ -1,5 +1,6 @@
use crate::ui::{Action, Key, View, MAX_COLS, SCROLL_LINES}; use crate::ui::{Action, Key, View, MAX_COLS, SCROLL_LINES};
use std::fmt; use std::fmt;
use termion::clear;
pub struct Text { pub struct Text {
url: String, url: String,
@ -104,36 +105,24 @@ impl View for Text {
.skip(self.scroll) .skip(self.scroll)
.take(rows - 1); .take(rows - 1);
let mut lines = 0;
for line in iter { for line in iter {
lines += 1;
if line == ".\r" { if line == ".\r" {
continue; continue;
} }
let mut line_size = 0;
if !self.wide { if !self.wide {
out.push_str(&indent); out.push_str(&indent);
line_size += indent.len();
} }
let line = line.trim_end_matches('\r').replace('\t', " "); let line = line.trim_end_matches('\r').replace('\t', " ");
out.push_str(&line); out.push_str(&line);
line_size += line.len();
// clear rest of line // clear rest of line
if cols > line_size { out.push_str(&format!("{}", clear::UntilNewline));
out.push_str(&" ".repeat(cols - line_size)); // fill line
}
out.push_str("\r\n"); out.push_str("\r\n");
} }
// clear remainder of screen // clear remainder of screen
let blank_line = " ".repeat(cols); out.push_str(&format!("{}", clear::AfterCursor));
for _ in 0..rows - lines - 1 {
out.push_str(&blank_line);
out.push_str(&"\r\n");
}
out out
} }

Loading…
Cancel
Save