Add BOLLUX_PRE_DISPLAY and functionality (fix #1)
authorCase Duckworth <acdw@acdw.net>
Sat, 27 Feb 2021 02:37:51 +0000 (20:37 -0600)
committerCase Duckworth <acdw@acdw.net>
Sat, 27 Feb 2021 02:37:51 +0000 (20:37 -0600)
Configuration option: BOLLUX_PRE_DISPLAY
- comma-separated list of following options: 'pre', 'alt', 'both'
- default: 'pre,alt,both'

Keybind: `
- when pressed, will cycle through the options in BOLLUX_PRE_DISPLAY

Full documentation coming in the next commit.

bollux

diff --git a/bollux b/bollux
index 07e12f0..df27c92 100755 (executable)
--- a/bollux
+++ b/bollux
@@ -50,6 +50,20 @@ trim_string() { # trim_string STRING
        printf '%s\n' "$_"
 }
 
+# cycle a variable, e.g. from 'one,two,three' => 'two,three,one'
+cycle_list() { # cycle_list LIST DELIM
+       local list="${!1}" delim="$2"
+       local first="${list%%${delim}*}"
+       local rest="${list#*${delim}}"
+       printf -v "$1" '%s%s%s' "${rest}" "${delim}" "${first}"
+}
+
+# determine the first element of a list, e.g. 'one,two,three' => 'one'
+first() { # first LIST DELIM
+       local list="${!1}" delim="$2"
+       printf '%s\n' "${list%%${delim}*}"
+}
+
 log() { # log LEVEL MESSAGE
        [[ "$BOLLUX_LOGLEVEL" == QUIET ]] && return
        local fmt
@@ -122,6 +136,7 @@ bollux_config() {
        : "${BOLLUX_PROTO:=gemini}"                    # default protocol
        : "${BOLLUX_URL:=}"                            # start url
        : "${BOLLUX_BYEMSG:=See You Space Cowboy ...}" # bye message
+       : "${BOLLUX_PRE_DISPLAY:=pre,alt,both}"        # how to view PRE blocks
        ## files
        : "${BOLLUX_DATADIR:=${XDG_DATA_DIR:-$HOME/.local/share}/bollux}"
        : "${BOLLUX_DOWNDIR:=.}"                       # where to save downloads
@@ -720,7 +735,7 @@ less_prompt_escape() { # less_prompt_escape STRING
 
 # generate a lesskey(1) file for custom keybinds
 mklesskey() { # mklesskey FILENAME
-       lesskey -o "$1" - <<-END
+       lesskey -o "$1" - <<-\END
                #command
                o quit 0 # 48 open a link
                g quit 1 # 49 goto a url
@@ -728,8 +743,9 @@ mklesskey() { # mklesskey FILENAME
                ] quit 3 # 51 forward
                r quit 4 # 52 re-request / download
                G quit 5 # 53 goto a url (pre-filled)
+               ` quit 6 # 54 cycle BOLLUX_PRE_DISPLAY and refresh
                # other keybinds
-               \\40 forw-screen-force
+               \40 forw-screen-force
                h left-scroll
                l right-scroll
                ? status   # 'status' will show a little help thing.
@@ -767,15 +783,26 @@ typeset_gemini() {
 
        log d "T_WIDTH=$T_WIDTH"
        log d "WIDTH=$WIDTH"
+       log d "$BOLLUX_PRE_DISPLAY"
 
        while IFS= read -r; do
                case "$REPLY" in
                '```'*)
+                       PRE_LINE_FORCE=false
                        if $pre; then
                                pre=false
                        else
                                pre=true
                        fi
+                       case "${BOLLUX_PRE_DISPLAY%%,*}" in
+                       pre)
+                               :
+                               ;;
+                       alt | both)
+                               $pre && PRE_LINE_FORCE=true \
+                                       gemini_pre "${REPLY#\`\`\`}"
+                               ;;
+                       esac
                        continue
                        ;;
                '=>'*)
@@ -881,8 +908,12 @@ gemini_text() {
 }
 
 gemini_pre() {
-       printf "\e[${C_SIGIL}m%${S_MARGIN}s " '```'
-       printf "\e[${C_PRE}m%s${C_RESET}\n" "$1"
+       # Print preformatted text, dependent on $BOLLUX_PRE_DISPLAY and
+       # $PRE_LINE_FORCE
+       if [[ alt != "${BOLLUX_PRE_DISPLAY%%,*}" ]] || $PRE_LINE_FORCE; then
+               printf "\e[${C_SIGIL}m%${S_MARGIN}s " '```'
+               printf "\e[${C_PRE}m%s${C_RESET}\n" "$1"
+       fi
 }
 
 # wrap lines on words to WIDTH
@@ -976,7 +1007,11 @@ handle_keypress() { # handle_keypress CODE
                run prompt -u GO
                run blastoff -u "$REPLY"
                ;;
-       *) # 54-57 -- still available for binding
+       54) # ` - change alt-text visibility and refresh
+               run cycle_list BOLLUX_PRE_DISPLAY ,
+               run blastoff "$BOLLUX_URL"
+               ;;
+       55) # 55-57 -- still available for binding
                die "$?" "less(1) error"
                ;;
        esac