Created
April 13, 2020 04:54
-
-
Save bryant1410/f4960127d093dff72bf46634e2ace339 to your computer and use it in GitHub Desktop.
Script to change the terminal width (passed by arg) from Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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