Commits

anatoly techtonik committed 0096efa

get console width in Python for linux

  • Participants
  • Parent commits e5cf409
  • Tags 0.6

Comments (0)

Files changed (3)

 
 ChangeLog
 =========
+0.6 (2010-04-24)
+ * it detects console width on POSIX
+
 0.5 (2010-04-23)
  * it detects console width on Windows
 
 
 setup(
     name='wget',
-    version='0.5',
+    version='0.6',
     author='anatoly techtonik <techtonik@gmail.com>',
     url='http://bitbucket.org/techtonik/python-wget/',
 
 import math
 
 
-version = "0.5"
+version = "0.6"
 
 
 def filename_from_url(url):
 
 
 def get_console_width():
-    """Return width of available window area. Autodetection only works
-       on Windows, returns 80 for other platforms
+    """Return width of available window area. Autodetection works for
+       Windows and POSIX platforms. Returns 80 for others
 
        Code from http://bitbucket.org/techtonik/python-pager
     """
         windll.kernel32.GetConsoleScreenBufferInfo(console_handle, byref(sbi))
         return (sbi.srWindow.Right+1, sbi.srWindow.Bottom+1)[0]
 
+    elif os.name == 'posix':
+        from fcntl import ioctl
+        from termios import TIOCGWINSZ
+        from array import array
+
+        winsize = array("H", [0] * 4)
+        ioctl(sys.stdout.fileno(), TIOCGWINSZ, winsize)
+        return (winsize[1], winsize[0])[0]
+
     return 80
 
 def progress_callback(blocks, block_size, total_size):