pull/6/head
dvkt 5 years ago
parent 7d92bec076
commit b72daf2946

@ -7,7 +7,7 @@ pub struct MenuView {
pub input: String, // user's inputted value
pub menu: Menu, // data
pub link: usize, // selected link
pub scroll: i16, // scrolling offset
pub scroll: usize, // scrolling offset
}
pub struct Menu {
@ -25,7 +25,7 @@ pub struct Line {
}
impl View for MenuView {
fn render(&self, cols: u16, rows: u16) -> String {
fn render(&self, cols: usize, rows: usize) -> String {
self.render_lines(cols, rows)
}
@ -56,7 +56,7 @@ impl MenuView {
self.menu.links()
}
fn render_lines(&self, cols: u16, rows: u16) -> String {
fn render_lines(&self, cols: usize, rows: usize) -> String {
let mut out = String::new();
macro_rules! push {
@ -69,16 +69,12 @@ impl MenuView {
}};
}
let iter = self
.lines()
.iter()
.skip(self.scroll as usize)
.take(rows as usize - 1);
let iter = self.lines().iter().skip(self.scroll).take(rows - 1);
let indent = if self.menu.longest > cols as usize {
let indent = if self.menu.longest > cols {
String::from("")
} else {
let left = (cols as usize - self.menu.longest) / 2;
let left = (cols - self.menu.longest) / 2;
if left > 6 {
" ".repeat(left - 6)
} else {
@ -105,8 +101,8 @@ impl MenuView {
out.push_str(".\x1b[0m ");
}
// truncate long lines, instead of wrapping
let name = if line.name.len() + 6 > cols as usize {
&line.name[0..cols as usize - 6]
let name = if line.name.len() + 6 > cols {
&line.name[0..cols - 6]
} else {
&line.name
};
@ -132,7 +128,7 @@ impl MenuView {
}
fn action_page_down(&mut self) -> Action {
if (self.scroll as usize) < self.lines().len() - 15 {
if self.scroll < self.lines().len() - 15 {
self.scroll += 15;
Action::Redraw
} else {
@ -162,7 +158,8 @@ impl MenuView {
}
fn action_down(&mut self) -> Action {
if self.link < self.links().count() - 1 {
let count = self.links().count();
if count > 0 && self.link < count - 1 {
self.link += 1;
Action::Redraw
} else {

@ -3,12 +3,12 @@ use ui::{Action, Key, View};
pub struct TextView {
url: String,
raw: String,
scroll: isize, // offset
lines: isize, // # of lines
scroll: usize, // offset
lines: usize, // # of lines
longest: usize, // longest line
}
const SCROLL_LINES: isize = 15;
const SCROLL_LINES: usize = 15;
impl View for TextView {
fn url(&self) -> String {
@ -67,12 +67,12 @@ impl View for TextView {
}
}
fn render(&self, cols: u16, rows: u16) -> String {
fn render(&self, cols: usize, rows: usize) -> String {
let mut out = String::new();
let indent = if self.longest > cols as usize {
let indent = if self.longest > cols {
String::from("")
} else {
let left = (cols as usize - self.longest) / 2;
let left = (cols - self.longest) / 2;
if left > 6 {
" ".repeat(left - 6)
} else {
@ -82,8 +82,8 @@ impl View for TextView {
let iter = self
.raw
.split_terminator('\n')
.skip(self.scroll as usize)
.take(rows as usize - 1);
.skip(self.scroll)
.take(rows - 1);
for line in iter {
out.push_str(&indent);

@ -31,7 +31,7 @@ pub enum Action {
pub trait View {
fn process_input(&mut self, c: Key) -> Action;
fn render(&self, width: u16, height: u16) -> String;
fn render(&self, width: usize, height: usize) -> String;
fn url(&self) -> String;
}
@ -72,7 +72,7 @@ impl UI {
let (cols, rows) = termion::terminal_size().expect("can't get terminal size"); // TODO
if self.pages.len() > 0 && self.page < self.pages.len() {
if let Some(page) = self.pages.get(self.page) {
return page.render(cols, rows);
return page.render(cols as usize, rows as usize);
}
}
String::from("N/A")

Loading…
Cancel
Save