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
 
@@ -69,6 +69,8 @@ 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.
 
@@ -264,9 +266,12 @@ function command_display() {
 

	
 
    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"
 
@@ -277,16 +282,30 @@ function command_display() {
 
        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"
 
@@ -297,7 +316,8 @@ function command_display() {
 

	
 
    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
 
@@ -335,10 +355,12 @@ function command_toggle() {
 
    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"
 
@@ -349,6 +371,20 @@ function command_toggle() {
 
        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
 
@@ -372,8 +408,9 @@ function command_toggle() {
 
    # 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=(
 
@@ -397,7 +434,8 @@ function command_toggle() {
 
            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
0 comments (0 inline, 0 general)