Created
April 30, 2013 21:23
-
-
Save sdressler/5492072 to your computer and use it in GitHub Desktop.
Proposed Multiline Grid Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import npyscreen, curses | |
class DividedMultiLine(npyscreen.wgwidget.Widget): | |
_subwidget = npyscreen.Textfield | |
def __init__(self, screen, values = None, **keywords): | |
super(DividedMultiLine, self).__init__(screen, **keywords) | |
self.values = values | |
column_width = int(self.width / len(self.values[0])) | |
self.rows = [] | |
y_offset = self.rely | |
for row in self.values: | |
x_offset = self.relx | |
cells = [] | |
for item in row: | |
cell = self._subwidget(self.parent, relx=x_offset, rely=y_offset, height=1, width=column_width + 1, value=self.display_value(item, column_width)) | |
cells.append(cell) | |
x_offset += column_width | |
self.rows.append(cells) | |
y_offset += 1 | |
self.highlighted_row = 0 | |
self.highlight_row(0) | |
def update(self, clear=True): | |
if clear: | |
self.clear() | |
for row in self.rows: | |
for cell in row: | |
cell.update() | |
def highlight_row(self, idx, highlight=True): | |
for cell in self.rows[idx]: | |
if highlight == True: | |
cell.highlight = 1 | |
else: | |
cell.highlight = 0 | |
cell.update() | |
''' This returns a zero-padded string to mimic a highlight of the entire line ''' | |
def display_value(self, value, column_width): | |
val_string = str(value) | |
pad = column_width - len(val_string) | |
# This is a bit lazy, I know | |
if pad < 0: | |
raise ValueError("Padding is negative, time for fixing it now!") | |
return val_string + (" " * pad) | |
def set_up_handlers(self): | |
self.handlers = { | |
curses.KEY_UP: self.h_move_line_up, | |
curses.KEY_DOWN: self.h_move_line_down, | |
} | |
def h_move_line_up(self, ch): | |
if self.highlighted_row == 0: | |
return | |
self.highlight_row(self.highlighted_row, False) | |
self.highlighted_row -= 1 | |
self.highlight_row(self.highlighted_row, True) | |
def h_move_line_down(self, ch): | |
if self.highlighted_row == len(self.values) - 1: | |
return | |
self.highlight_row(self.highlighted_row, False) | |
self.highlighted_row += 1 | |
self.highlight_row(self.highlighted_row, True) | |
class MyTestApp(npyscreen.NPSAppManaged): | |
def onStart(self): | |
self.registerForm("MAIN", MainForm()) | |
class MainForm(npyscreen.FormBaseNew): | |
def create(self): | |
self.add(npyscreen.TitleText, name = "Text:", value= "Press Escape to quit application") | |
self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application | |
self.nextrely += 1 | |
self.add(DividedMultiLine, values = [[1,2,3],[4,5,6],[7,8,9]]) | |
def exit_application(self): | |
self.parentApp.setNextForm(None) | |
self.editing = False | |
def main(): | |
TA = MyTestApp() | |
TA.run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment