Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

termsize: without python #10

Open
jraby opened this issue Apr 6, 2020 · 4 comments
Open

termsize: without python #10

jraby opened this issue Apr 6, 2020 · 4 comments

Comments

@jraby
Copy link

jraby commented Apr 6, 2020

(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
Copy link

wrljet commented Dec 30, 2020

That works well, too. Thanks.

@Jibun-no-Kage
Copy link

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

@jraby
Copy link
Author

jraby commented Jan 28, 2024

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

@Jibun-no-Kage
Copy link

Jibun-no-Kage commented Jan 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants