page: tidy horizontal scrolling

This commit is contained in:
dece 2021-02-15 18:51:45 +01:00
parent 4d1cc5650f
commit 8cfc266e48
2 changed files with 33 additions and 23 deletions

View file

@ -40,7 +40,7 @@ class Page:
content_position = self.current_line, self.current_column content_position = self.current_line, self.current_column
self.pad.refresh(*content_position, 0, 0, x, y) 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. """Make the content pad scroll up and down by num_lines.
Arguments: Arguments:
@ -63,14 +63,18 @@ class Page:
return True return True
return False return False
def scroll_left(self): def scroll_h(self, num_columns: int, window_width: int =0):
if self.current_column > 0: if num_columns < 0:
self.current_column -= 1 num_columns = -num_columns
return True min_column = 0
return False if self.current_column > min_column:
new_column = self.current_column - num_columns
def scroll_right(self, window_width): self.current_column = max(new_column, min_column)
if self.current_column < Page.MAX_COLS - window_width: return True
self.current_column += 1 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 True
return False return False

View file

@ -76,17 +76,17 @@ class Screen:
elif char == ord("s"): elif char == ord("s"):
self.set_status(f"h {self.h} w {self.w}") self.set_status(f"h {self.h} w {self.w}")
elif char == ord("h"): elif char == ord("h"):
if self.page.scroll_left(): self.scroll_page_horizontally(-1)
self.refresh_page()
elif char == ord("j"): elif char == ord("j"):
if self.page.scroll_v(1, self.h - 2): self.scroll_page_vertically(1)
self.refresh_page()
elif char == ord("k"): elif char == ord("k"):
if self.page.scroll_v(-1, self.h - 2): self.scroll_page_vertically(-1)
self.refresh_page()
elif char == ord("l"): elif char == ord("l"):
if self.page.scroll_right(self.w): self.scroll_page_horizontally(1)
self.refresh_page() 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"): elif char == ord("H"):
self.go_back() self.go_back()
elif curses.ascii.isdigit(char): elif curses.ascii.isdigit(char):
@ -324,11 +324,9 @@ class Screen:
Right now, only vertical scrolling is handled. Right now, only vertical scrolling is handled.
""" """
if bstate & ButtonState.SCROLL_UP: if bstate & ButtonState.SCROLL_UP:
if self.page.scroll_v(-3): self.scroll_page_vertically(-3)
self.refresh_page()
elif bstate & ButtonState.SCROLL_DOWN: elif bstate & ButtonState.SCROLL_DOWN:
if self.page.scroll_v(3, self.h - 2): self.scroll_page_vertically(3)
self.refresh_page()
def handle_resize(self): def handle_resize(self):
"""Try to not make everything collapse on resizes.""" """Try to not make everything collapse on resizes."""
@ -356,6 +354,14 @@ class Screen:
self.screen.refresh() self.screen.refresh()
self.refresh_windows() 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): def go_back(self):
"""Go back in history if possible.""" """Go back in history if possible."""
if self.history: if self.history: