-
Notifications
You must be signed in to change notification settings - Fork 6
/
slides
executable file
·373 lines (317 loc) · 10.4 KB
/
slides
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/bash
# Copyright (c) 2018 Stanislav Kontar
# License: MIT
#
# Inspired by https://github.com/fxn/tkn
CODE_STYLE="breeze"
SECTION_DECORATION=" ❧❦☙ "
LEN_DECORATION="${#SECTION_DECORATION}"
FORMATTING=(
"<b>" "$( tput bold )"
"<s>" "$( tput smso )"
"<i>" "$( tput sitm )"
"<u>" "$( tput smul )"
"<r>" "$( tput sgr0 )"
"<R>" "$( tput setaf 196 )"
"<G>" "$( tput setaf 82 )"
"<B>" "$( tput setaf 39 )"
"<Y>" "$( tput setaf 226 )"
"<V>" "$( tput setaf 129 )"
"<E>" "$( tput setaf 248 )"
)
NR_FORMATTING="${#FORMATTING[@]}"
for (( i = 0; i < NR_FORMATTING; i += 2 )); do
SED_DELETE+=" -e s/${FORMATTING[i]}//g"
SED_REPLACE+=" -e s/${FORMATTING[i]}/${FORMATTING[i + 1]}/g"
done
file="${1:-example}"
prepare_terminal() {
# Get terminal values, clean terminal and remove cursor
rows=$( tput lines )
columns=$( tput cols )
tput clear
tput civis
}
restore_terminal() {
# Clean terminal and show cursor again
tput clear
tput cnorm
}
read_slides() {
local i=0
slides=0 # slides are indexed starting with 1
slide_starts=()
slide_lengths=()
slide_raw_lines=()
slide_clean_lines=()
slide_formatted_lines=()
prepare_terminal
readarray -t slide_raw_lines < "$file"
readarray -t slide_clean_lines <<< "$( sed ${SED_DELETE} "$file" )"
readarray -t slide_formatted_lines <<< "$( sed ${SED_REPLACE} "$file" )"
while IFS='' read -r line; do
if [[ "$line" =~ ={40,} ]]; then
(( slides++ ))
slide_formats[slides]="$( sed -r -n 's/([a-zA-Z ]*[a-zA-Z])[[:space:]]*={40,}.*$/\1/p' <<< "$line" )"
slide_starts[slides]="$(( i + 1 ))"
slide_lengths[slides]=0
else
(( slide_lengths[slides]++ ))
fi
(( i++ ))
done < "$file"
# Remove empty lines at the end of the slide
for (( i = 1; i <= slides; i++ )); do
(( start = slide_starts[i] ))
(( length = slide_lengths[i] ))
while [[ "${slide_raw_lines[start + length - 1]}" == "" ]]; do
(( slide_lengths[i]-- ))
(( length = slide_lengths[i] ))
done
done
}
check_slides() {
local i
local j
for (( i = 1; i <= slides; i++ )); do
(( start = slide_starts[i] ))
(( length = slide_lengths[i] ))
# Check if slide fits in the terminal
if (( length > rows - 2 )); then
echo "Slide $i too tall."
return 1
fi
for (( j = start; j < start + length; j++ )); do
if (( ${#slide_clean_lines[j]} > columns - 4 )); then
echo "Slide $i too wide."
return 1
fi
done
done
return 0
}
show_slide() {
local i
prepare_terminal
start="${slide_starts[slide]}"
length="${slide_lengths[slide]}"
format="${slide_formats[slide]}"
# Compute vertical padding
lines_padding=$(( (rows - length + 1) / 2 )) # Add 1 to round up
# Center slide type
if [[ "$format" == "center" ]]; then
for (( i = 0; i < lines_padding; i++ )); do
echo
done
for (( i = start; i < start + length; i++ )); do
# Compute horizontal padding for every line
filler=$(( (columns - ${#slide_clean_lines[i]} + 1) / 2 )) # Add 1 to round up
printf "%${filler}s" " "
echo -e "${slide_formatted_lines[i]}"
done
# Block slide type
elif [[ "$format" == "block" ]]; then
for (( i = 0; i < lines_padding; i++ )); do
echo
done
# Compute horizontal padding for whole block
longest=0
for (( i = start; i < start + length; i++ )); do
if (( ${#slide_clean_lines[i]} > longest )); then
longest=${#slide_clean_lines[i]}
fi
done
filler=$(( (columns - longest + 1) / 2 )) # Add 1 to round up
for (( i = start; i < start + length; i++ )); do
printf "%${filler}s" " "
echo -e "${slide_formatted_lines[i]}"
done
# Code slide type
elif [[ "$format" =~ code ]]; then
code=$( sed -r -n 's/^code[[:space:]]+(.*)$/\1/p' <<< "$format" )
# Perform code formatting
text=$( for (( i = start; i < start + length; i++ )); do echo -e "${slide_raw_lines[i]}"; done )
formatted_text=$( highlight -S "$code" -s "$CODE_STYLE" -O xterm256 <<< "$text" )
readarray -t formatted_lines <<< "$formatted_text"
for (( i = 0; i < lines_padding; i++ )); do
echo
done
# Compute horizontal padding for whole code block
longest=0
for (( i = start; i < start + length; i++ )); do
if (( ${#slide_raw_lines[i]} > longest )); then
longest=${#slide_raw_lines[i]}
fi
done
filler=$(( (columns - longest + 1) / 2 )) # Add 1 to round up
for (( i = 0; i < length; i++ )); do
printf "%${filler}s" " "
echo -e "${formatted_lines[i]}"
done
# Section slide type
elif [[ "$format" == "section" ]]; then
# Shorter vertical padding because of decorations
for (( i = 0; i < lines_padding - 2; i++ )); do
echo
done
# Compute horizontal decoration padding
longest=0
for (( i = start; i < start + length; i++ )); do
if (( ${#slide_clean_lines[i]} > longest )); then
longest=${#slide_clean_lines[i]}
fi
done
line_filler=$(( (columns - longest + 1) / 2 - 1 )) # Add 1 to round up
line_length=$(( (longest - LEN_DECORATION + 1) / 2 + 1 )) # Add 1 to round up
# Upper decoration
printf "%${line_filler}s" " "
for (( i = 0; i < line_length; i++ )); do
printf "─"
done
printf "%s" "$SECTION_DECORATION"
for (( i = 0; i < line_length; i++ )); do
printf "─"
done
echo
echo
for (( i = start; i < start + length; i++ )); do
# Compute horizontal padding for every line
filler=$(( (columns - ${#slide_clean_lines[i]} + 1) / 2 )) # Add 1 to round up
# Also need to move one char right if line is one character longer
(( (longest - LEN_DECORATION) % 2 && filler++ ))
printf "%${filler}s" " "
echo -e "${slide_formatted_lines[i]}"
done
# Lower decoration
echo
printf "%${line_filler}s" " "
for (( i = 0; i < line_length; i++ )); do
printf "─"
done
printf "%s" "$SECTION_DECORATION"
for (( i = 0; i < line_length; i++ )); do
printf "─"
done
fi
}
show_status() {
local i
skip=$(( rows - length - lines_padding - 1 ))
if [[ "$format" == "section" ]]; then
(( skip-- ))
fi
for (( i = 0; i < skip; i++ )); do
echo
done
status_slide="$slide/$slides [${file##*/}]"
status_time=$( date +"%-k:%M" )
status_filler=$(( columns - ${#status_slide} - ${#status_time} - 2 ))
printf " %s%s%${status_filler}s%s%s" "$( tput setaf 240 )" "$status_slide" " " "$status_time" "$( tput sgr0 )"
}
hide_mouse_cursor() {
if (( unclutter_installed )); then
unclutter -idle 1 &
PID="$!"
fi
}
show_mouse_cursor() {
if (( unclutter_installed )); then
kill "$PID"
sleep 0.1
fi
}
# Check if syntax highlighter is installed
if ! command -v highlight &> /dev/null; then
echo "'highlight' command is required, but not installed. Exiting."
exit 1
fi
# Check if unclutter is installed
if command -v unclutter &> /dev/null; then
unclutter_installed=1
else
unclutter_installed=0
fi
slide=1 # Current slide
status=0 # Status line
read_slides
check_slides
should_show="!$?"
hide_mouse_cursor
while true; do
# Do not allow current slide number outside bounds
(( slide < 1 )) && (( slide = 1 ))
(( slide > slides )) && (( slide = slides ))
# Only show slide if checks passed
if (( should_show )); then
show_slide
if (( status )); then
show_status
fi
fi
# Read just one key press
read -rsn1 response
# Handle arrows, Page Up, Page Down, Home, End, and ESC
if [[ "$response" == $'\x1b' ]]; then
read -rsn1 -t "0.1" response
if [[ "$response" == "[" ]]; then
read -rsn1 -t "0.1" response
if [[ "$response" == "D" || "$response" == "5" ]]; then
(( slide-- ))
elif [[ "$response" == "C" || "$response" == "6" ]]; then
(( slide++ ))
elif [[ "$response" == "A" || "$response" == "H" ]]; then
(( slide = 1 ))
elif [[ "$response" == "B" || "$response" == "F" ]]; then
(( slide = slides ))
fi
else
break
fi
# Space or Enter, K
elif [[ "$response" == "" || "$response" == "k" ]]; then
(( slide++ ))
# Backspace, J
elif [[ "$response" == $'\x7F' || "$response" == "j" ]]; then
(( slide-- ))
# R
elif [[ "$response" == "r" ]]; then
read_slides
check_slides
should_show="!$?"
# B
elif [[ "$response" == "b" ]]; then
should_show=$(( ! should_show ))
prepare_terminal
# S
elif [[ "$response" == "s" ]]; then
(( status = ! status ))
# Q
elif [[ "$response" == "q" ]]; then
break
# P
elif [[ "$response" == "p" ]]; then
# Check if ImageMagick is installed
if ! command -v import &> /dev/null && ! command -v convert &> /dev/null; then
echo "ImageMagick is required, but not installed. Export to PDF disabled."
sleep 2
# Check if xprop is installed
elif ! command -v xprop &> /dev/null; then
echo "'xprop' is required, but not installed. Export to PDF disabled."
sleep 2
else
rm -rf export
mkdir -p export
active_window=$( xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2 )
for (( slide = 1; slide <= slides; slide++ )); do
show_slide
sleep 0.1
import -window "$active_window" "export/$( printf "%04d" "$slide" ).png"
done
convert "export/*.png" "${file}.pdf"
restore_terminal
break
fi
fi
done
show_mouse_cursor
restore_terminal