Changeset - 626f4a46ba00
[Not reviewed]
0 1 0
Branko Majic (branko) - 9 months ago 2025-03-07 19:35:30
branko@majic.rs
[cheatsheet_viewer.sh] Keep track of (and display) last opened cheatsheet when using the toggle command.
1 file changed with 23 insertions and 3 deletions:
0 comments (0 inline, 0 general)
utils/cheatsheet_viewer.sh
Show inline comments
 
@@ -101,13 +101,15 @@ toggle [PROFILE]
 

	
 
    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.
 
  cheatsheet-specific pqiv is present. Thex toggle command also keeps
 
  track of which cheatsheet was last selected, and will update the
 
  image viewer to point to it when opened.
 

	
 
$PROGRAM accepts the following options:
 

	
 
    -q
 
        Quiet mode.
 
    -d
 
@@ -297,14 +299,15 @@ function command_display() {
 
#
 
function command_toggle() {
 
    local config_dir="$1"
 
    local profile="$2"
 
    local profile_dir="$config_dir/profiles/$profile"
 
    local cheatsheets_file="$profile_dir/cheatsheets"
 
    local last_cheatsheet_file="$profile_dir/last"
 

	
 
    local current_process_id current_profile
 
    local current_process_id current_profile last_cheatsheet
 

	
 
    local cheatsheets=()
 

	
 
    if [[ ! -d $profile_dir ]]; then
 
        error "No such profile found under $profile_dir"
 
        return 1
 
@@ -316,12 +319,18 @@ function command_toggle() {
 

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

	
 
    if [[ -e $last_cheatsheet_file ]]; then
 
        last_cheatsheet=$(< "$last_cheatsheet_file")
 
    else
 
        last_cheatsheet=""
 
    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
 
@@ -336,13 +345,24 @@ function command_toggle() {
 
            "--zoom-level=1.0"
 
            "--transparent-background"
 
            "--hide-info-box"
 
            "--window-title=cheatsheet-viewer-$profile"
 
        )
 

	
 
        pqiv "${pqiv_options[@]}" "${cheatsheets[@]}" &
 
        if [[ -n $last_cheatsheet && -f $last_cheatsheet ]]; then
 
            pqiv_options+=("--action=goto_file_byname($last_cheatsheet); set_status_output(1)")
 
        else
 
            pqiv_options+=("--action=set_status_output(1)")
 
        fi
 

	
 
        (
 
            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=')
 
        ) &
 
    fi
 
}
 

	
 

	
 
# Set-up colours for message printing if we're not piping and terminal is
 
# capable of outputting the colors.
0 comments (0 inline, 0 general)