Initial implementation of slider
Single-file markdown slideshow for the terminal. Parses slides split by `---`, renders each with glow, and navigates with n/p/q or arrow keys. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
93
bin/slider
Executable file
93
bin/slider
Executable file
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# slider — terminal markdown slideshow
|
||||
# Usage: slider <file.md>
|
||||
|
||||
usage() {
|
||||
echo "Usage: slider <file.md>" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
[[ $# -ne 1 ]] && usage
|
||||
file="$1"
|
||||
[[ ! -f "$file" || ! -r "$file" ]] && { echo "Error: cannot read '$file'" >&2; exit 1; }
|
||||
|
||||
# Parse slides split on "---"
|
||||
slides=()
|
||||
current=""
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
if [[ "$line" == "---" ]]; then
|
||||
[[ -n "${current// }" ]] && slides+=("$current")
|
||||
current=""
|
||||
else
|
||||
current+="$line"$'\n'
|
||||
fi
|
||||
done < "$file"
|
||||
[[ -n "${current// }" ]] && slides+=("$current")
|
||||
|
||||
total=${#slides[@]}
|
||||
[[ $total -eq 0 ]] && { echo "No slides found in '$file'" >&2; exit 1; }
|
||||
|
||||
# Temp file for glow rendering; cleaned up on exit
|
||||
tmpfile=$(mktemp /tmp/slider.XXXXXX.md)
|
||||
trap 'rm -f "$tmpfile"' EXIT
|
||||
|
||||
idx=0
|
||||
|
||||
show_slide() {
|
||||
tput clear
|
||||
printf " Slide %d/%d [n] next [p] prev [q] quit\n\n" $((idx + 1)) "$total"
|
||||
printf '%s' "${slides[$idx]}" > "$tmpfile"
|
||||
glow - < "$tmpfile"
|
||||
}
|
||||
|
||||
show_slide
|
||||
|
||||
while true; do
|
||||
read -rsn1 key < /dev/tty
|
||||
case "$key" in
|
||||
n)
|
||||
if (( idx < total - 1 )); then
|
||||
idx=$(( idx + 1 ))
|
||||
show_slide
|
||||
else
|
||||
tput clear
|
||||
printf " Slide %d/%d [n] next [p] prev [q] quit\n\n" $((idx + 1)) "$total"
|
||||
printf '%s' "${slides[$idx]}" > "$tmpfile"
|
||||
glow - < "$tmpfile"
|
||||
printf "\n End of presentation — press q to quit\n"
|
||||
fi
|
||||
;;
|
||||
p)
|
||||
if (( idx > 0 )); then
|
||||
idx=$(( idx - 1 ))
|
||||
show_slide
|
||||
fi
|
||||
;;
|
||||
$'\x1b')
|
||||
read -rsn2 -t 0.1 rest < /dev/tty || true
|
||||
if [[ "$rest" == "[C" ]]; then # right arrow → next
|
||||
if (( idx < total - 1 )); then
|
||||
idx=$(( idx + 1 ))
|
||||
show_slide
|
||||
else
|
||||
tput clear
|
||||
printf " Slide %d/%d [n] next [p] prev [q] quit\n\n" $((idx + 1)) "$total"
|
||||
printf '%s' "${slides[$idx]}" > "$tmpfile"
|
||||
glow - < "$tmpfile"
|
||||
printf "\n End of presentation — press q to quit\n"
|
||||
fi
|
||||
elif [[ "$rest" == "[D" ]]; then # left arrow → prev
|
||||
if (( idx > 0 )); then
|
||||
idx=$(( idx - 1 ))
|
||||
show_slide
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
q)
|
||||
tput clear
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
Reference in New Issue
Block a user