From 626f4a46ba00b2d2d3205cef44565fd7e9b0f7e5 2025-03-07 19:35:30 From: Branko Majic Date: 2025-03-07 19:35:30 Subject: [PATCH] [cheatsheet_viewer.sh] Keep track of (and display) last opened cheatsheet when using the toggle command. --- diff --git a/utils/cheatsheet_viewer.sh b/utils/cheatsheet_viewer.sh index 863a02ff09e0806629e7d170dac1393ec72fe8d4..e9cd58547aedc214e19f718c25c4e5e17349af13 100755 --- a/utils/cheatsheet_viewer.sh +++ b/utils/cheatsheet_viewer.sh @@ -104,7 +104,9 @@ toggle [PROFILE] 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: @@ -300,8 +302,9 @@ function command_toggle() { 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=() @@ -319,6 +322,12 @@ function command_toggle() { 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-//') @@ -339,7 +348,18 @@ function command_toggle() { "--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 }