summary refs log tree commit diff stats
path: root/vellum
blob: 8cd03c21293c044f42bdcc7d8d27816d34d4ba07 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
set +x

readc() { # arg: <variable-name>
	# adapted from https://unix.stackexchange.com/a/464963
	if test -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

	# Sanitize any variable that's passed in
	case "$1" in
		c) var=_c ;;
		var) var=_var ;;
		'') var=REPLY ;;
		*) var="$1" ;;
	esac

	# Zero-out the variable
	eval "$var="

	# Read one full *character*, not *byte*
	while :; do
		c=$(dd bs=1 count=1 2>/dev/null; echo .)
		c=${c%.}

		# Break on EOF
		test -n "$c" || break

		eval "$var=\${$var}$c"

		# Break when we have a full character (wc -m)
		test "$(eval printf %s "\$$var" | wc -m)" -eq 0 || break
	done

	# Round-trip sanitized variables
	test "x$var" = x_c && eval c="\$$var"
	test "x$var" = x_var && eval var="\$$var"

	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
	arg="${1:-0}"
	case "$arg" in
		+*|-*) n=$((n+${arg#+})) ;;
		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 test $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.
	if test -t 2
	then
		# only works if not redirecting stderr
		tty <&2
	else
		echo /proc/self/fd/1 # probably only works on linux
	fi
}

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 "$@"