Esempio n. 1
0
 def handleKey(self, key):
   isKeystrokeConsumed = True
   if uiTools.isScrollKey(key):
     pageHeight = self.getPreferredSize()[0] - 1
     newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight, self.lastContentHeight)
     
     if self.scroll != newScroll:
       self.valsLock.acquire()
       self.scroll = newScroll
       self.redraw(True)
       self.valsLock.release()
   elif key in (ord('u'), ord('U')):
     self.valsLock.acquire()
     self.showDuplicates = not self.showDuplicates
     self.redraw(True)
     self.valsLock.release()
   elif key == ord('c') or key == ord('C'):
     msg = "This will clear the log. Are you sure (c again to confirm)?"
     keyPress = popups.showMsg(msg, attr = curses.A_BOLD)
     if keyPress in (ord('c'), ord('C')): self.clear()
   elif key == ord('f') or key == ord('F'):
     # Provides menu to pick regular expression filters or adding new ones:
     # for syntax see: http://docs.python.org/library/re.html#regular-expression-syntax
     options = ["None"] + self.filterOptions + ["New..."]
     oldSelection = 0 if not self.regexFilter else 1
     
     # does all activity under a curses lock to prevent redraws when adding
     # new filters
     panel.CURSES_LOCK.acquire()
     try:
       selection = popups.showMenu("Log Filter:", options, oldSelection)
       
       # applies new setting
       if selection == 0:
         self.setFilter(None)
       elif selection == len(options) - 1:
         # selected 'New...' option - prompt user to input regular expression
         self.showFilterPrompt()
       elif selection != -1:
         self.makeFilterSelection(self.filterOptions[selection - 1])
     finally:
       panel.CURSES_LOCK.release()
     
     if len(self.filterOptions) > MAX_REGEX_FILTERS: del self.filterOptions[MAX_REGEX_FILTERS:]
   elif key == ord('e') or key == ord('E'):
     self.showEventSelectionPrompt()
   elif key == ord('a') or key == ord('A'):
     self.showSnapshotPrompt()
   else: isKeystrokeConsumed = False
   
   return isKeystrokeConsumed
Esempio n. 2
0
 def handleKey(self, key):
   if uiTools.isScrollKey(key):
     pageHeight = self.getPreferredSize()[0] - 1
     newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight, self.lastContentHeight)
     
     if self.scroll != newScroll:
       self.valsLock.acquire()
       self.scroll = newScroll
       self.redraw(True)
       self.valsLock.release()
   elif key in (ord('u'), ord('U')):
     self.valsLock.acquire()
     self.showDuplicates = not self.showDuplicates
     self.redraw(True)
     self.valsLock.release()
 def handleKey(self, key):
   isKeystrokeConsumed = True
   if uiTools.isSelectionKey(key):
     self.prompt()
   elif uiTools.isScrollKey(key) and not self.isInputMode:
     pageHeight = self.getPreferredSize()[0] - 1
     displayLength = len(self.interpretor.getDisplayContents(PROMPT_LINE))
     newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight, displayLength)
     
     if self.scroll != newScroll:
       self.scroll = newScroll
       self.redraw(True)
   else: isKeystrokeConsumed = False
   
   return isKeystrokeConsumed
Esempio n. 4
0
    def handleKey(self, key):
        if uiTools.isScrollKey(key):
            pageHeight = self.getPreferredSize()[0] - 1
            newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight,
                                                  self.lastContentHeight)

            if self.scroll != newScroll:
                self.valsLock.acquire()
                self.scroll = newScroll
                self.redraw(True)
                self.valsLock.release()
        elif key in (ord('u'), ord('U')):
            self.valsLock.acquire()
            self.showDuplicates = not self.showDuplicates
            self.redraw(True)
            self.valsLock.release()
Esempio n. 5
0
 def handleKey(self, key):
   self.valsLock.acquire()
   if uiTools.isScrollKey(key):
     pageHeight = self.getPreferredSize()[0] - 1
     newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight, self._lastContentHeight)
     
     if self.scroll != newScroll:
       self.scroll = newScroll
       self.redraw(True)
   elif key == ord('n') or key == ord('N'):
     self.showLineNum = not self.showLineNum
     self._lastContentHeightArgs = None
     self.redraw(True)
   elif key == ord('s') or key == ord('S'):
     self.stripComments = not self.stripComments
     self._lastContentHeightArgs = None
     self.redraw(True)
   
   self.valsLock.release()
Esempio n. 6
0
    def handleKey(self, key):
        self.valsLock.acquire()
        if uiTools.isScrollKey(key):
            pageHeight = self.getPreferredSize()[0] - 1
            newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight,
                                                  self._lastContentHeight)

            if self.scroll != newScroll:
                self.scroll = newScroll
                self.redraw(True)
        elif key == ord('n') or key == ord('N'):
            self.showLineNum = not self.showLineNum
            self._lastContentHeightArgs = None
            self.redraw(True)
        elif key == ord('s') or key == ord('S'):
            self.stripComments = not self.stripComments
            self._lastContentHeightArgs = None
            self.redraw(True)

        self.valsLock.release()
Esempio n. 7
0
    def handleKey(self, key):
        self.valsLock.acquire()
        isKeystrokeConsumed = True
        if uiTools.isScrollKey(key):
            pageHeight = self.getPreferredSize()[0] - 1
            newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight, self._lastContentHeight)

            if self.scroll != newScroll:
                self.scroll = newScroll
                self.redraw(True)
        elif key == ord("n") or key == ord("N"):
            self.setLineNumberVisible(not self.showLineNum)
        elif key == ord("s") or key == ord("S"):
            self.setCommentsVisible(self.stripComments)
        elif key == ord("r") or key == ord("R"):
            self.reloadTorrc()
        else:
            isKeystrokeConsumed = False

        self.valsLock.release()
        return isKeystrokeConsumed
Esempio n. 8
0
    def handleKey(self, key):
        self.valsLock.acquire()
        isKeystrokeConsumed = True
        if uiTools.isScrollKey(key):
            pageHeight = self.getPreferredSize()[0] - 1
            newScroll = uiTools.getScrollPosition(key, self.scroll, pageHeight,
                                                  self._lastContentHeight)

            if self.scroll != newScroll:
                self.scroll = newScroll
                self.redraw(True)
        elif key == ord('n') or key == ord('N'):
            self.setLineNumberVisible(not self.showLineNum)
        elif key == ord('s') or key == ord('S'):
            self.setCommentsVisible(self.stripComments)
        elif key == ord('r') or key == ord('R'):
            self.reloadTorrc()
        else:
            isKeystrokeConsumed = False

        self.valsLock.release()
        return isKeystrokeConsumed
Esempio n. 9
0
def showConfirmationDialog(torrcContents, torrcLocation):
    """
  Shows a confirmation dialog with the given torrc contents, returning CANCEL,
  NEXT, or BACK based on the selection.
  
  Arguments:
    torrcContents - lines of torrc contents to be presented
    torrcLocation - path where the torrc will be placed
  """

    torrcLines = torrcContents.split("\n")
    options = ["Cancel", "Back to Setup", "Start Tor"]

    control = cli.controller.getController()
    screenHeight = control.getScreen().getmaxyx()[0]
    stickyHeight = sum(
        [stickyPanel.getHeight() for stickyPanel in control.getStickyPanels()])
    isScrollbarVisible = len(torrcLines) + stickyHeight + 5 > screenHeight

    xOffset = 3 if isScrollbarVisible else 0
    popup, width, height = cli.popups.init(len(torrcLines) + 5, 84 + xOffset)
    if not popup: return False

    try:
        scroll, selection = 0, 2
        curses.cbreak()

        while True:
            popup.win.erase()
            popup.win.box()

            # renders the scrollbar
            if isScrollbarVisible:
                popup.addScrollBar(scroll, scroll + height - 5,
                                   len(torrcLines), 1, height - 4, 1)

            # shows the path where the torrc will be placed
            titleMsg = "The following will be placed at '%s':" % torrcLocation
            popup.addstr(0, 0, titleMsg, curses.A_STANDOUT)

            # renders the torrc contents
            for i in range(scroll, min(len(torrcLines), height - 5 + scroll)):
                # parses the argument and comment from options
                option, arg, comment = uiTools.cropStr(torrcLines[i], width -
                                                       4 - xOffset), "", ""

                div = option.find("#")
                if div != -1: option, comment = option[:div], option[div:]

                div = option.strip().find(" ")
                if div != -1: option, arg = option[:div], option[div:]

                drawX = 2 + xOffset
                popup.addstr(i + 1 - scroll, drawX, option,
                             curses.A_BOLD | uiTools.getColor("green"))
                drawX += len(option)
                popup.addstr(i + 1 - scroll, drawX, arg,
                             curses.A_BOLD | uiTools.getColor("cyan"))
                drawX += len(arg)
                popup.addstr(i + 1 - scroll, drawX, comment,
                             uiTools.getColor("white"))

            # divider between the torrc and the options
            popup.addch(height - 4, 0, curses.ACS_LTEE)
            popup.addch(height - 4, width, curses.ACS_RTEE)
            popup.hline(height - 4, 1, width - 1)
            if isScrollbarVisible: popup.addch(height - 4, 2, curses.ACS_BTEE)

            # renders the selection options
            confirmationMsg = "Run tor with the above configuration?"
            popup.addstr(height - 3, width - len(confirmationMsg) - 1,
                         confirmationMsg,
                         uiTools.getColor("green") | curses.A_BOLD)

            drawX = width - 1
            for i in range(len(options) - 1, -1, -1):
                optionLabel = " %s " % options[i]
                drawX -= (len(optionLabel) + 4)

                selectionFormat = curses.A_STANDOUT if i == selection else curses.A_NORMAL
                popup.addstr(height - 2, drawX, "[", uiTools.getColor("green"))
                popup.addstr(
                    height - 2, drawX + 1, optionLabel,
                    uiTools.getColor("green") | selectionFormat
                    | curses.A_BOLD)
                popup.addstr(height - 2, drawX + len(optionLabel) + 1, "]",
                             uiTools.getColor("green"))

                drawX -= 1  # space gap between the options

            popup.win.refresh()
            key = cli.controller.getController().getScreen().getch()

            if key == curses.KEY_LEFT:
                selection = (selection - 1) % len(options)
            elif key == curses.KEY_RIGHT:
                selection = (selection + 1) % len(options)
            elif uiTools.isScrollKey(key):
                scroll = uiTools.getScrollPosition(key, scroll, height - 5,
                                                   len(torrcLines))
            elif uiTools.isSelectionKey(key):
                if selection == 0: return CANCEL
                elif selection == 1: return BACK
                else: return NEXT
            elif key in (27, ord('q'), ord('Q')): return CANCEL
    finally:
        cli.popups.finalize()
Esempio n. 10
0
def showConfirmationDialog(torrcContents, torrcLocation):
  """
  Shows a confirmation dialog with the given torrc contents, returning CANCEL,
  NEXT, or BACK based on the selection.
  
  Arguments:
    torrcContents - lines of torrc contents to be presented
    torrcLocation - path where the torrc will be placed
  """
  
  torrcLines = torrcContents.split("\n")
  options = ["Cancel", "Back to Setup", "Start Tor"]
  
  control = cli.controller.getController()
  screenHeight = control.getScreen().getmaxyx()[0]
  stickyHeight = sum([stickyPanel.getHeight() for stickyPanel in control.getStickyPanels()])
  isScrollbarVisible = len(torrcLines) + stickyHeight + 5 > screenHeight
  
  xOffset = 3 if isScrollbarVisible else 0
  popup, width, height = cli.popups.init(len(torrcLines) + 5, 84 + xOffset)
  if not popup: return False
  
  try:
    scroll, selection = 0, 2
    curses.cbreak()
    
    while True:
      popup.win.erase()
      popup.win.box()
      
      # renders the scrollbar
      if isScrollbarVisible:
        popup.addScrollBar(scroll, scroll + height - 5, len(torrcLines), 1, height - 4, 1)
      
      # shows the path where the torrc will be placed
      titleMsg = "The following will be placed at '%s':" % torrcLocation
      popup.addstr(0, 0, titleMsg, curses.A_STANDOUT)
      
      # renders the torrc contents
      for i in range(scroll, min(len(torrcLines), height - 5 + scroll)):
        # parses the argument and comment from options
        option, arg, comment = uiTools.cropStr(torrcLines[i], width - 4 - xOffset), "", ""
        
        div = option.find("#")
        if div != -1: option, comment = option[:div], option[div:]
        
        div = option.strip().find(" ")
        if div != -1: option, arg = option[:div], option[div:]
        
        drawX = 2 + xOffset
        popup.addstr(i + 1 - scroll, drawX, option, curses.A_BOLD | uiTools.getColor("green"))
        drawX += len(option)
        popup.addstr(i + 1 - scroll, drawX, arg, curses.A_BOLD | uiTools.getColor("cyan"))
        drawX += len(arg)
        popup.addstr(i + 1 - scroll, drawX, comment, uiTools.getColor("white"))
      
      # divider between the torrc and the options
      popup.addch(height - 4, 0, curses.ACS_LTEE)
      popup.addch(height - 4, width, curses.ACS_RTEE)
      popup.hline(height - 4, 1, width - 1)
      if isScrollbarVisible: popup.addch(height - 4, 2, curses.ACS_BTEE)
      
      # renders the selection options
      confirmationMsg = "Run tor with the above configuration?"
      popup.addstr(height - 3, width - len(confirmationMsg) - 1, confirmationMsg, uiTools.getColor("green") | curses.A_BOLD)
      
      drawX = width - 1
      for i in range(len(options) - 1, -1, -1):
        optionLabel = " %s " % options[i]
        drawX -= (len(optionLabel) + 4)
        
        selectionFormat = curses.A_STANDOUT if i == selection else curses.A_NORMAL
        popup.addstr(height - 2, drawX, "[", uiTools.getColor("green"))
        popup.addstr(height - 2, drawX + 1, optionLabel, uiTools.getColor("green") | selectionFormat | curses.A_BOLD)
        popup.addstr(height - 2, drawX + len(optionLabel) + 1, "]", uiTools.getColor("green"))
        
        drawX -= 1 # space gap between the options
      
      popup.win.refresh()
      key = cli.controller.getController().getScreen().getch()
      
      if key == curses.KEY_LEFT:
        selection = (selection - 1) % len(options)
      elif key == curses.KEY_RIGHT:
        selection = (selection + 1) % len(options)
      elif uiTools.isScrollKey(key):
        scroll = uiTools.getScrollPosition(key, scroll, height - 5, len(torrcLines))
      elif uiTools.isSelectionKey(key):
        if selection == 0: return CANCEL
        elif selection == 1: return BACK
        else: return NEXT
      elif key in (27, ord('q'), ord('Q')): return CANCEL
  finally:
    cli.popups.finalize()
Esempio n. 11
0
def showDescriptorPopup(connPanel):
    """
  Presents consensus descriptor in popup window with the following controls:
  Up, Down, Page Up, Page Down - scroll descriptor
  Right, Left - next / previous connection
  Enter, Space, d, D - close popup
  
  Arguments:
    connPanel - connection panel providing the dialog
  """

    # hides the title of the connection panel
    connPanel.setTitleVisible(False)
    connPanel.redraw(True)

    control = cli.controller.getController()
    panel.CURSES_LOCK.acquire()
    isDone = False

    try:
        while not isDone:
            selection = connPanel.getSelection()
            if not selection: break

            fingerprint = selection.foreign.getFingerprint()
            if fingerprint == "UNKNOWN": fingerprint = None

            displayText = getDisplayText(fingerprint)
            displayColor = cli.connections.connEntry.CATEGORY_COLOR[
                selection.getType()]
            showLineNumber = fingerprint != None

            # determines the maximum popup size the displayText can fill
            pHeight, pWidth = getPreferredSize(displayText, connPanel.maxX,
                                               showLineNumber)

            popup, _, height = cli.popups.init(pHeight, pWidth)
            if not popup: break
            scroll, isChanged = 0, True

            try:
                while not isDone:
                    if isChanged:
                        draw(popup, fingerprint, displayText, displayColor,
                             scroll, showLineNumber)
                        isChanged = False

                    key = control.getScreen().getch()

                    if uiTools.isScrollKey(key):
                        # TODO: This is a bit buggy in that scrolling is by displayText
                        # lines rather than the displayed lines, causing issues when
                        # content wraps. The result is that we can't have a scrollbar and
                        # can't scroll to the bottom if there's a multi-line being
                        # displayed. However, trying to correct this introduces a big can
                        # of worms and after hours decided that this isn't worth the
                        # effort...

                        newScroll = uiTools.getScrollPosition(
                            key, scroll, height - 2, len(displayText))

                        if scroll != newScroll:
                            scroll, isChanged = newScroll, True
                    elif uiTools.isSelectionKey(key) or key in (ord('d'),
                                                                ord('D')):
                        isDone = True  # closes popup
                    elif key in (curses.KEY_LEFT, curses.KEY_RIGHT):
                        # navigation - pass on to connPanel and recreate popup
                        connPanel.handleKey(curses.KEY_UP if key == curses.
                                            KEY_LEFT else curses.KEY_DOWN)
                        break
            finally:
                cli.popups.finalize()
    finally:
        connPanel.setTitleVisible(True)
        connPanel.redraw(True)
        panel.CURSES_LOCK.release()
Esempio n. 12
0
def showDescriptorPopup(connPanel):
  """
  Presents consensus descriptor in popup window with the following controls:
  Up, Down, Page Up, Page Down - scroll descriptor
  Right, Left - next / previous connection
  Enter, Space, d, D - close popup
  
  Arguments:
    connPanel - connection panel providing the dialog
  """
  
  # hides the title of the connection panel
  connPanel.setTitleVisible(False)
  connPanel.redraw(True)
  
  control = cli.controller.getController()
  panel.CURSES_LOCK.acquire()
  isDone = False
  
  try:
    while not isDone:
      selection = connPanel.getSelection()
      if not selection: break
      
      fingerprint = selection.foreign.getFingerprint()
      if fingerprint == "UNKNOWN": fingerprint = None
      
      displayText = getDisplayText(fingerprint)
      displayColor = cli.connections.connEntry.CATEGORY_COLOR[selection.getType()]
      showLineNumber = fingerprint != None
      
      # determines the maximum popup size the displayText can fill
      pHeight, pWidth = getPreferredSize(displayText, connPanel.maxX, showLineNumber)
      
      popup, _, height = cli.popups.init(pHeight, pWidth)
      if not popup: break
      scroll, isChanged = 0, True
      
      try:
        while not isDone:
          if isChanged:
            draw(popup, fingerprint, displayText, displayColor, scroll, showLineNumber)
            isChanged = False
          
          key = control.getScreen().getch()
          
          if uiTools.isScrollKey(key):
            # TODO: This is a bit buggy in that scrolling is by displayText
            # lines rather than the displayed lines, causing issues when
            # content wraps. The result is that we can't have a scrollbar and
            # can't scroll to the bottom if there's a multi-line being
            # displayed. However, trying to correct this introduces a big can
            # of worms and after hours decided that this isn't worth the
            # effort...
            
            newScroll = uiTools.getScrollPosition(key, scroll, height - 2, len(displayText))
            
            if scroll != newScroll:
              scroll, isChanged = newScroll, True
          elif uiTools.isSelectionKey(key) or key in (ord('d'), ord('D')):
            isDone = True # closes popup
          elif key in (curses.KEY_LEFT, curses.KEY_RIGHT):
            # navigation - pass on to connPanel and recreate popup
            connPanel.handleKey(curses.KEY_UP if key == curses.KEY_LEFT else curses.KEY_DOWN)
            break
      finally: cli.popups.finalize()
  finally:
    connPanel.setTitleVisible(True)
    connPanel.redraw(True)
    panel.CURSES_LOCK.release()