Changeset - 41ee6a32070a
[Not reviewed]
master
0 1 0
Branko Majic (branko) - 8 months ago 2025-03-19 23:08:05
branko@majic.rs
[cheatsheet_viewer.sh] Added support for automatically activating overlay mode based on current window title.
1 file changed with 45 insertions and 7 deletions:
0 comments (0 inline, 0 general)
utils/cheatsheet_viewer.sh
Show inline comments
 
@@ -66,12 +66,14 @@ activated. Base directory for profiles is:
 
Each profile definition consists out of a number of configuration
 
files:
 

	
 
  - ~/.config/cheatsheet_viewer/profiles/PROFILE/cheatsheets (list of
 
    cheatsheet files, one per line, blank lines are ignored, lines
 
    starting with '#' are ignored)
 
  - ~/.config/cheatsheet_viewer/profiles/PROFILE/overlay (list of
 
    window titles for which to automatically apply the overlay mode)
 

	
 
Multiple commands are provided for working with the profiles and
 
displaying the cheatsheets.
 

	
 
list
 

	
 
@@ -261,46 +263,64 @@ function command_display() {
 
    local config_dir="$1"
 
    local profile="$2"
 
    local overlay_mode="$3"
 

	
 
    local profile_dir="$config_dir/profiles/$profile"
 
    local cheatsheets_file="$profile_dir/cheatsheets"
 
    local overlay_limits_file="$profile_dir/overlay"
 

	
 
    local caller_window_id caller_window_title window_title
 

	
 
    local caller_window_id
 
    local cheatsheets=()
 
    local -A overlay_limits
 

	
 
    if [[ ! -d $profile_dir ]]; then
 
        error "No such profile found under $profile_dir"
 
        return 1
 
    fi
 

	
 
    if [[ -e $cheatsheets_file ]]; then
 
        readarray -t cheatsheets < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$cheatsheets_file")
 
    fi
 

	
 
    if (( overlay_mode )); then
 
        overlay_limits[_ALL_]=1
 
    elif [[ -e $overlay_limits_file ]]; then
 
        while read -r window_title; do
 
            if [[ -n $window_title ]]; then
 
                overlay_limits[$window_title]=1
 
            fi
 
        done < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$overlay_limits_file")
 
    else
 
        # Means that cheatsheets are not shown as overlays. Assignment
 
        # introduced to avoid unbound variable errors in checks.
 
        overlay_limits[_DISABLED_]=1
 
    fi
 

	
 
    if (( ${#cheatsheets[@]} == 0 )); then
 
        warning "No cheatsheets are defined under $cheatsheets_file"
 
        return 0
 
    fi
 

	
 
    if (( overlay_mode )); then
 
    if (( ! overlay_limits[_DISABLED_] )); then
 
        caller_window_id=$(xprop -root | grep '^_NET_ACTIVE_WINDOW(WINDOW)' | sed -e 's/.*window id # //')
 
        caller_window_title=$(xprop -id "$caller_window_id" _NET_WM_NAME | sed -Ee 's/^[^=]+ = "//;s/"$//')
 
    fi
 

	
 

	
 
    local pqiv_options=(
 
        "--scale-mode-screen-fraction=1.0"
 
        "--zoom-level=1.0"
 
        "--transparent-background"
 
        "--hide-info-box"
 
        "--window-title=cheatsheet-viewer-$profile"
 
    )
 

	
 
    pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" &
 

	
 
    if (( overlay_mode )); then
 
    # The ${caller_window_title-_} is used to avoid invalid array subscript.
 
    if (( overlay_limits[_ALL_] || overlay_limits[${caller_window_title-_}] )); then
 
        local counter=0
 
        while (( counter < 10 )) && ! wmctrl -l | grep -q "cheatsheet-viewer-$profile"; do
 
            sleep 0.05
 
            (( counter+=1 ))
 
        done
 

	
 
@@ -332,26 +352,42 @@ function command_toggle() {
 
    local profile="$2"
 
    local overlay_mode="$3"
 

	
 
    local profile_dir="$config_dir/profiles/$profile"
 
    local cheatsheets_file="$profile_dir/cheatsheets"
 
    local last_cheatsheet_file="$profile_dir/last"
 
    local overlay_limits_file="$profile_dir/overlay"
 

	
 
    local current_process_id current_profile last_cheatsheet caller_window_id
 
    local current_process_id current_profile last_cheatsheet caller_window_id caller_window_title window_title
 

	
 
    local cheatsheets=()
 
    local -A overlay_limits
 

	
 
    if [[ ! -d $profile_dir ]]; then
 
        error "No such profile found under $profile_dir"
 
        return 1
 
    fi
 

	
 
    if [[ -e $cheatsheets_file ]]; then
 
        readarray -t cheatsheets < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$cheatsheets_file")
 
    fi
 

	
 
    if (( overlay_mode )); then
 
        overlay_limits[_ALL_]=1
 
    elif [[ -e $overlay_limits_file ]]; then
 
        while read -r window_title; do
 
            if [[ -n $window_title ]]; then
 
                overlay_limits[$window_title]=1
 
            fi
 
        done < <(grep --invert-match --extended-regexp "(^[[:blank:]]*#|^[[:blank:]]*$)" "$overlay_limits_file")
 
    else
 
        # Means that cheatsheets are not shown as overlays. Assignment
 
        # introduced to avoid unbound variable errors in checks.
 
        overlay_limits[_DISABLED_]=1
 
    fi
 

	
 
    if (( ${#cheatsheets[@]} == 0 )); then
 
        warning "No cheatsheets are defined under $cheatsheets_file"
 
        return 0
 
    fi
 

	
 
    if [[ -e $last_cheatsheet_file ]]; then
 
@@ -369,14 +405,15 @@ function command_toggle() {
 
        kill "$current_process_id"
 
    fi
 

	
 
    # If requested profile is not the same as current one, we want to
 
    # show cheatsheets from requested profile.
 
    if [[ $current_profile != "$profile" ]]; then
 
        if (( overlay_mode )); then
 
        if (( ! overlay_limits[_DISABLED_] )); then
 
            caller_window_id=$(xprop -root | grep '^_NET_ACTIVE_WINDOW(WINDOW)' | sed -e 's/.*window id # //')
 
            caller_window_title=$(xprop -id "$caller_window_id" _NET_WM_NAME | sed -Ee 's/^[^=]+ = "//;s/"$//')
 
        fi
 

	
 
        local pqiv_options=(
 
            "--scale-mode-screen-fraction=1.0"
 
            "--zoom-level=1.0"
 
            "--transparent-background"
 
@@ -394,13 +431,14 @@ function command_toggle() {
 
            while read -r line; do
 
                eval "$line"
 
                echo "$CURRENT_FILE_NAME" > "$profile_dir/last"
 
            done < <(pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" | grep --line-buffered '^CURRENT_FILE_NAME=')
 
        ) &
 

	
 
        if (( overlay_mode )); then
 
        # The ${caller_window_title-_} is used to avoid invalid array subscript.
 
        if (( overlay_limits[_ALL_] || overlay_limits[${caller_window_title-_}] )); then
 
            local counter=0
 
            while (( counter < 10 )) && ! wmctrl -l | grep -q "cheatsheet-viewer-$profile"; do
 
                sleep 0.05
 
                (( counter+=1 ))
 
            done
 

	
0 comments (0 inline, 0 general)