Resize the terminal with Python?

前端 未结 2 1317
野的像风
野的像风 2021-01-03 04:58

I couldn\'t find anything with a quick Google search, nor anything on here, saving for this. However, it doesn\'t do the trick. So how exactly do you resize the ter

2条回答
  •  死守一世寂寞
    2021-01-03 05:40

    To change the tty/pty setting you have to use an ioctl on the stdin file descriptor.

    import termios
    import struct
    import fcntl
    
    def set_winsize(fd, row, col, xpix=0, ypix=0):
        winsize = struct.pack("HHHH", row, col, xpix, ypix)
        fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)
    

    But to change the actual window size you can use terminal escape sequences, but not all terminals support or enable that feature. If you're using urxvt you can do this:

    import sys
    sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=32, cols=100))
    

    But that may not work on all terminals.

提交回复
热议问题