Esempio n. 1
0
  def __init__(self, stdscr, config=None):
    '''Sets up a MainWindow.

    stdscr  -- a curses WindowObject to draw to
    config  -- a ConfigParser.Config object (see docs for possible values)

    '''
    self.logger = logging.getLogger('goldfinch' +
        "." + self.__class__.__name__)
    self.stdscr = stdscr
    self.config = config
    self.mode = 'edit'
    (self.term_height, self.term_width) = self.stdscr.getmaxyx()
    self.statusbar_top = StatusBar(0, self.stdscr)
    self.statusbar_bottom = StatusBar(self.term_height-2, self.stdscr)
    self.pager = Pager(200, self.stdscr, logging.getLogger(\
        'goldfinch' + '.' + Pager.__class__.__name__)) 
Esempio n. 2
0
class MainWindow(object):
  '''Represents the interface on the screen (view)'''
  
  def __init__(self, stdscr, config=None):
    '''Sets up a MainWindow.

    stdscr  -- a curses WindowObject to draw to
    config  -- a ConfigParser.Config object (see docs for possible values)

    '''
    self.logger = logging.getLogger('goldfinch' +
        "." + self.__class__.__name__)
    self.stdscr = stdscr
    self.config = config
    self.mode = 'edit'
    (self.term_height, self.term_width) = self.stdscr.getmaxyx()
    self.statusbar_top = StatusBar(0, self.stdscr)
    self.statusbar_bottom = StatusBar(self.term_height-2, self.stdscr)
    self.pager = Pager(200, self.stdscr, logging.getLogger(\
        'goldfinch' + '.' + Pager.__class__.__name__)) 

  def draw(self):
    '''Draw the various interface components to the screen'''
    self._draw_statusbars()
    self._draw_pager()
    self._draw_inputbox()
    self.stdscr.move(self.term_height-1,\
        0)  # move the cursor to the input box

  def set_mode(self, mode):
    assert mode == 'edit' or mode == 'command'
    self.input_box.mode = mode
    self.statusbar_bottom.add_text('['+mode+']', 'right')
    if mode == 'command':
      curses.curs_set(0) # hide
    else:
      curses.curs_set(1)
    self.logger.debug('set mode to ' + mode)

  def _draw_inputbox(self):
    '''Draws the input box to the main window using a goldfinch.CustomTextbox.
    The input marker is drawn outside the input box.

    '''
    self.input_win = curses.newwin(1, self.term_width, self.term_height-1, 0)
    # create a list of InputHandler objects to handle key press events on the
    # input box
    handlers = []

    #TODO: resize event doesn't seem to get recognised when on a remote
    #      terminal.
    resize_handler = InputHandler(\
        curses.KEY_RESIZE,
        self.draw,
        [],
        ['edit', 'command'])
    handlers.append(resize_handler)

    command_mode = InputHandler(\
        curses.ascii.ESC,
        self.set_mode,
        ['command'],
        ['edit'])
    handlers.append(command_mode)

    edit_mode = InputHandler(\
        ord('i'),
        self.set_mode,
        ['edit'],
        ['command'])
    handlers.append(edit_mode)

    scroll_up = InputHandler(\
        ord('k'),
        self.pager.scroll,
        [-1],
        ['command'])
    handlers.append(scroll_up)

    scroll_down = InputHandler(\
        ord('j'),
        self.pager.scroll,
        [1],
        ['command'])
    handlers.append(scroll_down)

    page_down = InputHandler(\
        curses.KEY_PPAGE,
        self.pager.scroll,
        [-self.term_height-3],  # -3 for 2 status bars + inputbox
        ['edit', 'command'])
    handlers.append(page_down)

    page_up = InputHandler(\
        curses.KEY_NPAGE,
        self.pager.scroll,
        [self.term_height-3], 
        ['edit', 'command'])
    handlers.append(page_up)

    space_page_down = InputHandler(\
        curses.ascii.SP,
        self.pager.scroll,
        [self.term_height-3],
        ['command'])
    handlers.append(space_page_down)

    self.input_box = CustomTextbox(self.input_win,\
         handlers)
    self.input_win.overwrite(self.stdscr)
    self.input_win.refresh()
    self.stdscr.refresh()
  
  def _draw_pager(self):
    '''Draws the main pager area to the screen'''
    self.pager.draw()

  def _draw_statusbars(self):
    self.statusbar_top.draw()
    self.statusbar_bottom.draw()
    self.statusbar_top.add_text('goldfinch ' + GoldFinch.__version__, 'left')
    self.statusbar_bottom.add_text(''.join(['[', self.mode, ']']), 'right')