File: Terminal_-_Detect_width.test

package info (click to toggle)
pv 1.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,076 kB
  • sloc: ansic: 8,421; sh: 6,426; makefile: 131; sed: 16
file content (97 lines) | stat: -rwxr-xr-x 2,879 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
#
# Check that the terminal width is detected on startup.

# Allow all tests to be skipped, e.g. during a release build
test "${SKIP_ALL_TESTS}" = "1" && exit 77

true "${testSubject:?not set - call this from 'make check'}"
true "${workFile1:?not set - call this from 'make check'}"
true "${workFile2:?not set - call this from 'make check'}"

# Skip the test if `tmux' is not available.
if ! command -v tmux >/dev/null 2>&1; then
	echo "test requires \`tmux'"
	exit 77
fi

# Skip the test if `tmux' does not have "-C".
if echo "kill-server" | tmux -C -L pvtest 2>&1 | grep -Fq "tmux: unknown option"; then
	echo "test requires a newer \`tmux'"
	exit 77
fi

# Run the given command $3 inside a terminal of the given width $1, waiting
# $2 seconds, and output the results.
#
runInTerminal () {
	terminalWidth="$1"
	secondsToWait="$2"
	commandToRun="$3"

	{
	echo "set remain-on-exit on"
	echo "new-session -d -x ${terminalWidth} -y 5"
	# starting the session doesn't always correctly set the size
	echo "resize-window -x ${terminalWidth} -y 5"
	# tmux 1.8 doesn't have "resize-window"
	echo "resize-pane -x ${terminalWidth} -y 5"
	echo "pipe-pane 'cat > ${workFile1}'"
	sleep 0.5
	echo "respawn-pane -k \"${commandToRun}\""
	sleep "${secondsToWait}"
	echo "kill-server"
	sleep 1
	} \
	| tmux -C -L pvtest >/dev/null

	cat "${workFile1}"
}

# Run a progress meter inside a terminal of the given width, and output the
# longest number of characters seen between "[" and "]".
#
runWidthTest () {
	runInTerminal "$1" "$2" "${testSubject} -pSs 1K </dev/zero >/dev/null" \
	| tr '\r' '\n' \
	| sed -n 's/^.*\[//;s/\].*$//p' \
	| awk 'BEGIN {m=0} {n=length($0); m=(n>m?n:m)} END {print m}'
}


# Perform a test run of the given width and exit on failure.  The longest
# bar found by runWidthTest() should be the width of the terminal minus the
# "[] 100%" characters, i.e. 7 less.
#
widthTest () {
	terminalWidth="$1"
	longestBar=$(runWidthTest "${terminalWidth}" 1)
	# Retry test with longer delay, if null result.
	if test -z "${longestBar}" || test "${longestBar}" -eq 0; then
		longestBar=$(runWidthTest "${terminalWidth}" 5)
	fi
	if test -z "${longestBar}"; then
		echo "no output from test at width=${terminalWidth}"
		exit 1
	elif ! test "${longestBar}" -eq $((terminalWidth-7)); then
		echo "bar size incorrect (${longestBar}) at width=${terminalWidth}"
		exit 1
	fi
}


# Check that tmux can actually resize the terminal.
checkSize="100"
detectedWidth=$(runInTerminal "${checkSize}" 1 "stty -a | tr \\\";\\\" \\\"\\\\n\\\" | grep col | sed -n 1p | tr -dc 0-9")
if ! test "${detectedWidth}" = "${checkSize}"; then
	echo "test requires a \`tmux' that can resize a detached window (requested width ${checkSize}, detected ${detectedWidth})"
	exit 77
fi

# Test the progress bar width at various terminal sizes.
widthTest 80
widthTest 120
widthTest 400
widthTest 20

exit 0