From ac4dfe8bc978a56e0073ba40f2a41256f4209d30 2025-03-07 19:13:08 From: Branko Majic Date: 2025-03-07 19:13:08 Subject: [PATCH] [cheatsheet_viewer.sh] Implemented toggle command for showing cheatsheets. --- diff --git a/utils/cheatsheet_viewer.sh b/utils/cheatsheet_viewer.sh index 8343b3e4aa94a6586d79369eb6019b27b99d0786..863a02ff09e0806629e7d170dac1393ec72fe8d4 100755 --- a/utils/cheatsheet_viewer.sh +++ b/utils/cheatsheet_viewer.sh @@ -32,6 +32,7 @@ Usage: $PROGRAM [OPTIONS] list $PROGRAM [OPTIONS] info PROFILE $PROGRAM [OPTIONS] display [PROFILE] + $PROGRAM [OPTIONS] toggle [PROFILE] EOF } @@ -94,6 +95,17 @@ display [PROFILE] Displays cheatsheets from the specified profile (default profile name is 'default'). +toggle [PROFILE] + + Arguments: + + PROFILE (profile name) + + Toggles display of cheatsheets from the specified profile (default + profile name is 'default'). Unlike the display command, the toggle + command is more intelligent and ensures that only one instance of + cheatsheet-specific pqiv is present. + $PROGRAM accepts the following options: -q @@ -269,6 +281,69 @@ function command_display() { } +# +# Toggles display of configured cheatsheets from a profile. +# +# Arguments: +# +# $1 (config_dir) +# Base directory under which all configuration files are stored. +# +# $2 (profile) +# Profile name. +# +# Returns: +# 0 on success, 1 otherwise. +# +function command_toggle() { + local config_dir="$1" + local profile="$2" + local profile_dir="$config_dir/profiles/$profile" + local cheatsheets_file="$profile_dir/cheatsheets" + + local current_process_id current_profile + + local cheatsheets=() + + 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 (( ${#cheatsheets[@]} == 0 )); then + warning "No cheatsheets are defined under $cheatsheets_file" + return 0 + fi + + # Get information about existing running cheatsheet viewer pqiv instance (if any). + current_process_id=$(wmctrl -lp | grep 'cheatsheet-viewer-' | awk '{print $3}') + current_profile=$(wmctrl -lp | grep 'cheatsheet-viewer-' | awk '{print $5}' | sed -e 's/cheatsheet-viewer-//') + + # Kill off the existing running process. + if [[ -n $current_process_id ]]; then + 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 + 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[@]}" & + fi +} + + # Set-up colours for message printing if we're not piping and terminal is # capable of outputting the colors. _COLOR_TERMINAL=$(tput colors 2>&1) @@ -291,6 +366,7 @@ else fi # Set-up functions for printing coloured messages. + function debug() { if [[ $DEBUG != 0 ]]; then echo "${_TEXT_BOLD}${_TEXT_BLUE}[DEBUG]${_TEXT_RESET}" "$@" @@ -382,6 +458,13 @@ elif [[ $COMMAND == display ]]; then command_display "$CONFIG_DIR" "$PROFILE" +elif [[ $COMMAND == toggle ]]; then + + PROFILE="${1-default}" + shift + + command_toggle "$CONFIG_DIR" "$PROFILE" + else error "Unsupported command: $COMMAND"