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
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
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

View file

@ -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: