| |
| """ |
| Determines and prints COLUMNS and LINES of the attached window width. |
| |
| A strange problem: programs that perform screen addressing incorrectly |
| determine the screen margins. Calls to reset(1) do not resolve the |
| issue. |
| |
| This may often happen because the transport is incapable of communicating |
| the terminal size, such as over a serial line. This demonstration program |
| determines true screen dimensions and produces output suitable for evaluation |
| by a bourne-like shell:: |
| |
| $ eval `./resize.py` |
| |
| The following remote login protocols communicate window size: |
| |
| - ssh: notifies on dedicated session channel, see for example, |
| ``paramiko.ServerInterface.check_channel_window_change_request``. |
| |
| - telnet: sends window size through NAWS (negotiate about window |
| size, RFC 1073), see for example, |
| ``telnetlib3.TelnetServer.naws_receive``. |
| |
| - rlogin: protocol sends only initial window size, and does not notify |
| about size changes. |
| |
| This is a simplified version of `resize.c |
| <https://github.com/joejulian/xterm/blob/master/resize.c>`_ provided by the |
| xterm package. |
| """ |
| from __future__ import print_function |
|
|
| |
| import sys |
| import collections |
|
|
| |
| from blessed import Terminal |
|
|
|
|
| def main(): |
| """Program entry point.""" |
| |
| |
| Position = collections.namedtuple('Position', ('row', 'column')) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| term = Terminal(stream=sys.stderr) |
|
|
| |
| |
| |
| |
| with term.location(999, 999): |
|
|
| |
| |
| |
| |
| |
| pos = Position(*term.get_location(timeout=5.0)) |
|
|
| if -1 not in pos: |
| |
| lines, columns = pos.row, pos.column |
|
|
| else: |
| |
| |
| |
| lines, columns = term.height, term.width |
|
|
| print("COLUMNS={columns};\nLINES={lines};\nexport COLUMNS LINES;" |
| .format(columns=columns, lines=lines)) |
|
|
|
|
| if __name__ == '__main__': |
| exit(main()) |
|
|