Resizable serial console window

serial-console

When using the serial console of my system I always end up with $COLUMNS=80 and $LINES=24.

While I can change these variables manually it is somewhat annoying to do this any time when the client side terminal window has been resized.

Usually I'm connecting to the console using screen /dev/mytty baudrate.

Changing the $TERM environment variable to "screen" or "xterm" does not help.

Will I need to call getty with some of those instead of vt100?

Needless to say that all of this works fine, when I connect to the same machine using ssh.

Best Answer

Like the commentators before me mentioned there is no alternative to calling resize after every command, if you don't have this command and you don't want to install a package where it's in (xterm), here are two POSIX shell script that do the same using ANSI terminal escape codes:

res() {

  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"
}

res2() {

  old=$(stty -g)
  stty raw -echo min 0 time 5

  printf '\033[18t' > /dev/tty
  IFS=';t' read -r _ rows cols _ < /dev/tty

  stty "$old"

  # echo "cols:$cols"
  # echo "rows:$rows"
  stty cols "$cols" rows "$rows"
}

BTW, in my .profile file you will find the following: [ $(tty) = /dev/ttyS0 ] && res so that the the terminal size is determined on every login over the serial line (the one I use for management), e.g. after you reboot the device.
See also the idea by rsaw in the comments to have the line [ $(tty) = /dev/ttyS0 ] && trap res2 DEBUG there instead so the resizing runs after every command (note that AFAIK it's not or not always possible on busybox though).

Related Question