akkana / scripts

A collection of hopefully useful scripts.
GNU General Public License v2.0
266 stars 116 forks source link

termsize: without python #10

Open jraby opened 4 years ago

jraby commented 4 years ago

(this is not really an issue)

First, thanks for the blogpost about termsize, it got me started on some yak shaving I didn't intend to do today... :-)

While reading your post I figured there should be a way to do it without python at all.

Here's a way to do it, I thought you might be interested.

#!/bin/bash
set -eu

# Reset term to initial state on exit
stty_ori=$(stty -g)
function atexit {
    stty $stty_ori
}
trap atexit EXIT

# Turn echo off to avoid writing term size on the screen
stty -echo

# Use bash's "read" to send the control seq to the term and read its answer
# 7: save cursor pos
# [r: scroll screen
# [x;yH: set cursor pos
# [6n: get cursor pos
read -p $'\e7\e[r\e[9999;9999H\e[6n' -d R termsize

# Strip leading control char + [ (e.g. \e[x;yH)
termsize=${termsize#??}

# Reset cursor pos
echo -en '\e8'

# Actually resize the terminal
stty $(echo $termsize  | sed 's/^/rows /; s/;/ cols /')

Also, busybox has resize(1). (which I discovered after writing the above...) https://git.busybox.net/busybox/tree/console-tools/resize.c#n85

wrljet commented 3 years ago

That works well, too. Thanks.

Jibun-no-Kage commented 10 months ago

Fails on Pi OS Bookwarn with the following message... 6: function: not found

jraby commented 10 months ago

The script only works with bash, it relies on bash's read built-in (specifically its -d option)

Jibun-no-Kage commented 10 months ago

That is what is odd... I am using BASH, on Pi OS. I was able to get around it by embedding the following in my .bashrc script file. Still testing this it will force the (agetty) based serial console (say opened with putty) to the desired rows and columns but is not perfect, if you happen to have the putty (COM) session smaller than 60 x 200 it will resize the terminal window and everything seems to honor it... but if serial console is bigger than 60 x 200 it does not seem to shrink the (putty) terminal window (which I expected). I can live with the issue, since typically putty defaults to 24x80 on serial consoles.

    if [ $(tty) = "/dev/ttyS0" ]; then

            stty rows 60 cols 200
            echo -e "\033[8;60;200t"

    fi

The big headache is that agetty just does not seem to have any easy way to be changed, looking at the C source code. It defaults to 24x80 but only it the agetty executable does not get a valid terminal size returned

 1395     /* Check for terminal size and if not found set default */
 1396     if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0) {
 1397         if (ws.ws_row == 0)
 1398             ws.ws_row = 24;
 1399         if (ws.ws_col == 0)
 1400             ws.ws_col = 80;
 1401         if (ioctl(STDIN_FILENO, TIOCSWINSZ, &ws))
 1402             debug("TIOCSWINSZ ioctl failed\n");
 1403     }

Would be nice if agetty could be changed to honor say a configuration file in say /etc/default, or such that would allow a default rows and columns values to be set and consistently honored.