ui: add horizontal scrolling in pager

It only took what, 3 years?
jmap
Manos Pitsidianakis 2019-11-09 17:45:23 +02:00
parent e600b0252f
commit 1bd343988e
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 42 additions and 42 deletions

View File

@ -277,8 +277,7 @@ pub enum PageMovement {
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct Pager { pub struct Pager {
text: String, text: String,
cursor_pos: usize, cursor: (usize, usize),
max_cursor_pos: Option<usize>,
height: usize, height: usize,
width: usize, width: usize,
dirty: bool, dirty: bool,
@ -314,8 +313,7 @@ impl Pager {
self.height = height; self.height = height;
self.width = width; self.width = width;
self.dirty = true; self.dirty = true;
self.cursor_pos = 0; self.cursor = (0, 0);
self.max_cursor_pos = None;
} }
pub fn from_string( pub fn from_string(
mut text: String, mut text: String,
@ -374,7 +372,7 @@ impl Pager {
}; };
Pager { Pager {
text, text,
cursor_pos: cursor_pos.unwrap_or(0), cursor: (0, cursor_pos.unwrap_or(0)),
height: content.size().1, height: content.size().1,
width: content.size().0, width: content.size().0,
dirty: true, dirty: true,
@ -397,7 +395,7 @@ impl Pager {
Pager::print_string(&mut content, lines); Pager::print_string(&mut content, lines);
Pager { Pager {
text: text.to_string(), text: text.to_string(),
cursor_pos: cursor_pos.unwrap_or(0), cursor: (0, cursor_pos.unwrap_or(0)),
height, height,
width, width,
dirty: true, dirty: true,
@ -410,7 +408,7 @@ impl Pager {
let (width, height) = content.size(); let (width, height) = content.size();
Pager { Pager {
text: String::new(), text: String::new(),
cursor_pos: cursor_pos.unwrap_or(0), cursor: (0, cursor_pos.unwrap_or(0)),
height, height,
width, width,
dirty: true, dirty: true,
@ -434,7 +432,7 @@ impl Pager {
} }
} }
pub fn cursor_pos(&self) -> usize { pub fn cursor_pos(&self) -> usize {
self.cursor_pos self.cursor.1
} }
} }
@ -453,18 +451,18 @@ impl Component for Pager {
if let Some(mvm) = self.movement.take() { if let Some(mvm) = self.movement.take() {
match mvm { match mvm {
PageMovement::PageUp => { PageMovement::PageUp => {
self.cursor_pos = self.cursor_pos.saturating_sub(height); self.cursor.1 = self.cursor.1.saturating_sub(height);
} }
PageMovement::PageDown => { PageMovement::PageDown => {
if self.cursor_pos + height < self.height { if self.cursor.1 + height < self.height {
self.cursor_pos += height; self.cursor.1 += height;
} }
} }
PageMovement::Home => { PageMovement::Home => {
self.cursor_pos = 0; self.cursor.1 = 0;
} }
PageMovement::End => { PageMovement::End => {
self.cursor_pos = (self.height / height) * height; self.cursor.1 = (self.height / height) * height;
} }
} }
} }
@ -474,29 +472,26 @@ impl Component for Pager {
} }
clear_area(grid, area); clear_area(grid, area);
//let pager_context: usize = context.settings.pager.pager_context; let (width, height) = self.content.size();
//let pager_stop: bool = context.settings.pager.pager_stop; let (cols, rows) = (width!(area), height!(area));
//let rows = y(bottom_right) - y(upper_left); self.cursor = (
//let page_length = rows / self.height; std::cmp::min(width.saturating_sub(cols), self.cursor.0),
let width = width!(area); std::cmp::min(height.saturating_sub(rows), self.cursor.1),
if width != self.width { );
// Reflow text. copy_area(
let lines: Vec<&str> = word_break_string(self.text.as_str(), width);
let height = lines.len() + 1;
self.width = width;
self.height = height;
self.content =
CellBuffer::new_with_context(width, height, Cell::with_char(' '), context);
Pager::print_string(&mut self.content, lines);
}
if self.cursor_pos + height >= self.height {
self.cursor_pos = self.height.saturating_sub(height);
};
copy_area_with_break(
grid, grid,
&self.content, &self.content,
area, area,
((0, self.cursor_pos), (self.width - 1, self.height - 1)), (
(
std::cmp::min((width - 1).saturating_sub(cols), self.cursor.0),
std::cmp::min((height - 1).saturating_sub(rows), self.cursor.1),
),
(
std::cmp::min(self.cursor.0 + cols, width - 1),
std::cmp::min(self.cursor.1 + rows, height - 1),
),
),
); );
context.dirty_areas.push_back(area); context.dirty_areas.push_back(area);
} }
@ -504,17 +499,23 @@ impl Component for Pager {
let shortcuts = &self.get_shortcuts(context)[Self::DESCRIPTION]; let shortcuts = &self.get_shortcuts(context)[Self::DESCRIPTION];
match event { match event {
UIEvent::Input(ref key) if *key == shortcuts["scroll_up"] => { UIEvent::Input(ref key) if *key == shortcuts["scroll_up"] => {
if self.cursor_pos > 0 { self.cursor.1 = self.cursor.1.saturating_sub(1);
self.cursor_pos -= 1; self.dirty = true;
self.dirty = true;
}
return true; return true;
} }
UIEvent::Input(ref key) if *key == shortcuts["scroll_down"] => { UIEvent::Input(ref key) if *key == shortcuts["scroll_down"] => {
if self.height > 0 && self.cursor_pos + 1 < self.height { self.cursor.1 = self.cursor.1 + 1;
self.cursor_pos += 1; self.dirty = true;
self.dirty = true; return true;
} }
UIEvent::Input(Key::Left) => {
self.cursor.0 = self.cursor.0.saturating_sub(1);
self.dirty = true;
return true;
}
UIEvent::Input(Key::Right) => {
self.cursor.0 = self.cursor.0 + 1;
self.dirty = true;
return true; return true;
} }
UIEvent::Input(ref key) if *key == shortcuts["page_up"] => { UIEvent::Input(ref key) if *key == shortcuts["page_up"] => {
@ -567,7 +568,6 @@ impl Component for Pager {
} }
UIEvent::Resize => { UIEvent::Resize => {
self.dirty = true; self.dirty = true;
self.max_cursor_pos = None;
} }
_ => {} _ => {}
} }