Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle terminal resizing #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions gitbrowse/ui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import curses
import signal
from curses.textpad import Textbox
from curses import ascii

Expand Down Expand Up @@ -71,6 +72,16 @@ def handle_input(self, mode, data):
def __init__(self):
self.scroll_line = 0
self._highlight_line = 0
signal.signal(signal.SIGWINCH, self.sigwinch_handler)

def sigwinch_handler(self, signum, frame):
self.window_resized()

def window_resized(self):
self._teardown_curses()
self._setup_curses()
self._draw()
curses.doupdate()

@property
def highlight_line(self):
Expand All @@ -92,7 +103,7 @@ def highlight_line(self, value):
self._highlight_line = value

# Ensure highlighted line is visible
if value > self.scroll_line + curses.LINES - 3:
if value > self.scroll_line + self.console_height() - 3:
max_scroll_line = self._max_scroll_line()
self.scroll_line = min(self.scroll_line + delta, max_scroll_line)
elif self.highlight_line < self.scroll_line:
Expand Down Expand Up @@ -169,17 +180,21 @@ def _setup_curses(self):
self.INV_GREEN = curses.color_pair(4)
self.INV_YELLOW = curses.color_pair(5)

w = curses.COLS
h = curses.LINES
w = curses.tigetnum("cols")
h = curses.tigetnum("lines")

self.content_win = self.screen.subwin(h-1, w, 0, 0)
self.status_win = self.screen.subwin(1, w, h-2, 0)
self.mode_win = self.screen.subwin(1, 2, h-1, 0)
self.command_win = self.screen.subwin(1, w-1, h-1, 1)

self.command_input = ModalTextbox(self.command_win, delegate=self)
for trigger, name in self.get_modes().items():
self.command_input.add_mode(name, trigger)
if hasattr(self,'command_input'):
self.command_input.win.keypad(0)
self.command_input.win = self.command_win
self.command_win.keypad(1)
else:
self.command_input = ModalTextbox(self.command_win, delegate=self)
for trigger, name in self.get_modes().items():
self.command_input.add_mode(name, trigger)

def _teardown_curses(self):
curses.nocbreak()
Expand All @@ -190,7 +205,8 @@ def _teardown_curses(self):
def _draw(self):
self.content_win.clear()
start = self.scroll_line
stop = self.scroll_line + curses.LINES - 2
# stop = self.scroll_line + self.console_height() - 2
stop = self.scroll_line + self.content_win.getmaxyx()[0] - 1
for row, line in enumerate(self.content()[start:stop]):
highlight = (row + start == self.highlight_line)
self.draw_content_line(line, row, self.content_win, highlight)
Expand All @@ -210,6 +226,9 @@ def _draw(self):
self.command_win.noutrefresh()
curses.doupdate()

def console_height(self):
return curses.tigetnum("lines")

def content(self):
"""
Override this method to provide content. It should return a list of
Expand Down Expand Up @@ -289,12 +308,12 @@ def down(self, lines=1):

@key_bindings('d')
def half_page_down(self, times=1):
half_page = (curses.LINES - 2) / 2
half_page = (self.console_height() - 2) / 2
self.down(half_page * times)

@key_bindings('f', ' ', 'z', curses.KEY_NPAGE)
def page_down(self, times=1):
page = curses.LINES - 2
page = self.console_height() - 2
self.down(page * times)

@key_bindings('k', 'y', curses.KEY_UP)
Expand All @@ -307,12 +326,12 @@ def up(self, lines=1):

@key_bindings('u')
def half_page_up(self, times=1):
half_page = (curses.LINES - 2) / 2
half_page = (self.console_height() - 2) / 2
self.up(half_page * times)

@key_bindings('b', 'w', curses.KEY_PPAGE)
def page_up(self, times=1):
page = curses.LINES - 2
page = self.console_height() - 2
self.up(page * times)

@key_bindings('g', '<', curses.KEY_HOME)
Expand All @@ -326,7 +345,7 @@ def end(self, times=None):
self.highlight_line = self.content_length() - 1

def _max_scroll_line(self):
return self.content_length() - curses.LINES + 2
return self.content_length() - self.console_height() + 2


class ModalTextbox(Textbox, object):
Expand Down Expand Up @@ -431,6 +450,9 @@ def _transform_input_key(self, key):
return key

def _process_key(self, key):
if key == -1: # terminal resize
self.clear()
return None
if self.mode == self.DEFAULT_MODE:
if ord('0') <= key <= ord('9') or key in self.EDIT_KEYS:
return self._transform_input_key(key)
Expand Down