Containers, ttys and curses

OK, this is a bit low-level, feel free to gloss over, there’s no quiz this week.

There’s a minor irritation that exists in the world of containers. Sometimes you need to go poke around in them, and you want to use an application that uses curses (e.g. vi). And you find that you have 24 rows and 80 columns just like that old vt100 you used to use. Sure you could install xterm and get ‘resize’, but then you have a whole ton of libraries etc chewing up space and time.

Well, from the ‘hack it old-school’ category, I present ‘resize via escape sequence only’. See here for what CSI 8 is. Side note, this comes from ‘dtterm’, which is a product I worked on in ~1994! You can run this w/o all those messy support libraries, and, in fact, without even tty support.

#!/bin/bash
  
# CSI 8 size request
stty -echo
echo -ne '\e[18t'

# Read size (CSI 8 ; height ; width t)
width=
heigh=
p=0
while IFS= read -r -n1 char
do
  # Skip CSI
  if [[ "$p" == 0  &&  "$char" == ";" ]];  then
    p=$((p+1))
  elif [[ "$p" == 1  ]];  then
    [[ "$char" == ";" ]]  &&  p=$((p+1))  ||  height="${height}${char}"
  elif [[ "$p" == 2  ]];  then
    [[ "$char" == "t" ]]  &&  break  ||  width="${width}${char}"
  fi
done

# Done
stty echo
echo LINES=$height
echo COLUMNS=$width

 


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *