def draw(self, force=False): """draw everything, and all widgets""" (h, w) = get_real_termial_size() if force or self.__last_size != (h, w): log.msg("App Draw called ", (h, w), self.__last_size) attr = curses.color_pair(1) self._menu.clear() self._menu.refresh() self._menu = curses.newwin(h, w, 0, 0) self._menu.attrset(attr) self._menu.box() self._menu.hline(2, 1, curses.ACS_HLINE, w - 2) self.__draw_menu() self._menu.refresh() # draw children last self.__drawwidgets(force) self.__last_size = (h, w)
def __init__(self, reactor, title="My App", menu={}): """menu -> { 'file':callback, 'view':callback} """ log.startLogging(open("log.log", "w")) self.__reactor = reactor signal(SIGWINCH, self.onResize) # curses.setupterm() self.__first_time_through = True self.__stdscr = curses.initscr() self.__stdscr.keypad(True) self.__stdscr.nodelay(True) y, x = get_real_termial_size() self._menu = curses.newwin(y, x, 0, 0) self.__last_size = (y, x) self.__focus__items = [] self.__in_focus = 0 curses.start_color() # normal colour curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) # focused colour curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK) # lighter curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK) # self.__stdscr.bkgd(' ', curses.color_pair(1)) curses.noecho() curses.cbreak() curses.curs_set(False) self.title = title self.__menu__ = menu self.__key_handler__ = {"menu": {}} # list of widgets self._widgets = {} # refresh self.draw(True) # so we can read stdio self.__reactor.addReader(self)
def __size__(self): # look for springs (-1 means fix to screen dim) # use -1 , fill width, # -2, half of the width ?? y, x = get_real_termial_size() w = x - self.x if self.w < 0 else self.w h = y - self.y if self.h < 0 else self.h if (w, h) != self.__last_size: self.__changed = True self.__last_size = (w, h) self.__max_number_of_displayed_rows__ = h - 2 return h, w
def __size__(self): # look for springs (-1 means fix to screen dim) # use -1 , fill width, # -2, half of the width ?? y, x = get_real_termial_size() w = x - self.x if self.w < 0 else self.w h = y - self.y if self.h < 0 else self.h if (w, h) != self.__last_size: self.__changed = True self.__last_size = (w, h) self.__column_width = self.w / self.dim[0] - 1 # (excluding the '|') self.__row_height = self.h / self.dim[1] - 1 # (excluding the '-') self.__max_number_of_displayed_rows__ = h - 2 self.__max_number_of_displayed_cols__ = w / 4 - 2 return h, w
def __draw_menu(self): position = 2 self.__key_handler__["menu"] = {} for menu, callback in self.__menu__: text = menu.split("&") hot_key = text[1][0] self._menu.addstr(1, position, text[0], curses.A_NORMAL) position += len(text[0]) self._menu.addstr(1, position, hot_key, curses.A_UNDERLINE | curses.A_BOLD) position += 1 self._menu.addstr(1, position, text[1][1:], curses.A_NORMAL) position += len(text[1][1:]) + 2 self.__key_handler__["menu"][ord(hot_key)] = callback # draw app title y, x = get_real_termial_size() middle = (x - position) / 2 self._menu.addstr(1, middle, "=== " + self.title + " ===", curses.A_NORMAL)
def onResize(self, sig, stack): h, w = get_real_termial_size() curses.resizeterm(h, w) self.process_character(curses.KEY_RESIZE)