Skip to content
Snippets Groups Projects
Commit 47c09331 authored by Georg Sauthoff's avatar Georg Sauthoff
Browse files

add detect-size - detect and set real geometry of a terminal

parent 8992696f
No related branches found
No related tags found
No related merge requests found
...@@ -163,6 +163,7 @@ set(scripts ...@@ -163,6 +163,7 @@ set(scripts
check-dnsbl.py check-dnsbl.py
chromium-extensions.py chromium-extensions.py
cpufreq.py cpufreq.py
detect-size.py
devof.sh devof.sh
disas.sh disas.sh
firefox-addons.py firefox-addons.py
......
...@@ -31,6 +31,8 @@ This repository contains a collection of command line utilities. ...@@ -31,6 +31,8 @@ This repository contains a collection of command line utilities.
-- decompressing cat (autodetects gzip/zstd/bz2/...) -- decompressing cat (autodetects gzip/zstd/bz2/...)
- [dcheck](#dcheck) - [dcheck](#dcheck)
-- run a program under DBX's memory check mode -- run a program under DBX's memory check mode
- detect-size
-- detect and set real height/width of a terminal
- devof - devof
-- list network device names given an address (prefix) -- list network device names given an address (prefix)
- disas - disas
......
#!/usr/bin/env python3
# detect-size - detect and set real height/width of a terminal
#
# replacement for xterm's resize command for systems that
# don't package it separately (e.g. in xterm-resize as Fedora does)
#
# useful for terminals that are attached to a serial line,
# including emulated ones (e.g. on a VM)
#
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: © 2021 Georg Sauthoff <mail@gms.tf>
import fcntl
import os
import struct
import sys
import termios
import tty
def main():
# source: https://sources.debian.org/src/xterm/366-1/resize.c/?hl=137#L139
print('\x1b' '7' '\x1b' '[r' '\x1b' '[9999;9999H' '\x1b' '[6n',
flush=True, end='')
# cf. https://stackoverflow.com/q/40931467/427158
bak = termios.tcgetattr(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
r = b''.join(iter(lambda : os.read(sys.stdin.fileno(), 1), b'R'))
termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, bak)
i = r.rindex(b'[')
j = r.index(b';')
h = int(r[i+1:j])
w = int(r[j+1:])
x = struct.pack('HHHH', h, w, 0, 0)
print(f'\rheight: {h}, width: {w}')
fcntl.ioctl(sys.stdout, termios.TIOCSWINSZ, x)
# alternatively: ioctl() on /dev/tty
if __name__ == '__main__':
sys.exit(main())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment