summaryrefslogtreecommitdiffstats
path: root/bin/ovr
blob: a93071054c04d2cd47893032080c8ca5b1076f7c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh

trap '' INT;


err()
{
	>&2 echo "$(basename "$0"): error: $*";
	exit 1;
}


ovr_alt()
{
	permanent=;

	enter() { tput smcup; } >/dev/tty;
	leave() { tput rmcup; } >/dev/tty;

	trap leave QUIT TERM;

	enter;
	permanent="$(tee /dev/tty | tail -n "$n"; echo x)";
	leave;
	printf '%s' "${permanent%x}";
}


ovr_noalt()
{
	sed -e "s/^\(.\{,$(tput cols)\}\).*$/\1/" \
	    -e '2,$s/^/\x1B[1A\x1B[K/';
}


n=1;
alt=1
while test $# -ge 1; do
	case "$1" in
	-n | --lines)
		if test $# -lt 2; then
			err "$1: Missing option argument.";
		fi;
		n="$2";
		shift;
		;;
	--no-alt)
		alt=0;
		;;
	-*)
		err "$1: Unknown option.";
		;;
	*)
		err "$1: Unknown argument.";
		;;
	esac;
	shift;
done;

if test "$alt" = "1"; then
	ovr_alt;
else
	ovr_noalt;
fi;