[or-cvs] r24243: {arm} Removing the last remaining unsafe usage of the raw subwindo (in arm/trunk/src: interface interface/connections interface/graphing util)

Damian Johnson atagar1 at gmail.com
Sat Feb 19 23:25:09 UTC 2011


Author: atagar
Date: 2011-02-19 23:25:09 +0000 (Sat, 19 Feb 2011)
New Revision: 24243

Modified:
   arm/trunk/src/interface/configPanel.py
   arm/trunk/src/interface/connPanel.py
   arm/trunk/src/interface/connections/connPanel.py
   arm/trunk/src/interface/controller.py
   arm/trunk/src/interface/graphing/graphPanel.py
   arm/trunk/src/interface/headerPanel.py
   arm/trunk/src/interface/logPanel.py
   arm/trunk/src/interface/torrcPanel.py
   arm/trunk/src/util/panel.py
   arm/trunk/src/util/uiTools.py
Log:
Removing the last remaining unsafe usage of the raw subwindow. Abstracting it to the panel also leads to cleaner code.



Modified: arm/trunk/src/interface/configPanel.py
===================================================================
--- arm/trunk/src/interface/configPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/configPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -248,7 +248,7 @@
       self.redraw(True)
     self.valsLock.release()
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     self.valsLock.acquire()
     
     # draws the top label
@@ -313,7 +313,7 @@
     # This is a solid border unless the scrollbar is visible, in which case a
     # 'T' pipe connects the border to the bar.
     uiTools.drawBox(self, 0, 0, width, detailPanelHeight)
-    if width >= 2 and isScrollbarVisible: self.win.addch(detailPanelHeight, 1, curses.ACS_TTEE)
+    if isScrollbarVisible: self.addch(detailPanelHeight, 1, curses.ACS_TTEE)
     
     selectionFormat = curses.A_BOLD | uiTools.getColor(CATEGORY_COLOR[cursorSelection.get(Field.CATEGORY)])
     

Modified: arm/trunk/src/interface/connPanel.py
===================================================================
--- arm/trunk/src/interface/connPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/connPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -508,7 +508,7 @@
     else: return # skip following redraw
     self.redraw(True)
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     self.connectionsLock.acquire()
     try:
       # hostnames frequently get updated so frequent sorting needed
@@ -529,7 +529,7 @@
         if self.showingDetails:
           listingHeight -= 8
           isScrollBarVisible = len(self.connections) > height - 9
-          if width > 80: subwindow.hline(8, 80, curses.ACS_HLINE, width - 81)
+          if width > 80: self.win.hline(8, 80, curses.ACS_HLINE, width - 81)
         else:
           isScrollBarVisible = len(self.connections) > height - 1
         xOffset = 3 if isScrollBarVisible else 0 # content offset for scroll bar

Modified: arm/trunk/src/interface/connections/connPanel.py
===================================================================
--- arm/trunk/src/interface/connections/connPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/connections/connPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -97,7 +97,7 @@
         self.redraw(True)
         lastDraw += 1
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     self.valsLock.acquire()
     
     # title label with connection counts

Modified: arm/trunk/src/interface/controller.py
===================================================================
--- arm/trunk/src/interface/controller.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/controller.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -85,7 +85,7 @@
     self.msgText = msgText
     self.msgAttr = msgAttr
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     msgText = self.msgText
     msgAttr = self.msgAttr
     barTab = 2                # space between msgText and progress bar

Modified: arm/trunk/src/interface/graphing/graphPanel.py
===================================================================
--- arm/trunk/src/interface/graphing/graphPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/graphing/graphPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -275,7 +275,7 @@
     
     self.graphHeight = max(MIN_GRAPH_HEIGHT, newGraphHeight)
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     """ Redraws graph panel """
     
     if self.currentDisplay:

Modified: arm/trunk/src/interface/headerPanel.py
===================================================================
--- arm/trunk/src/interface/headerPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/headerPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -95,7 +95,7 @@
     if self.vals["tor/orPort"]: return 4 if isWide else 6
     else: return 3 if isWide else 4
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     self.valsLock.acquire()
     isWide = width + 1 >= MIN_DUAL_COL_WIDTH
     

Modified: arm/trunk/src/interface/logPanel.py
===================================================================
--- arm/trunk/src/interface/logPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/logPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -778,7 +778,7 @@
       self.redraw(True)
       self.valsLock.release()
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     """
     Redraws message log. Entries stretch to use available space and may
     contain up to two lines. Starts with newest entries.
@@ -831,23 +831,22 @@
         # bottom of the divider
         if seenFirstDateDivider:
           if lineCount >= 1 and lineCount < height and showDaybreaks:
-            self.win.vline(lineCount, dividerIndent, curses.ACS_LLCORNER | dividerAttr, 1)
-            self.win.hline(lineCount, dividerIndent + 1, curses.ACS_HLINE | dividerAttr, width - dividerIndent - 1)
-            self.win.vline(lineCount, width, curses.ACS_LRCORNER | dividerAttr, 1)
+            self.addch(lineCount, dividerIndent, curses.ACS_LLCORNER,  dividerAttr)
+            self.hline(lineCount, dividerIndent + 1, width - dividerIndent - 1, dividerAttr)
+            self.addch(lineCount, width, curses.ACS_LRCORNER, dividerAttr)
           
           lineCount += 1
         
         # top of the divider
         if lineCount >= 1 and lineCount < height and showDaybreaks:
           timeLabel = time.strftime(" %B %d, %Y ", time.localtime(entry.timestamp))
-          self.win.vline(lineCount, dividerIndent, curses.ACS_ULCORNER | dividerAttr, 1)
-          self.win.hline(lineCount, dividerIndent + 1, curses.ACS_HLINE | dividerAttr, 1)
+          self.addch(lineCount, dividerIndent, curses.ACS_ULCORNER, dividerAttr)
+          self.addch(lineCount, dividerIndent + 1, curses.ACS_HLINE, dividerAttr)
           self.addstr(lineCount, dividerIndent + 2, timeLabel, curses.A_BOLD | dividerAttr)
           
-          if dividerIndent + len(timeLabel) + 2 <= width:
-            lineLength = width - dividerIndent - len(timeLabel) - 2
-            self.win.hline(lineCount, dividerIndent + len(timeLabel) + 2, curses.ACS_HLINE | dividerAttr, lineLength)
-            self.win.vline(lineCount, dividerIndent + len(timeLabel) + 2 + lineLength, curses.ACS_URCORNER | dividerAttr, 1)
+          lineLength = width - dividerIndent - len(timeLabel) - 2
+          self.hline(lineCount, dividerIndent + len(timeLabel) + 2, lineLength, dividerAttr)
+          self.addch(lineCount, dividerIndent + len(timeLabel) + 2 + lineLength, curses.ACS_URCORNER, dividerAttr)
         
         seenFirstDateDivider = True
         lineCount += 1
@@ -886,8 +885,8 @@
           
           if drawLine < height and drawLine >= 1:
             if seenFirstDateDivider and width - dividerIndent >= 3 and showDaybreaks:
-              self.win.vline(drawLine, dividerIndent, curses.ACS_VLINE | dividerAttr, 1)
-              self.win.vline(drawLine, width, curses.ACS_VLINE | dividerAttr, 1)
+              self.addch(drawLine, dividerIndent, curses.ACS_VLINE, dividerAttr)
+              self.addch(drawLine, width, curses.ACS_VLINE, dividerAttr)
             
             self.addstr(drawLine, cursorLoc, msg, format)
           
@@ -902,13 +901,9 @@
       # if this is the last line and there's room, then draw the bottom of the divider
       if not deduplicatedLog and seenFirstDateDivider:
         if lineCount < height and showDaybreaks:
-          # when resizing with a small width the following entries can be
-          # problematc (though I'm not sure why)
-          try:
-            self.win.vline(lineCount, dividerIndent, curses.ACS_LLCORNER | dividerAttr, 1)
-            self.win.hline(lineCount, dividerIndent + 1, curses.ACS_HLINE | dividerAttr, width - dividerIndent - 1)
-            self.win.vline(lineCount, width, curses.ACS_LRCORNER | dividerAttr, 1)
-          except: pass
+          self.addch(lineCount, dividerIndent, curses.ACS_LLCORNER, dividerAttr)
+          self.hline(lineCount, dividerIndent + 1, width - dividerIndent - 1, dividerAttr)
+          self.addch(lineCount, width, curses.ACS_LRCORNER, dividerAttr)
         
         lineCount += 1
     

Modified: arm/trunk/src/interface/torrcPanel.py
===================================================================
--- arm/trunk/src/interface/torrcPanel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/interface/torrcPanel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -60,7 +60,7 @@
     
     self.valsLock.release()
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     self.valsLock.acquire()
     
     # If true, we assume that the cached value in self._lastContentHeight is

Modified: arm/trunk/src/util/panel.py
===================================================================
--- arm/trunk/src/util/panel.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/util/panel.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -188,7 +188,7 @@
     if setWidth != -1: newWidth = min(newWidth, setWidth)
     return (newHeight, newWidth)
   
-  def draw(self, subwindow, width, height):
+  def draw(self, width, height):
     """
     Draws display's content. This is meant to be overwritten by 
     implementations and not called directly (use redraw() instead). The
@@ -196,10 +196,8 @@
     a column less than the actual space.
     
     Arguments:
-      sudwindow - panel's current subwindow instance, providing raw access to
-                  its curses functions
-      width     - horizontal space available for content
-      height    - vertical space available for content
+      width  - horizontal space available for content
+      height - vertical space available for content
     """
     
     pass
@@ -243,12 +241,12 @@
     try:
       if forceRedraw:
         self.win.erase() # clears any old contents
-        self.draw(self.win, self.maxX - 1, self.maxY)
+        self.draw(self.maxX - 1, self.maxY)
       self.win.refresh()
     finally:
       CURSES_LOCK.release()
   
-  def hline(self, y, x, length):
+  def hline(self, y, x, length, attr=curses.A_NORMAL):
     """
     Draws a horizontal line. This should only be called from the context of a
     panel's draw method.
@@ -257,17 +255,18 @@
       y      - vertical location
       x      - horizontal location
       length - length the line spans
+      attr   - text attributes
     """
     
     if self.win and self.maxX > x and self.maxY > y:
       try:
         drawLength = min(length, self.maxX - x)
-        self.win.hline(y, x, curses.ACS_HLINE, drawLength)
+        self.win.hline(y, x, curses.ACS_HLINE | attr, drawLength)
       except:
         # in edge cases drawing could cause a _curses.error
         pass
   
-  def vline(self, y, x, length):
+  def vline(self, y, x, length, attr=curses.A_NORMAL):
     """
     Draws a vertical line. This should only be called from the context of a
     panel's draw method.
@@ -276,16 +275,36 @@
       y      - vertical location
       x      - horizontal location
       length - length the line spans
+      attr   - text attributes
     """
     
     if self.win and self.maxX > x and self.maxY > y:
       try:
         drawLength = min(length, self.maxY - y)
-        self.win.vline(y, x, curses.ACS_VLINE, drawLength)
+        self.win.vline(y, x, curses.ACS_VLINE | attr, drawLength)
       except:
         # in edge cases drawing could cause a _curses.error
         pass
   
+  def addch(self, y, x, char, attr=curses.A_NORMAL):
+    """
+    Draws a single character. This should only be called from the context of a
+    panel's draw method.
+    
+    Arguments:
+      y    - vertical location
+      x    - horizontal location
+      char - character to be drawn
+      attr - text attributes
+    """
+    
+    if self.win and self.maxX > x and self.maxY > y:
+      try:
+        self.win.addch(y, x, char, attr)
+      except:
+        # in edge cases drawing could cause a _curses.error
+        pass
+  
   def addstr(self, y, x, msg, attr=curses.A_NORMAL):
     """
     Writes string to subwindow if able. This takes into account screen bounds

Modified: arm/trunk/src/util/uiTools.py
===================================================================
--- arm/trunk/src/util/uiTools.py	2011-02-19 23:18:21 UTC (rev 24242)
+++ arm/trunk/src/util/uiTools.py	2011-02-19 23:25:09 UTC (rev 24243)
@@ -194,7 +194,7 @@
   if getRemainder: return (returnMsg, remainder)
   else: return returnMsg
 
-def drawBox(panel, top, left, width, height):
+def drawBox(panel, top, left, width, height, attr=curses.A_NORMAL):
   """
   Draws a box in the panel with the given bounds.
   
@@ -204,33 +204,22 @@
     left   - horizontal position of the box's left side
     width  - width of the drawn box
     height - height of the drawn box
+    attr   - text attributes
   """
   
-  panelHeight, panelWidth = panel.getPreferredSize()
-  
-  # checks if there's nothing to display
-  if left > panelWidth or top > panelHeight: return
-  
   # draws the top and bottom
-  panel.hline(top, left + 1, width - 1)
-  panel.hline(top + height, left + 1, width - 1)
+  panel.hline(top, left + 1, width - 1, attr)
+  panel.hline(top + height, left + 1, width - 1, attr)
   
   # draws the left and right sides
-  panel.vline(top + 1, left, height - 1)
-  panel.vline(top + 1, left + width, height - 1)
+  panel.vline(top + 1, left, height - 1, attr)
+  panel.vline(top + 1, left + width, height - 1, attr)
   
   # draws the corners
-  corners = ((0, 0, curses.ACS_ULCORNER),
-             (0, 1, curses.ACS_URCORNER),
-             (1, 0, curses.ACS_LLCORNER),
-             (1, 1, curses.ACS_LRCORNER))
-  
-  for isBottom, isRight, cornerType in corners:
-    yLoc = top + height * isBottom
-    xLoc = left + width * isRight
-    
-    if yLoc < panelHeight and xLoc < panelWidth:
-      panel.win.addch(yLoc, xLoc, cornerType)
+  panel.addch(top, left, curses.ACS_ULCORNER, attr)
+  panel.addch(top, left + width, curses.ACS_URCORNER, attr)
+  panel.addch(top + height, left, curses.ACS_LLCORNER, attr)
+  panel.addch(top + height, left + width, curses.ACS_LRCORNER, attr)
 
 def isSelectionKey(key):
   """



More information about the tor-commits mailing list