From 4ed285479149811ad8449a4e9dd034c07846651f Mon Sep 17 00:00:00 2001 From: pagedown Date: Tue, 29 Mar 2022 16:42:29 +0800 Subject: [PATCH] Add a new standalone action scroll_prompt_to_top Refactor clear_terminal to move the functions to Window. Fix the action not scrolling to the bottom when the screen is scrolled. --- kitty/boss.py | 28 +++++++++++++++------------- kitty/window.py | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/kitty/boss.py b/kitty/boss.py index c4d6104d0..c4f888572 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -815,19 +815,21 @@ class Boss: windows.append(w) else: windows = list(self.all_windows) - reset = action == 'reset' - how = 3 if action == 'scrollback' else 2 - for w in windows: - if action in ('to_cursor', 'scroll'): - w.screen.scroll_until_cursor_prompt() - if action == 'to_cursor': - w.screen.clear_scrollback() - continue - w.screen.cursor.x = w.screen.cursor.y = 0 - if reset: - w.screen.reset() - else: - w.screen.erase_in_display(how, False) + if action == 'reset': + for w in windows: + w.clear_screen(reset=True, scrollback=True) + elif action == 'scrollback': + for w in windows: + w.clear_screen(scrollback=True) + elif action == 'clear': + for w in windows: + w.clear_screen() + elif action == 'scroll': + for w in windows: + w.scroll_prompt_to_top() + elif action == 'to_cursor': + for w in windows: + w.scroll_prompt_to_top(clear_scrollback=True) def increase_font_size(self) -> None: # legacy cfs = global_font_size() diff --git a/kitty/window.py b/kitty/window.py index c9fc1aa40..5ecefb2a5 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -1308,6 +1308,13 @@ class Window: text = text.replace(b'\r\n', b'\n').replace(b'\n', b'\r') self.screen.paste(text) + def clear_screen(self, reset: bool = False, scrollback: bool = False) -> None: + self.screen.cursor.x = self.screen.cursor.y = 0 + if reset: + self.screen.reset() + else: + self.screen.erase_in_display(3 if scrollback else 2, False) + # actions {{{ @ac('cp', 'Show scrollback in a pager like less') @@ -1445,7 +1452,16 @@ class Window: if self.screen.is_main_linebuf(): self.screen.scroll_to_prompt(num_of_prompts) - @ac('sc', 'Scroll prompt to the bottom of the screen, filling in extra lines form the scrollback buffer') + @ac('sc', 'Scroll prompt to the top of the screen, filling screen with empty lines') + def scroll_prompt_to_top(self, clear_scrollback: bool = False) -> None: + if self.screen.is_main_linebuf(): + self.screen.scroll_until_cursor_prompt() + if clear_scrollback: + self.screen.clear_scrollback() + elif self.screen.scrolled_by > 0: + self.screen.scroll(SCROLL_FULL, False) + + @ac('sc', 'Scroll prompt to the bottom of the screen, filling in extra lines from the scrollback buffer') def scroll_prompt_to_bottom(self) -> None: self.screen.scroll_prompt_to_bottom()