From 41ee6a32070a4969a7ce7224fa2b00fbb01856e1 2025-03-19 23:08:05 From: Branko Majic Date: 2025-03-19 23:08:05 Subject: [PATCH] [cheatsheet_viewer.sh] Added support for automatically activating overlay mode based on current window title. --- diff --git a/utils/cheatsheet_viewer.sh b/utils/cheatsheet_viewer.sh index c8365d1f3d6c5382db14fd08b4c41a488905fb36..23401be18454c95f697c115a7502b16e7a43050b 100755 --- a/utils/cheatsheet_viewer.sh +++ b/utils/cheatsheet_viewer.sh @@ -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