#!/bin/bash

get_rows() {
	local size=$(stty size)
	echo ${size% *}
}

print_screenful_of_text() {
	local prefix="$1"
	local rows=$(get_rows)
	local i
	echo -n $'\e[H\e[J'
	for i in `seq 1 $rows`; do
		echo -n "$prefix, line $i"
		if [ $i = 1 ]; then
			echo ' -- top row'
		elif [ $i = $rows ]; then
			echo -n ' -- bottom row'
		else
			echo
		fi
	done
}

prev_rows=$(get_rows)

# Make sure scrollbar appears
for i in `seq 1 $((prev_rows+10))`; do
	echo "text to be scrolled out, line $i"
done

# Print first screenful
n=1
print_screenful_of_text "iteration $n"

# Repaint when number of rows changes
while :; do
	sleep 0.1
	rows=$(get_rows)
	if [ $rows != $prev_rows ]; then
		prev_rows=$rows
		n=$((n+1))
		print_screenful_of_text "iteration $n"
	fi
done
