Skip to content

Instantly share code, notes, and snippets.

@marsam
Created November 1, 2013 17:22
Show Gist options
  • Select an option

  • Save marsam/7268750 to your computer and use it in GitHub Desktop.

Select an option

Save marsam/7268750 to your computer and use it in GitHub Desktop.
terminal size
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def terminal_size():
width, height = None, None
if sys.platform == 'win32':
import win32utils
width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
else:
try:
import struct, fcntl, termios
s = struct.pack('HHHH', 0, 0, 0, 0)
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
height, width = struct.unpack('HHHH', x)[0:2]
except (IOError, AttributeError):
pass
return width, height
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment