pexpect / ptyprocess

Run a subprocess in a pseudo terminal
https://ptyprocess.readthedocs.io/en/latest/
Other
217 stars 71 forks source link

The positions of the two arguments in setwinsize function appear to be reversed #71

Open coderk17 opened 2 years ago

coderk17 commented 2 years ago

Recently I made a Web terminal with ptyprocess, websocket, and XTerm, but eventually found that the Web terminal size could not adapt after the resize event was triggered.

I even thought my computer was actually a big phone until I switched the positions of two parameters and the page appeared normal.

Before:

def _setwinsize(fd, rows, cols):
    # Some very old platforms have a bug that causes the value for
    # termios.TIOCSWINSZ to be truncated. There was a hack here to work
    # around this, but it caused problems with newer platforms so has been
    # removed. For details see https://github.com/pexpect/pexpect/issues/39
    TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
    # Note, assume ws_xpixel and ws_ypixel are zero.
    s = struct.pack('HHHH', rows, cols, 0, 0)
    fcntl.ioctl(fd, TIOCSWINSZ, s)

After using the following modified code, the interface display is normal:

def _setwinsize(fd, rows, cols):
    # Some very old platforms have a bug that causes the value for
    # termios.TIOCSWINSZ to be truncated. There was a hack here to work
    # around this, but it caused problems with newer platforms so has been
    # removed. For details see https://github.com/pexpect/pexpect/issues/39
    width, height = cols, rows
    TIOCSWINSZ = getattr(termios, 'TIOCSWINSZ', -2146929561)
    # Note, assume ws_xpixel and ws_ypixel are zero.
    s = struct.pack('HHHH', width, height, 0, 0)
    fcntl.ioctl(fd, TIOCSWINSZ, s)
takluyver commented 2 years ago

The ioctl man page says that rows (i.e. height) comes first, and then columns (width). I've also used this code to do basically the same thing you describe years ago, to have a web based terminal in the Jupyter interface (part of this is the terminado package).

So double check and see if it's possible that the parameters are being switched round somewhere before they get to ptyprocess. :slightly_smiling_face:

coderk17 commented 2 years ago

All right, I'll check again and post the results below. Thank you for your reply.