class ErrorScreen(BaseScreen): """Display a traceback within curses. """ def __init__(self, screen, traceback_): """Takes the screen where the error occured and the traceback. """ self.screen = screen self.colors = screen.colors self.blocks = screen.blocks self.traceback_ = traceback_ self.win = self.screen.win self.win.clear() self.win.refresh() # BaseScreen contracts # ==================== ui_chars = (ord('q'), curses.KEY_UP, curses.KEY_DOWN, curses.KEY_LEFT, curses.KEY_PPAGE, curses.KEY_NPAGE, curses.KEY_BACKSPACE) def resize(self): try: self.lines = format_tb(self.W - 1, self.traceback_) self.area = ScrollArea(self.H, len(self.lines), 0) self.draw() except: logger.critical(traceback.format_exc()) def react(self, c): try: if c in (ord('q'), curses.KEY_BACKSPACE, ascii.BS, ascii.ESC, curses.KEY_LEFT): return self.screen elif c == curses.KEY_UP: # up self.area.move_cursor(0) self.area.scroll(-1) elif c == curses.KEY_DOWN: # down self.area.move_cursor(self.area.numrows - 1) self.area.scroll(1) elif c == curses.KEY_PPAGE: # page up self.area.page_up() elif c == curses.KEY_NPAGE: # page down self.area.page_down() self.draw() except: logger.critical(traceback.format_exc()) # Writes # ====== def draw(self): # Clear the screen and then draw our rows. # ======================================== self.win.clear() self.win.refresh() for index, rownum in self.area: self.win.addstr(rownum, 0, self.lines[index]) # Continuation indicators # ======================= color = self.colors.BLUE if self.area.start > 0: c = curses.ACS_UARROW else: c = ord(' ') self.win.addch(0, self.W, c, color) if self.area.end_ < self.area.numitems: c = curses.ACS_LANTERN else: c = ord(' ') self.win.addch(self.H - 1, self.W, c, color) # Commit # ====== self.win.refresh()
class TwoAndAHalfPageListing(unittest.TestCase): def setUp(self): #wheeee! self.area = ScrollArea(20, 50, 3) self.area.refuse = refuse_pass def testInit(self): expected = (20, 0, 50, 0, 20, 0, [3, 4, 5, 6, 7, 8, 9, 10, 11]) actual = self.area.stat() self.assertEqual(expected, actual) # scroll_one # ========== def testScrollOne(self): self.area.scroll_one() expected = (20, 1, 50, 0, 20, 1, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollDownThenUp(self): self.area.scroll_one() self.area.scroll_one(up=True) expected = (20, 0, 50, 0, 20, 0, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) # scroll # ====== def testScroll(self): self.area.scroll(1) expected = (20, 1, 50, 0, 20, 1, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollToEdgeOfScreen(self): self.area.scroll(19) expected = (20, 19, 50, 0, 20, 19, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollJustPastEdgeOfScreen(self): self.area.scroll(20) expected = (20, 19, 50, 1, 21, 20, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollWellPastEdgeOfScreen(self): self.area.scroll(25) expected = (20, 19, 50, 6, 26, 25, range(5, 14)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollToEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollJustPastEdgeOfList(self): self.area.scroll(51) expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollWellPastEdgeOfList(self): self.area.scroll(1000) expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollTooFarDownTriggersRefusal(self): self.area.refuse = refuse_raise self.assertRaises(DoneScrolling, self.area.scroll, 1000) # scroll up def testScrollAllTheWayDownAndThenUpToEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual) self.area.scroll(-50) expected = (20, 0, 50, 0, 20, 0, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollAllTheWayDownAndThenUpJustPastEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual) self.area.scroll(-51) expected = (20, 0, 50, 0, 20, 0, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollAllTheWayDownAndThenUpWellPastEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual) self.area.scroll(-1000) expected = (20, 0, 50, 0, 20, 0, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollTooFarUpTriggersRefusal(self): self.area.refuse = refuse_raise self.assertRaises(DoneScrolling, self.area.scroll, -1000) # page_down # ========= def testPageDown(self): self.area.page_down() expected = (20, 0, 50, 20, 40, 20, range(11, 20)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageDownFullThenPartial(self): self.area.page_down() self.area.page_down() expected = (20, 0, 50, 40, 50, 40, range(19, 23)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageDownFullThenPartialThenFinal(self): self.area.page_down() self.area.page_down() self.area.page_down() expected = (20, 0, 50, 49, 50, 49, range(22, 23)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageDownCursorStaysPut(self): self.area.cursor = 7 self.area.page_down() expected = (20, 7, 50, 20, 40, 27, range(11, 20)) actual = self.area.stat() self.assertEqual(expected, actual) # page_up # ======= # Each starts by scrolling all the way down. def testPageUp(self): self.area.scroll(50) self.area.page_up() expected = (20, 19, 50, 10, 30, 29, range(7, 16)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageUpFullThenPartial(self): self.area.scroll(50) self.area.page_up() self.area.page_up() #expected = (20, 19, 50, 0, 20, 19, range(3,12)) expected = (20, 0, 50, 0, 20, 0, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageUpCursorStaysPut(self): self.area.scroll(50) self.area.cursor = 7 self.area.page_up() expected = (20, 7, 50, 10, 30, 17, range(7, 16)) actual = self.area.stat() self.assertEqual(expected, actual) # combined # ======== def testPageDownIntoPartialThenUp(self): self.area.page_down() self.area.page_down() self.area.page_up() expected = (20, 0, 50, 20, 40, 20, range(11, 20)) actual = self.area.stat() self.assertEqual(expected, actual) # home/end # ======== def testHome(self): self.area.scroll(50) self.area.home() expected = (20, 0, 50, 0, 20, 0, range(3, 12)) actual = self.area.stat() self.assertEqual(expected, actual) def testEnd(self): self.area.end() expected = (20, 19, 50, 30, 50, 49, range(15, 23)) actual = self.area.stat() self.assertEqual(expected, actual)
class TwoAndAHalfPageListing(unittest.TestCase): def setUp(self): #wheeee! self.area = ScrollArea(20, 50, 3) self.area.refuse = refuse_pass def testInit(self): expected = (20, 0, 50, 0, 20, 0, [3, 4, 5, 6, 7, 8, 9, 10, 11]) actual = self.area.stat() self.assertEqual(expected, actual) # scroll_one # ========== def testScrollOne(self): self.area.scroll_one() expected = (20, 1, 50, 0, 20, 1, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollDownThenUp(self): self.area.scroll_one() self.area.scroll_one(up=True) expected = (20, 0, 50, 0, 20, 0, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) # scroll # ====== def testScroll(self): self.area.scroll(1) expected = (20, 1, 50, 0, 20, 1, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollToEdgeOfScreen(self): self.area.scroll(19) expected = (20, 19, 50, 0, 20, 19, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollJustPastEdgeOfScreen(self): self.area.scroll(20) expected = (20, 19, 50, 1, 21, 20, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollWellPastEdgeOfScreen(self): self.area.scroll(25) expected = (20, 19, 50, 6, 26, 25, range(5,14)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollToEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollJustPastEdgeOfList(self): self.area.scroll(51) expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollWellPastEdgeOfList(self): self.area.scroll(1000) expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollTooFarDownTriggersRefusal(self): self.area.refuse = refuse_raise self.assertRaises(DoneScrolling, self.area.scroll, 1000) # scroll up def testScrollAllTheWayDownAndThenUpToEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual) self.area.scroll(-50) expected = (20, 0, 50, 0, 20, 0, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollAllTheWayDownAndThenUpJustPastEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual) self.area.scroll(-51) expected = (20, 0, 50, 0, 20, 0, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollAllTheWayDownAndThenUpWellPastEdgeOfList(self): self.area.scroll(50) expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual) self.area.scroll(-1000) expected = (20, 0, 50, 0, 20, 0, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testScrollTooFarUpTriggersRefusal(self): self.area.refuse = refuse_raise self.assertRaises(DoneScrolling, self.area.scroll, -1000) # page_down # ========= def testPageDown(self): self.area.page_down() expected = (20, 0, 50, 20, 40, 20, range(11,20)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageDownFullThenPartial(self): self.area.page_down() self.area.page_down() expected = (20, 0, 50, 40, 50, 40, range(19,23)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageDownFullThenPartialThenFinal(self): self.area.page_down() self.area.page_down() self.area.page_down() expected = (20, 0, 50, 49, 50, 49, range(22,23)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageDownCursorStaysPut(self): self.area.cursor = 7 self.area.page_down() expected = (20, 7, 50, 20, 40, 27, range(11,20)) actual = self.area.stat() self.assertEqual(expected, actual) # page_up # ======= # Each starts by scrolling all the way down. def testPageUp(self): self.area.scroll(50) self.area.page_up() expected = (20, 19, 50, 10, 30, 29, range(7,16)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageUpFullThenPartial(self): self.area.scroll(50) self.area.page_up() self.area.page_up() #expected = (20, 19, 50, 0, 20, 19, range(3,12)) expected = (20, 0, 50, 0, 20, 0, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testPageUpCursorStaysPut(self): self.area.scroll(50) self.area.cursor = 7 self.area.page_up() expected = (20, 7, 50, 10, 30, 17, range(7,16)) actual = self.area.stat() self.assertEqual(expected, actual) # combined # ======== def testPageDownIntoPartialThenUp(self): self.area.page_down() self.area.page_down() self.area.page_up() expected = (20, 0, 50, 20, 40, 20, range(11,20)) actual = self.area.stat() self.assertEqual(expected, actual) # home/end # ======== def testHome(self): self.area.scroll(50) self.area.home() expected = (20, 0, 50, 0, 20, 0, range(3,12)) actual = self.area.stat() self.assertEqual(expected, actual) def testEnd(self): self.area.end() expected = (20, 19, 50, 30, 50, 49, range(15,23)) actual = self.area.stat() self.assertEqual(expected, actual)
class ErrorScreen(BaseScreen): """Display a traceback within curses. """ def __init__(self, screen, traceback_): """Takes the screen where the error occured and the traceback. """ self.screen = screen self.colors = screen.colors self.blocks = screen.blocks self.traceback_ = traceback_ self.win = self.screen.win self.win.clear() self.win.refresh() # BaseScreen contracts # ==================== ui_chars = ( ord('q') , curses.KEY_UP , curses.KEY_DOWN , curses.KEY_LEFT , curses.KEY_PPAGE , curses.KEY_NPAGE , curses.KEY_BACKSPACE ) def resize(self): try: self.lines = format_tb(self.W-1, self.traceback_) self.area = ScrollArea(self.H, len(self.lines), 0) self.draw() except: logger.critical(traceback.format_exc()) def react(self, c): try: if c in ( ord('q') , curses.KEY_BACKSPACE , ascii.BS , ascii.ESC , curses.KEY_LEFT ): return self.screen elif c == curses.KEY_UP: # up self.area.move_cursor(0) self.area.scroll(-1) elif c == curses.KEY_DOWN: # down self.area.move_cursor(self.area.numrows-1) self.area.scroll(1) elif c == curses.KEY_PPAGE: # page up self.area.page_up() elif c == curses.KEY_NPAGE: # page down self.area.page_down() self.draw() except: logger.critical(traceback.format_exc()) # Writes # ====== def draw(self): # Clear the screen and then draw our rows. # ======================================== self.win.clear() self.win.refresh() for index, rownum in self.area: self.win.addstr(rownum,0,self.lines[index]) # Continuation indicators # ======================= color = self.colors.BLUE if self.area.start > 0: c = curses.ACS_UARROW else: c = ord(' ') self.win.addch(0,self.W,c,color) if self.area.end_ < self.area.numitems: c = curses.ACS_LANTERN else: c = ord(' ') self.win.addch(self.H-1,self.W,c,color) # Commit # ====== self.win.refresh()