From 8cfc266e4873446c46ad10fc25141a5a0dfcc89e Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 15 Feb 2021 18:51:45 +0100 Subject: [PATCH] page: tidy horizontal scrolling --- bebop/page.py | 26 +++++++++++++++----------- bebop/screen.py | 30 ++++++++++++++++++------------ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/bebop/page.py b/bebop/page.py index 678290d..2de27d0 100644 --- a/bebop/page.py +++ b/bebop/page.py @@ -40,7 +40,7 @@ class Page: content_position = self.current_line, self.current_column self.pad.refresh(*content_position, 0, 0, x, y) - def scroll_v(self, num_lines: int, window_height: int =None): + def scroll_v(self, num_lines: int, window_height: int =0): """Make the content pad scroll up and down by num_lines. Arguments: @@ -63,14 +63,18 @@ class Page: return True return False - def scroll_left(self): - if self.current_column > 0: - self.current_column -= 1 - return True - return False - - def scroll_right(self, window_width): - if self.current_column < Page.MAX_COLS - window_width: - self.current_column += 1 - return True + def scroll_h(self, num_columns: int, window_width: int =0): + if num_columns < 0: + num_columns = -num_columns + min_column = 0 + if self.current_column > min_column: + new_column = self.current_column - num_columns + self.current_column = max(new_column, min_column) + return True + else: + max_column = self.dim[1] - window_width + if self.current_column < max_column: + new_column = self.current_column + num_columns + self.current_column = min(new_column, max_column) + return True return False diff --git a/bebop/screen.py b/bebop/screen.py index 104184d..200540f 100644 --- a/bebop/screen.py +++ b/bebop/screen.py @@ -76,17 +76,17 @@ class Screen: elif char == ord("s"): self.set_status(f"h {self.h} w {self.w}") elif char == ord("h"): - if self.page.scroll_left(): - self.refresh_page() + self.scroll_page_horizontally(-1) elif char == ord("j"): - if self.page.scroll_v(1, self.h - 2): - self.refresh_page() + self.scroll_page_vertically(1) elif char == ord("k"): - if self.page.scroll_v(-1, self.h - 2): - self.refresh_page() + self.scroll_page_vertically(-1) elif char == ord("l"): - if self.page.scroll_right(self.w): - self.refresh_page() + self.scroll_page_horizontally(1) + elif char == ord("f"): + self.scroll_page_vertically(self.page_pad_size[0]) + elif char == ord("b"): + self.scroll_page_vertically(-self.page_pad_size[0]) elif char == ord("H"): self.go_back() elif curses.ascii.isdigit(char): @@ -324,11 +324,9 @@ class Screen: Right now, only vertical scrolling is handled. """ if bstate & ButtonState.SCROLL_UP: - if self.page.scroll_v(-3): - self.refresh_page() + self.scroll_page_vertically(-3) elif bstate & ButtonState.SCROLL_DOWN: - if self.page.scroll_v(3, self.h - 2): - self.refresh_page() + self.scroll_page_vertically(3) def handle_resize(self): """Try to not make everything collapse on resizes.""" @@ -356,6 +354,14 @@ class Screen: self.screen.refresh() self.refresh_windows() + def scroll_page_vertically(self, by_lines: int): + if self.page.scroll_v(by_lines, self.h - 2): + self.refresh_page() + + def scroll_page_horizontally(self, by_columns: int): + if self.page.scroll_h(by_columns, self.w): + self.refresh_page() + def go_back(self): """Go back in history if possible.""" if self.history: