On a Raspberry Pi (Debian Buster) I use this for Serial Gadget (based on phk's answer):.
Calling resize only once per line.
sudo nano .profile
add:
# resize function for connected serial console
resize() {
old=$(stty -g)
stty raw -echo min 0 time 5
printf '\0337\033[r\033[999;999H\033[6n\0338' > /dev/tty
IFS='[;R' read -r _ rows cols _ < /dev/tty
stty "$old"
#echo "cols:$cols"
#echo "rows:$rows"
stty cols "$cols" rows "$rows"
}
ALL_COMMANDS_FINISHED=1
PreCommands() {
if [ "$ALL_COMMANDS_FINISHED" = '1' ]; then
ALL_COMMANDS_FINISHED=0
resize
#echo "1->0"
fi
}
PostCommands() {
ALL_COMMANDS_FINISHED=1
}
# if login thru ttyGS0tty*
if [ $(tty) = '/dev/ttyGS0'tty* ]; then
# PreCommands is called before every single command on a command line
trap PreCommands DEBUG
# PostCommands is called after all commands on a command line are finished
PROMPT_COMMAND="PostCommands"
fi