summary refs log tree commit diff stats
path: root/vellum
blob: b436b6cfefc0307d27734a5b052336c6e94c28d4 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh

# https://unix.stackexchange.com/a/464963
readc() { # arg: <variable-name>
	if [ -t 0 ]; then
		# if stdin is a tty device, put it out of icanon, set min and
		# time to sane value, but don't otherwise touch other input or
		# or local settings (echo, isig, icrnl...). Take a backup of the
		# previous settings beforehand.
		saved_tty_settings=$(stty -g)
		stty -icanon min 1 time 0
	fi
	eval "$1="
	while
		# read one byte, using a work around for the fact that command
		# substitution strips the last character.
		c=$(dd bs=1 count=1 2> /dev/null; echo .)
		c=${c%.}

		# break out of the loop on empty input (eof) or if a full character
		# has been accumulated in the output variable (using "wc -m" to count
		# the number of characters).
		[ -n "$c" ] &&
			eval "$1=\${$1}"'$c
	  [ "$(($(printf %s "${'"$1"'}" | wc -m)))" -eq 0 ]'; do
		continue
	done
	if [ -t 0 ]; then
		# restore settings saved earlier if stdin is a tty device.
		stty "$saved_tty_settings"
	fi
}

setup_terminal() {
	# Setup the terminal for the TUI.
	# '\e[?1049h': Use alternative screen buffer.
	# '\e[?7l':    Disable line wrapping.
	# '\e[?25l':   Hide the cursor.
	# '\e[2J':     Clear the screen.
	# '\e[1;Nr':   Limit scrolling to scrolling area.
	#              Also sets cursor to (0,0).
	printf '\e[?1049h\e[?7l\e[2J\e[1;%sr' "$((LINES - 1))"

	# Hide echoing of user input
	stty -echo
}

reset_terminal() {
	# Reset the terminal to a useable state (undo all changes).
	# '\e[?7h':   Re-enable line wrapping.
	# '\e[?25h':  Unhide the cursor.
	# '\e[2J':    Clear the terminal.
	# '\e[;r':    Set the scroll region to its default value.
	#             Also sets cursor to (0,0).
	# '\e[?1049l: Restore main screen buffer.
	printf '\e[?7h\e[?25h\e[2J\e[;r\e[?1049l'

	# Show user input.
	stty echo
}

printlines() { # printlines file min max
	if test -n "$DEBUG"; then
		echo --- >&2
	else
		clear
	fi
	sed -n "$2,$3p" "$1"
	notify_flush
}

notify() { # notify STRING
	notification="$notification$1"
}

notify_flush() { # notify_flush
	winch
	printf '\e[s\e[%s;%sH' 0 "$c"
	printf '%s' "$notification"
	printf '\e[u'
	notification=
}

scroll() { # scroll nlines -- MUTATES GLOBAL STATE
	case "${1:-0}" in
		\+*|\-*) n=$((n+$1)) ;;
		0) ;;
		*) n=$1 ;;
	esac

	if test $n -gt $((max - l))
	then
		n=$((max - l))
		notify '_'
	fi

	if test $n -lt $min
	then
		notify '^'
		n=$min
	fi

	m=$((n+l))
	if $m -ge $max
	then
		m=$max
	fi
	# echo "min:$min max:$max n:$n m:$m l:$l" >&2
}

ctty() { # ctty
	## Get the controlling TTY interface.
	tty <&2 || # only works if not redirecting stderr
		echo /proc/self/fd/1 # probably only works on linux
}

winch() {
	stty size > "$wf"
	read -r l c < "$wf"
	# echo l:$l c:$c >&2
	l=$((l-2)) # last line
}

cleanup() {
	rm "$bf" "$wf"
	reset_terminal
	exit
}

vellum() { # vellum [-n NUM] < INPUT
	# Set up buffer
	bf="$(mktemp)" # buffer
	cat "${@:--}" > "$bf"
	# Redirect input to the controlling terminal
	## XXX: This assumes that you haven't redirected stderr.
	exec < "$(ctty)"
	max=$(wc -l < "$bf")
	min=1 # min = minimum accessible line num.
	n=$min # n = num. first line to show

	# Set up terminal
	setup_terminal
	wf="$(mktemp)" # winch file
	winch; trap winch WINCH
	m=$((n+l)) # m = num. last line to show

	trap cleanup EXIT INT QUIT

	# Main loop
	while :; do
		scroll
		printlines "$bf" $n $m

		readc key >/dev/null 2>&1
		case "$key" in
			' ') scroll +$l ;;
			'') scroll -$l ;;
			j) scroll +1 ;;
			k) scroll -1 ;;
			g) scroll $min ;;
			G) scroll $max ;;
			q) break ;;
		esac
	done
	break
	echo
}

test -n "$DEBUG" && { set -x; exec 2> /tmp/vellum.log; }
test -z "$SOURCE" && vellum "$@"