Release notes: Turn the 0.15-rc1 release notes into 0.15 release notes
[stgit] / stgit / out.py
index d3c86b4..753c176 100644 (file)
@@ -17,10 +17,10 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
-import sys
+import sys, textwrap
 
 class MessagePrinter(object):
-    def __init__(self):
+    def __init__(self, file = None):
         class Output(object):
             def __init__(self, write, flush):
                 self.write = write
@@ -49,6 +49,10 @@ class MessagePrinter(object):
                     self.at_start_of_line = False
             def tagged_lines(self, tag, lines):
                 tag += ': '
+                width = 79 - 2*self.level - len(tag)
+                lines = [wl for line in lines
+                         for wl in textwrap.wrap(line, width,
+                                                 break_long_words = False)]
                 for line in lines:
                     self.single_line(tag + line)
                     tag = ' '*len(tag)
@@ -64,9 +68,12 @@ class MessagePrinter(object):
                 self.new_line()
                 self.write(string)
                 self.at_start_of_line = string.endswith('\n')
-        self.__stderr = Output(sys.stderr.write, sys.stderr.flush)
-        self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
-        if sys.stdout.isatty():
+        if file:
+            self.__stdout = self.__stderr = Output(file.write, file.flush)
+        else:
+            self.__stdout = Output(sys.stdout.write, sys.stdout.flush)
+            self.__stderr = Output(sys.stdout.write, sys.stdout.flush)
+        if file or sys.stdout.isatty():
             self.__out = self.__stdout
             self.__err = self.__stdout
         else: