shlide (view raw)
1#!/usr/bin/env bash
2#
3# Usage: shlide path/to/slides/
4# Each slide is a textfile under path/to/slides
5
6# Color definitions
7
8BLK="\e[30m"
9RED="\e[31m"
10GRN="\e[32m"
11YLW="\e[33m"
12BLU="\e[34m"
13PUR="\e[35m"
14CYN="\e[36m"
15RST="\e[0m"
16
17lines() {
18 mapfile -tn 0 lines < "$1"
19 printf '%s\n' "${#lines[@]}"
20}
21
22longest_line() {
23 max=0 IFS=
24 while read -r line; do
25 if [ "${#line}" -gt "$max" ]; then max="${#line}"; fi
26 done < "$1"
27 printf '%s\n' "$max"
28}
29
30display() {
31 # 1 - slide contents
32 # 2 - slide name
33
34 slide_contents="$1"
35
36 # Hides the cursor.
37 printf '\e[?25l'
38
39 # Clear the screen.
40 printf '\e[2J'
41
42 # Move the cursor to the center.
43 read -r LINES COLUMNS < <(stty -F /dev/tty size)
44 height=$(lines "$2")
45 width=$(longest_line "$2")
46
47 # Rough estimates for the true center.
48 ((l=$LINES/2 - $height/2))
49 ((c=$COLUMNS/2 - $width/2))
50
51 printf '\e[%s;%sH' "$l" "$c"
52
53 while IFS= read -r line; do
54 # Print the contents of the slide file,
55 # line by line.
56 printf "%s" "$line"
57 # Move down and back after each print.
58 printf '\e[%sD\e[B' "${#line}"
59 done <<< "$slide_contents"
60
61}
62
63main() {
64
65 slides_dir="${1:-./}"
66 slides=("$slides_dir"/[0-9]*.txt)
67 i=0
68 while true; do
69
70 # Exit after last slide.
71 [[ "$i" -gt "$((${#slides[@]} - 1))" ]] && {
72 printf '\e[?25h'
73 exit
74 }
75
76 # Don't go below 0.
77 [[ "$i" -lt 0 ]] && i=0
78
79 # Navigate on j/k/n/p and quit on q.
80 display "$(<${slides[$i]})" "${slides[$i]}"
81 read -rsn1 input
82 case "$input" in
83 "j"|"n")
84 ((++i))
85 ;;
86 "k"|"p")
87 ((--i))
88 ;;
89 "q")
90 # Return the cursor on exit.
91 printf '\e[?25h'
92 exit
93 ;;
94 esac
95 done
96
97 # Return the cursor.
98 printf '\e[?25h'
99
100}
101
102main "$@"