shlide (view raw)
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 |
#!/usr/bin/env bash # shellcheck disable=2154 # # shlide - a slide deck presentation tool written in pure bash # https://github.com/icyphox/shlide # # The MIT License (MIT) # # Copyright (c) 2020 Anirudh Oppiliappan <x@icyphox.sh> # Color definitions. export BLK="\e[30m" export RED="\e[31m" export GRN="\e[32m" export YLW="\e[33m" export BLU="\e[34m" export PUR="\e[35m" export CYN="\e[36m" export RST="\e[0m" # Other formatting. export BLD="\e[1m" export DIM="\e[2m" export ITA="\e[3m" export UND="\e[4m" export FLS="\e[5m" export REV="\e[7m" export INV="\e[8m" export STR="\e[9m" lines() { mapfile -tn 0 lines < "$1" printf '%s\n' "${#lines[@]}" } longest_line() { max=0 local IFS= while read -r line; do l=$(ansi_filter "$(colorify "$line")") if [ "${#l}" -gt "$max" ]; then max="${#l}"; fi done < "$1" printf '%s\n' "$max" } colorify() { # 'eval' hack to achieve substitution for colors. # Exclude SC2154. eval "declare dummy=\"$1\"" printf '%b' "$dummy" } # Filter out color sequences. ansi_filter() { shopt -s extglob local IFS= printf '%s' "${1//$'\e'[\[(]*([0-9;])[@-n]/}" #" A little fix to prevent vim syntax highlighting from breaking. shopt -u extglob } display() { # 1 - slide contents # 2 - slide name # 3 - current slide nr. # 4 - total nr. of slides slide_contents="$1" # Hide the cursor. printf '\e[?25l' # Clear the screen. printf '\e[2J' # Get screen size. case "$OSTYPE" in "darwin"*) read -r LINES COLUMNS < <(stty -f /dev/tty size) ;; *) read -r LINES COLUMNS < <(stty -F /dev/tty size) ;; esac # Write slide number at bottom left. printf '\e[H' printf '\e[%sB%s/%s' "$LINES" "$(($3+1))" "$4" # Custom calculations to center text. height=$(lines "$2") width=$(longest_line "$2") # Rough estimates for the true center. ((l=LINES/2 - height/2)) ((c=COLUMNS/2 - width/2)) printf '\e[%s;%sH' "$l" "$c" while IFS= read -r line; do reduce=0 # Print the contents of the slide file, # line by line. l=$(colorify "$line") printf '%s' "$l" case $line in "" | "\n") ((++reduce));; esac # Move down and back after each print. l=$(ansi_filter "$l") printf '\e[%sD\e[B' "$((${#l} - reduce))" done <<< "$slide_contents" } die() { printf '\e[2J' printf '\e[?25h' exit 0 } display_end() { ((l=LINES/2)) ((c=COLUMNS/2 - 8)) printf '\e[2J' printf '\e[0;%sH' "$c" printf 'END. Press q to quit.' } main() { slides_dir="${1:-./}" slides=("$slides_dir"/[0-9]*.txt) total="${#slides[@]}" i=0 while :; do # Clear the screen. printf '\e[2J' # Capture Ctrl+C. trap 'die' INT # Don't go below 0. [[ "$i" -lt 0 ]] && i=0 # Display END reached prompt, and then exit. if [[ "$i" -eq "$total" ]]; then display_end else display "$(<"${slides[$i]}")" "${slides[$i]}" "$i" "$total" fi # Navigation read -rsn1 input case "$input" in "j"|"n"|""|";") [[ "$i" -eq "$total" ]] && die ((++i)) ;; "k"|"p"|""|",") ((--i)) ;; "0") ((i=0)) ;; "G") ((i=total-1)) ;; "q") # Return the cursor on exit. die ;; *) # Reloads the slide ;; esac done # Return the cursor. printf '\e[?25h' } main "$@" |