File: format-strings.sh

package info (click to toggle)
tmux 3.6a-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,508 kB
  • sloc: ansic: 72,681; sh: 1,813; yacc: 1,483; awk: 339; makefile: 235; perl: 41
file content (234 lines) | stat: -rw-r--r-- 8,541 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/sh

# Tests of formats as described in tmux(1) FORMATS

PATH=/bin:/usr/bin
TERM=screen

[ -z "$TEST_TMUX" ] && TEST_TMUX=$(readlink -f ../tmux)
TMUX="$TEST_TMUX -Ltest"

# test_format $format $expected_result
test_format()
{
	fmt="$1"
	exp="$2"

	out=$($TMUX display-message -p "$fmt")

	if [ "$out" != "$exp" ]; then
		echo "Format test failed for '$fmt'."
		echo "Expected: '$exp'"
		echo "But got   '$out'"
		exit 1
	fi
}

# test_conditional_with_pane_in_mode $format $exp1 $exp2
#
# Tests the format string $format to yield $exp1 if #{pane_in_mode} is true and
# $exp2 when #{pane_in_mode} is false.
test_conditional_with_pane_in_mode()
{
	fmt="$1"
	exp_true="$2"
	exp_false="$3"

	$TMUX copy-mode # enter copy mode
	test_format "$fmt" "$exp_true"
	$TMUX send-keys -X cancel # leave copy mode
	test_format "$fmt" "$exp_false"
}

# test_conditional_with_session_name #format $exp_summer $exp_winter
#
# Tests the format string $format to yield $exp_summer if the session name is
# 'Summer' and $exp_winter if the session name is 'Winter'.
test_conditional_with_session_name()
{
	fmt="$1"
	exp_summer="$2"
	exp_winter="$3"

	$TMUX rename-session "Summer"
	test_format "$fmt" "$exp_summer"
	$TMUX rename-session "Winter"
	test_format "$fmt" "$exp_winter"
	$TMUX rename-session "Summer" # restore default
}


$TMUX kill-server 2>/dev/null
$TMUX -f/dev/null new-session -d || exit 1

# used later in conditionals
$TMUX rename-session "Summer" || exit 1
$TMUX set @true 1 || exit 1
$TMUX set @false 0 || exit 1
$TMUX set @warm Summer || exit 1
$TMUX set @cold Winter || exit 1

# Plain string without substitutions et al
test_format "abc xyz" "abc xyz"

# Test basic escapes for "#", "{", "#{" "}", "#}", ","
test_format "##" "#"
test_format "#," ","
test_format "{" "{"
test_format "##{" "#{"
test_format "#}" "}"
test_format "###}" "#}" # not a "basic" one but interesting nevertheless

# Simple expansion
test_format "#{pane_in_mode}" "0"

# Simple conditionals
test_format "#{?}" ""
test_format "#{?abc}" "abc"
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc}" "abc" ""
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,xyz}" "abc" "xyz"
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,@true,xyz}" "abc" "xyz"
test_format "#{?@false,abc,@false,xyz}" ""
test_format "#{?@false,abc,@false,xyz,default}" "default"

# Expansion in conditionals
test_format "#{?#{@warm}}" "Summer"
test_conditional_with_pane_in_mode "#{?#{pane_in_mode},#{@warm}}" "Summer" ""
test_conditional_with_pane_in_mode "#{?#{pane_in_mode},#{@warm},#{@cold}}" "Summer" "Winter"

# Basic escapes in conditionals
# Value of an (else-)if-condition
test_conditional_with_pane_in_mode "#{?pane_in_mode,##,xyz}" "#" "xyz"
test_conditional_with_pane_in_mode "#{?pane_in_mode,#,,xyz}" "," "xyz"
test_conditional_with_pane_in_mode "#{?pane_in_mode,{,xyz}" "{" "xyz"
test_conditional_with_pane_in_mode "#{?pane_in_mode,##{,xyz}" "#{" "xyz"
test_conditional_with_pane_in_mode "#{?pane_in_mode,#},xyz}" "}" "xyz"
# not a "basic" one but interesting nevertheless
test_conditional_with_pane_in_mode "#{?pane_in_mode,###},xyz}" "#}" "xyz"
# Default value if no condition matches
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,##}" "abc" "#"
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,#,}" "abc" ","
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,{}" "abc" "{"
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,##{}" "abc" "#{"
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,#}}" "abc" "}"
# not a "basic" one but interesting nevertheless
test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,###}}" "abc" "#}"
# mixed
test_conditional_with_pane_in_mode "#{?pane_in_mode,{,#}}" "{" "}"
test_conditional_with_pane_in_mode "#{?pane_in_mode,#},{}" "}" "{"
test_conditional_with_pane_in_mode "#{?pane_in_mode,##{,###}}" "#{" "#}"
test_conditional_with_pane_in_mode "#{?pane_in_mode,###},##{}" "#}" "#{"

# Curly brackets {...} do not capture a comma inside of conditionals as the
# conditional ends on the first '}'
test_conditional_with_pane_in_mode "#{?pane_in_mode,{abc,xyz},bonus}" "{abc,bonus}" "xyz,bonus}"

# Substitutions '#{...}' capture the comma
# invalid format: #{abc,xyz} is not a known variable name.
#test_conditional_with_pane_in_mode "#{?pane_in_mode,#{abc,xyz},bonus}" "" "bonus"

# Parenthesis (...) do not capture a comma, and "xyz)" is a false condition
test_conditional_with_pane_in_mode "#{?pane_in_mode,(abc,xyz),bonus}" "(abc" ""
test_conditional_with_pane_in_mode "#{?pane_in_mode,(abc#,xyz),bonus}" "(abc,xyz)" "bonus"

# Brackets [...] do not capture a comma, and "xyz]" is a false condition
test_conditional_with_pane_in_mode "#{?pane_in_mode,[abc,xyz],bonus}" "[abc" ""
test_conditional_with_pane_in_mode "#{?pane_in_mode,[abc#,xyz],bonus}" "[abc,xyz]" "bonus"


# Escape comma inside of #(...) Note: #() commands are run asynchronous and are
# substituted with result of the *previous* run, an empty string if the command
# is new, or a placeholder after a few seconds. The format is updated as soon
# as the command finishes. As we are printing the message only once it never
# gets updated and the displayed message is empty.
test_format "#{?pane_in_mode,#(echo #,),xyz}" "xyz"
test_conditional_with_pane_in_mode "#{?pane_in_mode,#(echo #,),xyz}" "" "xyz"
# This caching does not work :-(
#$TMUX display-message -p "#(echo #,)" > /dev/null
#test_conditional_with_pane_in_mode "#{?pane_in_mode,#(echo #,),xyz}" "," "xyz"
#test_conditional_with_pane_in_mode "#{?pane_in_mode,#(echo #,),xyz}" "," "xyz"

# invalid format: '#(' is not closed in the first argument of #{?,,}.
test_conditional_with_pane_in_mode "#{?pane_in_mode,#(echo ,)xyz}" "" ")xyz"

# Escape comma inside of #[...]
test_conditional_with_pane_in_mode "#{?pane_in_mode,#[fg=default#,bg=default]abc,xyz}" "#[fg=default,bg=default]abc" "xyz"
# invalid style: '#[' is not closed in the first argument of #{?,,}
test_conditional_with_pane_in_mode "#{?pane_in_mode,#[fg=default,bg=default]abc}" "#[fg=default" "bg=default]abc"

# Conditionals with comparison
test_conditional_with_session_name "#{?#{==:#{session_name},Summer},abc,xyz}" "abc" "xyz"
# Conditionals with comparison and escaped commas
$TMUX rename-session ","
test_format "#{?#{==:#,,#{session_name}},abc,xyz}" "abc"
$TMUX rename-session "Summer" # reset to default

# Conditional in conditional
test_conditional_with_pane_in_mode "#{?pane_in_mode,#{?#{==:#{session_name},Summer},ABC,XYZ},xyz}" "ABC" "xyz"
test_conditional_with_session_name "#{?pane_in_mode,#{?#{==:#{session_name},Summer},ABC,XYZ},xyz}" "xyz" "xyz"

test_conditional_with_pane_in_mode "#{?pane_in_mode,abc,#{?#{==:#{session_name},Summer},ABC,XYZ}}" "abc" "ABC"
test_conditional_with_session_name "#{?pane_in_mode,abc,#{?#{==:#{session_name},Summer},ABC,XYZ}}" "ABC" "XYZ"

# Some fancy stackings
test_conditional_with_pane_in_mode "#{?#{==:#{?pane_in_mode,#{session_name},#(echo Spring)},Summer},abc,xyz}" "abc" "xyz"



# Tests for boolean expressions

# "0" and the empty string are false, everything else is true.
test_format "#{!!:0}" "0"
test_format "#{!!:}" "0"
test_format "#{!!:1}" "1"
test_format "#{!!:2}" "1"
test_format "#{!!:non-empty string}" "1"
test_format "#{!!:-0}" "1"
test_format "#{!!:0.0}" "1"

# Logical operators
test_format "#{!:0}" "1"
test_format "#{!:1}" "0"

test_format "#{&&:0}" "0"
test_format "#{&&:1}" "1"
test_format "#{&&:0,0}" "0"
test_format "#{&&:0,1}" "0"
test_format "#{&&:1,0}" "0"
test_format "#{&&:1,1}" "1"
test_format "#{&&:0,0,0}" "0"
test_format "#{&&:0,1,1}" "0"
test_format "#{&&:1,0,1}" "0"
test_format "#{&&:1,1,0}" "0"
test_format "#{&&:1,1,1}" "1"

test_format "#{||:0}" "0"
test_format "#{||:1}" "1"
test_format "#{||:0,0}" "0"
test_format "#{||:0,1}" "1"
test_format "#{||:1,0}" "1"
test_format "#{||:1,1}" "1"
test_format "#{||:0,0,0}" "0"
test_format "#{||:1,0,0}" "1"
test_format "#{||:0,1,0}" "1"
test_format "#{||:0,0,1}" "1"
test_format "#{||:1,1,1}" "1"



# Format test for the literal option
# Note: The behavior for #{l:...} with escapes is sometimes weird as #{l:...}
# respects the escapes.
test_format "#{l:#{}}" "#{}"
test_format "#{l:#{pane_in_mode}}" "#{pane_in_mode}"
test_format "#{l:#{?pane_in_mode,#{?#{==:#{session_name},Summer},ABC,XYZ},xyz}}" "#{?pane_in_mode,#{?#{==:#{session_name},Summer},ABC,XYZ},xyz}"

# With escapes (which escape but are returned literally)
test_format "#{l:##{}" "#{"
test_format "#{l:#{#}}}" "#{#}}"

# Invalid formats:
#test_format "#{l:#{}" ""
#test_format "#{l:#{#}}" ""

exit 0