all repos — shlide @ 7b8ed609c722d87f53ab8d59277ff5a3500e0bc3

slide deck presentation tool written in pure bash

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
#!/usr/bin/env bash
# 
# Usage: shlide path/to/slides/
# Each slide is a textfile under path/to/slides

# Color definitions

BLK="\e[30m"
RED="\e[31m"
GRN="\e[32m"
YLW="\e[33m"
BLU="\e[34m"
PUR="\e[35m"
CYN="\e[36m"
RST="\e[0m"

lstrip() {
    # See: https://github.com/dylanaraps/pure-bash-bible#strip-pattern-from-start-of-string
    printf '%s\n' "${1##$2}"
}

lines() {
    mapfile -tn 0 lines < "$1"
    printf '%s\n' "${#lines[@]}"
}

longest_line() {
    max=0 IFS=
    while read -r line; do
        if [ "${#line}" -gt "$max" ]; then max="${#line}"; fi
    done < "$1"
    printf '%s\n' "$max"
}

get_term_size() {
    # POSIX alternative to 'checkwinsize'.
    read -r LINES COLUMNS < <(stty -F /dev/tty size)

}

# navigate() {
#     case "$1" in
#         "j") 
# }

display() {
    # 1 - slide contents
    # 2 - slide name

    slide_contents="$1"
    # slide_name="$(lstrip $2 "[0-9]-")"

    # Hides the cursor.
    printf '\e[?25l'

    # Clear the screen.
    printf '\e[2J'

    # Move the cursor to the center.
    read -r LINES COLUMNS < <(stty -F /dev/tty size)
    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
        # Print the contents of the slide file, 
        # line by line.
        printf "%s" "$line"
        # Move down and back after each print.
        printf '\e[%sD\e[B' "${#line}"
    done <<< "$slide_contents"

    # Change slide on space.
    read -rsn1 input
    #navigate "$input"
}

main() {
    
    slides_dir="$1"

    for f in "$slides_dir"/[0-9]*.txt; do
        f_contents="$(<$f)"
        display "$f_contents" "$f"
    done

    # Return the cursor.
    printf '\e[?25h'

}

main "$@"