Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to return width of terminal running the script. Perfect for CLI apps in determining available space for formatting output data. If run as a script simply prints the number.
#!/usr/bin/env python
import subprocess
command = ['tput', 'cols']
def get_terminal_width():
try:
width = int(subprocess.check_output(command))
except OSError as e:
print("Invalid Command '{0}': exit status ({1})".format(
command[0], e.errno))
except subprocess.CalledProcessError as e:
print("Command '{0}' returned non-zero exit status: ({1})".format(
command, e.returncode))
else:
return width
def main():
width = get_terminal_width()
if width:
print(width)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment