From 59677f3b9ca9b792f76b0a16eef0203eca8f187c Mon Sep 17 00:00:00 2001
From: Marty Oehme <marty.oehme@gmail.com>
Date: Fri, 3 Jan 2025 22:32:55 +0100
Subject: [PATCH] qutebrowser: Include sessions into qutedmenu script

If 'yq' is found on the system, the qutedmenu script will now also
traverse any saved sessions for urls (either active or historic) and
display those as well.

Also added a simple check for the sqlite3 and yq tools and spit out a
warning if either isn't found. Program will continue unhindered and just
ignore the history/session urls respectively.
---
 qutebrowser/scripts/qutedmenu | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/qutebrowser/scripts/qutedmenu b/qutebrowser/scripts/qutedmenu
index a6b69b8..d32b47f 100755
--- a/qutebrowser/scripts/qutedmenu
+++ b/qutebrowser/scripts/qutedmenu
@@ -7,6 +7,11 @@
 #:bind o spawn --userscript /path/to/userscripts/qutedmenu open
 #:bind O spawn --userscript /path/to/userscripts/qutedmenu tab
 
+warn() {
+    echo "$1" >&2
+}
+
+
 readonly confdir=${XDG_CONFIG_HOME:-$HOME/.config}
 readonly datadir=${XDG_DATA_HOME:-$HOME/.local/share}
 readonly optsfile=$confdir/dmenu/bemenucolors
@@ -24,8 +29,21 @@ create_menu() {
         printf -- '%s\n' "$url"
     done <"$QUTE_CONFIG_DIR"/bookmarks/urls
 
+    # Saved sessions
+    if type yq >/dev/null 2>&1; then
+        while read -r url; do
+            printf -- '%s\n' "$url"
+        done < <(yq ".windows.[].tabs.[].history.[].url" "${QUTE_DATA_DIR}"/sessions/*.yml | sed -e 's/^"\(.*\)"$/\1/')
+    else
+        warn "Did not find yq executable, not searching saved sessions."
+    fi
+
     # Finally history
-    printf -- '%s\n' "$(sqlite3 -separator ' ' "$QUTE_DATA_DIR/history.sqlite" 'select title, url from CompletionHistory')"
+    if type sqlite3 >/dev/null 2>&1; then
+        printf -- '%s\n' "$(sqlite3 -separator ' ' "$QUTE_DATA_DIR/history.sqlite" 'select title, url from CompletionHistory')"
+    else
+        warn "Did not find sqlite3 executable, not searching history."
+    fi
 }
 
 get_selection() {
@@ -56,5 +74,6 @@ url=${url/*http/http}
 
 case $1 in
 open) printf '%s' "open $url" >>"$QUTE_FIFO" || qutebrowser "$url" ;;
+print) echo "$url" ;;
 tab | *) printf '%s' "open -t $url" >>"$QUTE_FIFO" || qutebrowser "$url" ;;
 esac