]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fixed the output wrapping for Alembic message output, so that
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 21 Nov 2013 22:47:24 +0000 (17:47 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 21 Nov 2013 22:47:24 +0000 (17:47 -0500)
we either get the terminal width for "pretty printing" with
indentation, or if not we just output the text as is; in any
case the text won't be wrapped too short. #135

alembic/util.py
docs/build/changelog.rst

index a8750a2c145d967c6ec313538e8daa130c340c8c..b9565480be3eeb087621607f3ec6b41411ae1a97 100644 (file)
@@ -30,10 +30,17 @@ if not sqla_07:
 from sqlalchemy.util import format_argspec_plus, update_wrapper
 from sqlalchemy.util.compat import inspect_getfullargspec
 
+
 try:
-    width = int(os.environ['COLUMNS'])
-except (KeyError, ValueError):
-    width = 80
+    import fcntl
+    import termios
+    import struct
+    ioctl = fcntl.ioctl(0, termios.TIOCGWINSZ,
+                           struct.pack('HHHH', 0, 0, 0, 0))
+    _h, TERMWIDTH, _hp, _wp = struct.unpack('HHHH', ioctl)
+except (ImportError, IOError):
+    TERMWIDTH = None
+
 
 def template_to_file(template_file, dest, **kw):
     with open(dest, 'w') as f:
@@ -171,11 +178,17 @@ def warn(msg):
     warnings.warn(msg)
 
 def msg(msg, newline=True):
-    lines = textwrap.wrap(msg, width)
-    if len(lines) > 1:
-        for line in lines[0:-1]:
-            write_outstream(sys.stdout, "  ", line, "\n")
-    write_outstream(sys.stdout, "  ", lines[-1], ("\n" if newline else ""))
+    if TERMWIDTH is None:
+        write_outstream(sys.stdout, msg)
+        if newline:
+            write_outstream(sys.stdout, "\n")
+    else:
+        # left indent output lines
+        lines = textwrap.wrap(msg, TERMWIDTH)
+        if len(lines) > 1:
+            for line in lines[0:-1]:
+                write_outstream(sys.stdout, "  ", line, "\n")
+        write_outstream(sys.stdout, "  ", lines[-1], ("\n" if newline else ""))
 
 def load_python_file(dir_, filename):
     """Load a file from the given path as a Python module."""
index 364bc8408628e44a8a2e7f6dbc4f58480552bbf4..96ed78303eb83570bb9c3129bc619fd309f8109d 100644 (file)
@@ -7,6 +7,15 @@ Changelog
     :version: 0.6.1
     :released: no release date
 
+    .. change::
+      :tags: bug
+      :tickets: 135
+
+      Fixed the output wrapping for Alembic message output, so that
+      we either get the terminal width for "pretty printing" with
+      indentation, or if not we just output the text as is; in any
+      case the text won't be wrapped too short.
+
     .. change::
       :tags: bug
       :tickets: 145