Skip to content

Instantly share code, notes, and snippets.

@bryant1410
Created April 13, 2020 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryant1410/f4960127d093dff72bf46634e2ace339 to your computer and use it in GitHub Desktop.
Save bryant1410/f4960127d093dff72bf46634e2ace339 to your computer and use it in GitHub Desktop.
Script to change the terminal width (passed by arg) from Python.
#!/usr/bin/env python
"""Script to change the terminal width (passed by arg) from Python."""
import argparse
import fcntl
import struct
import sys
import termios
from array import array
from typing import IO
# From https://stackoverflow.com/a/6420070/1165181
def set_terminal_size(fd: IO, n_rows: int, n_cols: int, x_pix: int = 0, y_pix: int = 0) -> None:
size = struct.pack("HHHH", n_rows, n_cols, x_pix, y_pix)
fcntl.ioctl(fd, termios.TIOCSWINSZ, size) # noqa
def set_width(width: int, fd: IO = sys.stdin) -> None:
# From tqdm's way of getting the terminal width.
size = array('h', fcntl.ioctl(fd, termios.TIOCSWINSZ, '\0' * 8)) # noqa
size[1] = width
set_terminal_size(sys.stdin, *size)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('width')
return parser.parse_args()
def main():
args = parse_args()
set_width(int(args.width))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment