Changeset - ac4dfe8bc978
[Not reviewed]
0 1 0
Branko Majic (branko) - 1 month ago 2025-03-07 19:13:08
branko@majic.rs
[cheatsheet_viewer.sh] Implemented toggle command for showing cheatsheets.
1 file changed with 83 insertions and 0 deletions:
0 comments (0 inline, 0 general) First comment
utils/cheatsheet_viewer.sh
Show inline comments
 
@@ -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"
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now