(Resend from subscribed address)

On Mittwoch 10 Juni 2009, Ian Ward wrote:
> Andreas Klöckner wrote:
> > While I'm at it, two questions for Ian:
> >
> > - I'd love to be able to replace the hardcoded sys.stdout in raw_display.
> > Would you take a patch? (This would allow me to implement an option to
> > hijack sys.stdout. This would get rid of the annoying flicker on every
> > program execution step.)
>
> I encourage patches.  If it's general and useful I might even apply it :-)

Attached. Let me know what you think.

> I wish I knew when I will have enough time to finish the release.  I'm
> starting to think I should scale back what will be going out in 0.9.9 just
> to try to get it out sooner.

I think scaling back might be wise. Nobody is going to be upset if you do two 
more incremental releases rather than one gargantuan one.

Unrelatedly, PuDB has progressed to version 0.91.2, which makes PuDB into a 
much more useful debugger by fixing a large number of issues and adding a few 
features. Feedback would be much appreciated. (And yes, 'easy_install pudb' 
actually works now.)

Andreas
diff -r 25b1d200b2b9 urwid/raw_display.py
--- a/urwid/raw_display.py	Sun Jun 07 12:49:53 2009 -0400
+++ b/urwid/raw_display.py	Wed Jun 10 12:14:31 2009 -0400
@@ -45,7 +45,15 @@
     pass
 
 class Screen(BaseScreen, RealTerminal):
-    def __init__(self):
+    def __init__(self, term_output_file=None, term_input_file=None):
+        """Initialize a screen that directly prints escape codes to an output
+        termainal.
+
+        term_output_file -- a file-like object to which the screen is written. 
+            Defaults to sys.stdout if given as None.
+        term_input_file -- a file-like object from which input is read.
+            Defaults to sys.stdin if given as None.
+        """
         super(Screen, self).__init__()
         self._pal_escape = {}
         signals.connect_signal(self, UPDATE_PALETTE_ENTRY, 
@@ -68,6 +76,16 @@
         self._started = False
         self.bright_is_bold = os.environ.get('TERM',None) != "xterm"
         self._next_timeout = None
+
+        if term_output_file is None:
+            self._term_output_file = sys.stdout
+        else:
+            self._term_output_file = term_output_file
+
+        if term_input_file is None:
+            self._term_input_file = sys.stdin
+        else:
+            self._term_input_file = term_input_file
     
     started = property(lambda self: self._started)
 
@@ -132,7 +150,7 @@
         After calling this function get_input will include mouse
         click events along with keystrokes.
         """
-        sys.stdout.write(escape.MOUSE_TRACKING_ON)
+        self._term_output_file.write(escape.MOUSE_TRACKING_ON)
 
         self._start_gpm_tracking()
     
@@ -157,13 +175,13 @@
         """
         assert not self._started
         if alternate_buffer:
-            sys.stdout.write(escape.SWITCH_TO_ALTERNATE_BUFFER)
+            self._term_output_file.write(escape.SWITCH_TO_ALTERNATE_BUFFER)
             self._rows_used = None
         else:
             self._rows_used = 0
         self._old_termios_settings = termios.tcgetattr(0)
         self.signal_init()
-        tty.setcbreak(sys.stdin.fileno())
+        tty.setcbreak(self._term_input_file.fileno())
         self._alternate_buffer = alternate_buffer
         self._input_iter = self._run_input_iter()
         self._next_timeout = self.max_wait
@@ -192,7 +210,7 @@
         elif self.maxrow is not None:
             move_cursor = escape.set_cursor_position( 
                 0, self.maxrow)
-        sys.stdout.write(self._attrspec_to_escape(AttrSpec('','')) 
+        self._term_output_file.write(self._attrspec_to_escape(AttrSpec('','')) 
             + escape.SI
             + escape.MOUSE_TRACKING_OFF
             + escape.SHOW_CURSOR
@@ -299,7 +317,7 @@
 
         Use this method if you are implementing yout own event loop.
         """
-        fd_list = [sys.stdin.fileno()]
+        fd_list = [self._term_input_file.fileno()]
         if self.gpm_mev is not None:
             fd_list.append(self.gpm_mev.fromchild.fileno())
         return fd_list
@@ -371,7 +389,7 @@
 
     def _wait_for_input_ready(self, timeout):
         ready = None
-        fd_list = [sys.stdin.fileno()]
+        fd_list = [self._term_input_file.fileno()]
         if self.gpm_mev is not None:
             fd_list += [ self.gpm_mev.fromchild ]
         while True:
@@ -396,8 +414,8 @@
         if self.gpm_mev is not None:
             if self.gpm_mev.fromchild.fileno() in ready:
                 self.gpm_event_pending = True
-        if sys.stdin.fileno() in ready:
-            return ord(os.read(sys.stdin.fileno(), 1))
+        if self._term_input_file.fileno() in ready:
+            return ord(os.read(self._term_input_file.fileno(), 1))
         return -1
     
     def _encode_gpm_event( self ):
@@ -480,8 +498,8 @@
         
         while True:
             try:
-                sys.stdout.write(escape.DESIGNATE_G1_SPECIAL)
-                sys.stdout.flush()
+                self._term_output_file.write(escape.DESIGNATE_G1_SPECIAL)
+                self._term_output_file.flush()
                 break
             except IOError, e:
                 pass
@@ -628,12 +646,12 @@
         try:
             k = 0
             for l in o:
-                sys.stdout.write( l )
+                self._term_output_file.write( l )
                 k += len(l)
                 if k > 1024:
-                    sys.stdout.flush()
+                    self._term_output_file.flush()
                     k = 0
-            sys.stdout.flush()
+            self._term_output_file.flush()
         except IOError, e:
             # ignore interrupted syscall
             if e.args[0] != 4:
@@ -805,8 +823,8 @@
 
         modify = ["%d;rgb:%02x/%02x/%02x" % (index, red, green, blue)
             for index, red, green, blue in entries]
-        seq = sys.stdout.write("\x1b]4;"+";".join(modify)+"\x1b\\")
-        sys.stdout.flush()
+        seq = self._term_output_file.write("\x1b]4;"+";".join(modify)+"\x1b\\")
+        self._term_output_file.flush()
 
 
     # shortcut for creating an AttrSpec with this screen object's

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid

Reply via email to