Skip to content

communicate terminal size to pty after fit on xterm #5847

@jacopofar

Description

@jacopofar

First Check

  • I added a very descriptive title here.
  • This is not a security issue (those should be reported via the security advisory instead).
  • This is not a Q&A. I am sure something is wrong with NiceGUI or its documentation.
  • I used the GitHub search to find a similar issue and came up empty.

Example Code

Description

More like a feature request than a bug.

Using the ui.xterm example from this same repo I found that the terminal cannot correctly detect its own size:

$ python
>>> import os
>>> os.get_terminal_size()
os.terminal_size(columns=0, lines=0)

the docstring of the xterm component mentions that there is a fit method but it resizes the xterm in the client to the container, while the pty remains unaware of the size change.

I found that that the terminal size can be communicated by modifying the example like this:

terminal = ui.xterm({"cols": 130, "rows": 40})
[...]
if pty_pid == pty.CHILD:
    os.execv(
        "/bin/bash", ("bash",)
    )  # child process of the fork gets replaced with "bash"
else:
    winsize = struct.pack("HHHH", 40, 130, 0, 0)
    fcntl.ioctl(pty_fd, termios.TIOCSWINSZ, winsize)

that is, first I pass 140x30 to the frontend component and then send the control codes to the terminal file itself so it is in sync.

I tested it and it works with the os.terminal_size function and also with a more sophisticated TUI app.

Is there a way to automate this and let the terminal always know its own size upon fit (and perhaps once at initialization)? I am not familiar with the internals of nicegui to do it, but it seems an useful feature to have.

NiceGUI Version

3.8.0

Python Version

3.14

Browser

Firefox

Operating System

Linux

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureType/scope: New or intentionally changed behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions