From c2f57110a7a3a06f00aace4d65d46cfda7dd83d9 Mon Sep 17 00:00:00 2001 From: tonybanters Date: Thu, 11 Sep 2025 09:16:32 -0700 Subject: [PATCH] Added rofi script, and made it more modular for different terminals. --- bookmarks-dmenu.sh | 4 +- bookmarks-rofi.sh | 88 +++++++++++++++++ configs-dmenu.sh | 43 -------- readme.org | 238 +++++++++++++++++++++++++++++++++++++++++++++ repos-dmenu.sh | 10 +- repos-rofi.sh | 21 ++++ tmux-dmenu.sh | 63 ++++++------ 7 files changed, 390 insertions(+), 77 deletions(-) create mode 100755 bookmarks-rofi.sh delete mode 100755 configs-dmenu.sh create mode 100644 readme.org create mode 100755 repos-rofi.sh diff --git a/bookmarks-dmenu.sh b/bookmarks-dmenu.sh index 116032c..54732e3 100755 --- a/bookmarks-dmenu.sh +++ b/bookmarks-dmenu.sh @@ -2,13 +2,15 @@ set -eu # Files +# Put your files in .config/bookmarks/. PERS_FILE="${PERS_FILE:-$HOME/.config/bookmarks/personal.txt}" WORK_FILE="${WORK_FILE:-$HOME/.config/bookmarks/work.txt}" # dmenu look -DMENU="dmenu -i -c -bw 3 -W 900 -l 30 -h 40 -F -fn 'JetBrainsMono Nerd Font:size=16' -p 'Bookmarks:'" +DMENU="dmenu -i -vi -c -bw 3 -W 900 -l 30 -h 40 -F -fn 'JetBrainsMono Nerd Font:size=16' -p 'Bookmarks:'" # Browsers +# Choose your browsers accordingly FIREFOX="$(command -v firefox || true)" BRAVE="$(command -v brave || command -v brave-browser || true)" FALLBACK="$(command -v xdg-open || echo firefox)" diff --git a/bookmarks-rofi.sh b/bookmarks-rofi.sh new file mode 100755 index 0000000..b27b013 --- /dev/null +++ b/bookmarks-rofi.sh @@ -0,0 +1,88 @@ +#!/bin/sh +set -eu + +# Files +# Put your files in .config/bookmarks/. +PERS_FILE="${PERS_FILE:-$HOME/.config/bookmarks/personal.txt}" +WORK_FILE="${WORK_FILE:-$HOME/.config/bookmarks/work.txt}" + +# Rofi command +ROFI="rofi -dmenu -p 'Bookmarks:'" + +# Browsers +# Choose your browsers accordingly +FIREFOX="$(command -v firefox || true)" +BRAVE="$(command -v brave || command -v brave-browser || true)" +FALLBACK="$(command -v xdg-open || echo firefox)" + +# Ensure files exist +mkdir -p "$(dirname "$PERS_FILE")" +[ -f "$PERS_FILE" ] || cat >"$PERS_FILE" <<'EOF' +# personal +tonybtw :: https://tonybtw.com +https://youtube.com +EOF +[ -f "$WORK_FILE" ] || cat >"$WORK_FILE" <<'EOF' +# work +[docs] NixOS Manual :: https://nixos.org/manual/ +EOF + +emit() { + tag="$1"; file="$2" + [ -f "$file" ] || return 0 + # Output: "[tag] :: " + # We keep the whole line after '::' as the raw RHS, or the entire line if no '::' + grep -vE '^\s*(#|$)' "$file" | while IFS= read -r line; do + case "$line" in + *"::"*) + lhs="${line%%::*}"; rhs="${line#*::}" + lhs="$(printf '%s' "$lhs" | sed 's/[[:space:]]*$//')" + rhs="$(printf '%s' "$rhs" | sed 's/^[[:space:]]*//')" + printf '[%s] %s :: %s\n' "$tag" "$lhs" "$rhs" + ;; + *) + printf '[%s] %s :: %s\n' "$tag" "$line" "$line" + ;; + esac + done +} + +# Build combined list +choice="$({ + emit personal "$PERS_FILE" + emit work "$WORK_FILE" +} | sort | eval "$ROFI" || true)" + +[ -n "$choice" ] || exit 0 + +# Parse tag and raw URL +tag="${choice%%]*}"; tag="${tag#\[}" +raw="${choice##* :: }" + +# Strip inline comments and trim +raw="$(printf '%s' "$raw" \ + | sed -e 's/[[:space:]]\+#.*$//' -e 's/[[:space:]]\/\/.*$//' \ + -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" + +# Ensure scheme +case "$raw" in + http://*|https://*|file://*|about:*|chrome:*) url="$raw" ;; + *) url="https://$raw" ;; +esac + +# Pick browser by tag +open_with() { + cmd="$1" + if [ -n "$cmd" ]; then + nohup "$cmd" --new-tab "$url" >/dev/null 2>&1 & exit 0 + fi +} + +case "$tag" in + personal) open_with "$FIREFOX" ;; + work) open_with "$BRAVE" ;; +esac + +# Fallback if specific browser not found +nohup $FALLBACK "$url" >/dev/null 2>&1 & + diff --git a/configs-dmenu.sh b/configs-dmenu.sh deleted file mode 100755 index 3a75c38..0000000 --- a/configs-dmenu.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/sh -set -eu - -# Grab only directories inside ~/debian-dotfiles -configs="$(ls -1d ~/debian-dotfiles/*/ 2>/dev/null | xargs -n1 basename)" -[ -n "$configs" ] || exit 0 - -chosen="$(printf '%s\n' $configs | dmenu -i -c -vi -bw 3 -W 800 -l 30 -h 40 -F -fn "JetBrainsMono Nerd Font:size=16" -p 'configs:')" -[ -n "$chosen" ] || exit 0 - -dir="$HOME/debian-dotfiles/$chosen" - -# If inside tmux: switch or create -if [ "${TMUX-}" ]; then - if tmux has-session -t "$chosen" 2>/dev/null; then - exec tmux switch-client -t "$chosen" - else - exec tmux new-session -s "$chosen" -c "$dir" "nvim ." - fi -fi - -# If there's an st client running tmux, switch that instead of opening a new one -client_tty="$( - tmux list-clients -F '#{client_tty} #{client_termname}' 2>/dev/null \ - | awk '$2 ~ /^st/ { print $1; exit }' || true -)" - -if [ -n "${client_tty}" ]; then - if tmux has-session -t "$chosen" 2>/dev/null; then - tmux switch-client -c "${client_tty}" -t "$chosen" - else - tmux new-session -s "$chosen" -c "$dir" "nvim ." - fi - exit 0 -fi - -# Otherwise, spawn a new st terminal -if tmux has-session -t "$chosen" 2>/dev/null; then - exec st -e tmux attach -t "$chosen" -else - exec st -e tmux new-session -s "$chosen" -c "$dir" "nvim ." -fi - diff --git a/readme.org b/readme.org new file mode 100644 index 0000000..2485f4a --- /dev/null +++ b/readme.org @@ -0,0 +1,238 @@ +#+TITLE: dmenu/rofi Utilities — README +#+AUTHOR: Tony +#+OPTIONS: toc:2 num:t +#+PROPERTY: header-args :results verbatim :exports code + +This repo contains *three* tiny scripts (each with a =dmenu= and a =rofi= variant): + +1) =bookmarks-.sh=: quick launcher for personal/work bookmarks +2) =repos-.sh=: pick a repo directory and attach/create a =tmux= session in it +3) =tmux-.sh=: “sessionizer” for creating/switching =tmux= sessions + +Throughout, you can specify your terminal as needed. See [[#terminal]]. + +* Table of Contents :TOC_2: +- [[#requirements][Requirements]] +- [[#terminal-selection-st-alacritty-kitty-etc][Terminal selection (st, alacritty, kitty, etc.)]] +- [[#installation--files][Installation & files]] +- [[#1-bookmarks-menush][1) =bookmarks-.sh=]] + - [[#bookmark-file-format][Bookmark file format]] + - [[#configuration][Configuration]] + - [[#usage][Usage]] +- [[#2-repos-menush][2) =repos-.sh=]] + - [[#configuration-1][Configuration]] + - [[#usage-1][Usage]] +- [[#3-tmux-menush-sessionizer][3) =tmux-.sh= (“sessionizer”)]] + - [[#configuration-2][Configuration]] + - [[#usage-2][Usage]] +- [[#dmenu-vs-rofi-flags--examples][dmenu vs rofi (flags & examples)]] +- [[#optional-desktop-entries--keybinds][Optional: desktop entries & keybinds]] + - [[#desktop-entries][Desktop entries]] + - [[#keybinds][Keybinds]] +- [[#troubleshooting][Troubleshooting]] + +* Requirements +- POSIX shell (/bin/sh) +- =tmux= (for =repos-*= and =tmux-*=) +- One of: =dmenu= or =rofi= +- A terminal emulator (e.g., =st=, =alacritty=, =kitty=) +- Optional: =firefox=, =brave= (or =brave-browser=), =xdg-open= for bookmarks + +* Terminal selection (st, alacritty, kitty, etc.) :terminal: +All scripts are written to allow specifying your terminal. If a script hard-codes =st= in the example below, simply: +- Set an environment variable before launching, e.g.: + #+begin_src sh + export TERMINAL="alacritty" + #+end_src +- Or change the =terminal= variable near the top of the script: + #+begin_src sh + terminal="${TERMINAL:-st}" # prefer $TERMINAL, else use st + #+end_src + +Anywhere you see =pkill -x st= or =exec st -e …= you can safely swap =st= for your terminal. If your terminal supports =-e= differently, adjust accordingly (e.g., Kitty: =kitty tmux …=; Alacritty: =alacritty -e tmux …=). + +* Installation & files :installation: +1. Put the scripts somewhere on your =$PATH=, e.g. =~/bin=. +2. Make them executable: + #+begin_src sh + chmod +x ~/bin/bookmarks-dmenu.sh \ + ~/bin/bookmarks-rofi.sh \ + ~/bin/repos-dmenu.sh \ + ~/bin/repos-rofi.sh \ + ~/bin/tmux-dmenu.sh \ + ~/bin/tmux-rofi.sh + #+end_src +3. For bookmarks, the first run will create example files if missing: + - =~/.config/bookmarks/personal.txt= + - =~/.config/bookmarks/work.txt= + +* 1) =bookmarks-.sh= :1-bookmarks:bookmarks: +Quickly search and open bookmarks from *personal* and *work* lists. The script: +- Aggregates lines from personal/work files +- Normalizes “display :: target” lines (or raw URLs) +- Lets you pick from a fuzzy-ish menu +- Chooses a browser based on the tag (=personal= → Firefox, =work= → Brave); falls back to =xdg-open= + +** Bookmark file format :bookmark-format: +Each line is one entry. Blank lines or lines beginning with =#= are ignored. + +- With display: + #+begin_example + Some Site :: https://example.com/page + #+end_example +- Raw URL (display = URL): + #+begin_example + https://news.ycombinator.com + #+end_example + +The script emits entries like: +#+begin_example +[personal] Some Site :: https://example.com/page +[work] Docs :: https://nixos.org/manual/ +#+end_example + +** Configuration :bookmark-config: +Environment variables (override as needed): +#+begin_src sh +PERS_FILE="$HOME/.config/bookmarks/personal.txt" +WORK_FILE="$HOME/.config/bookmarks/work.txt" + +# Browsers (detected automatically if installed) +FIREFOX="$(command -v firefox || true)" +BRAVE="$(command -v brave || command -v brave-browser || true)" +FALLBACK="$(command -v xdg-open || echo firefox)" +#+end_src + +You can change which browser opens which tag by editing the =case "$tag" in …= section. + +** Usage :bookmark-usage: +- dmenu: + #+begin_src sh + bookmarks-dmenu.sh + #+end_src +- rofi: + #+begin_src sh + bookmarks-rofi.sh + #+end_src + +Pick an entry and it opens in the appropriate browser. If a scheme is missing, =https://= is assumed. + +* 2) =repos-.sh= :2-repos:repos: +Presents a list of directories under =~/repos= and opens a dedicated =tmux= session per project. It enforces a “single terminal” workflow by killing any existing instance of your chosen terminal, then starting a clean one attached to the chosen session. + +** Configuration :repos-config: +- Repo root: + #+begin_src sh + REPO_ROOT="$HOME/repos" + #+end_src +- Terminal (see [[#terminal][Terminal selection]]): + #+begin_src sh + terminal="${TERMINAL:-st}" + #+end_src +- Menu prompt & layout are set via the menu’s flags (see [[#dmenu-vs-rofi][dmenu vs rofi]]). + +** Usage :repos-usage: +- dmenu: + #+begin_src sh + repos-dmenu.sh + #+end_src +- rofi: + #+begin_src sh + repos-rofi.sh + #+end_src + +Flow: +1. Pick a directory from =~/repos= +2. Script kills any existing =$terminal= (optional “one terminal at a time” behavior) +3. Launches =tmux new-session -As "" -c ""= inside =$terminal= + +* 3) =tmux-.sh= (“sessionizer”) :3-tmux:tmux: +Lists existing =tmux= sessions, plus a “Create new session…” option. Works both *inside* and *outside* tmux. + +** Configuration :tmux-config: +- Terminal (outside-tmux attach/create path): + #+begin_src sh + terminal="${TERMINAL:-st}" + #+end_src +- Menu binary & options are configurable (see [[#dmenu-vs-rofi][dmenu vs rofi]]). + +** Usage :tmux-usage: +- dmenu: + #+begin_src sh + tmux-dmenu.sh + #+end_src +- rofi: + #+begin_src sh + tmux-rofi.sh + #+end_src + +Behavior: +- Inside =tmux=: + - “Create new …” → creates a *detached* session if it doesn’t exist, then switches client to it + - Selecting an existing session just switches the client +- Outside =tmux=: + - Kills existing =${terminal}= (optional “one terminal” behavior) + - “Create new …” → starts =${terminal} -e tmux new-session -s ""= + - Existing session → =${terminal} -e tmux attach -t ""= + +* dmenu vs rofi (flags & examples) :dmenu-vs-rofi: +Both variants do the same thing; only the menu program/flags differ. + +- dmenu example: + #+begin_src sh + dmenu -i -vi -c -bw 3 -W 900 -l 30 -h 40 -F -fn 'JetBrainsMono Nerd Font:size=16' -p 'Bookmarks:' + #+end_src + +- rofi equivalent (=rofi -dmenu= mode): + #+begin_src sh + rofi -dmenu -i -p 'Bookmarks:' -theme-str 'listview { lines: 30; }' \ + -font 'JetBrainsMono Nerd Font 16' + #+end_src + +Notes: +- =-l 30= (dmenu “lines”) → use a rofi theme or =-theme-str= to set visible rows +- Font names are similar; for rofi, omit the colon syntax +- Centering in rofi is controlled by theme; you can ship a =.rasi= or use =-theme-str= +- If you want near-identical looks, create a tiny =.rasi= theme and point both rofi variants at it + +* Optional: desktop entries & keybinds :desktop-entries: +** Desktop entries +Create =~/.local/share/applications/bookmarks.desktop=: +#+begin_src ini +[Desktop Entry] +Type=Application +Name=Bookmarks (dmenu) +Exec=/home/tony/bin/bookmarks-dmenu.sh +Terminal=false +Categories=Utility; +#+end_src + +Do the same for =repos-*= and =tmux-*=. + +** Keybinds +- dwm (example): + #+begin_src c + { MODKEY, XK_b, spawn, SHCMD("bookmarks-dmenu.sh") }, + { MODKEY, XK_p, spawn, SHCMD("repos-dmenu.sh") }, + { MODKEY|ShiftMask, XK_p, spawn, SHCMD("tmux-dmenu.sh") }, + #+end_src +- qtile (example): + #+begin_src python + Key([mod], "b", lazy.spawn("bookmarks-rofi.sh")), + Key([mod], "p", lazy.spawn("repos-rofi.sh")), + Key([mod, "shift"], "p", lazy.spawn("tmux-rofi.sh")), + #+end_src + +* Troubleshooting :troubleshooting: +- “No =tmux= found”: install =tmux= and ensure it’s on =$PATH= +- “Menu not found”: install either =dmenu= or =rofi= (match the script you’re calling) +- Terminal doesn’t open: + - Verify your =TERMINAL= supports =-e= the way you invoke it + - Replace =pkill -x st= with your terminal name or remove the “one terminal” behavior +- Bookmarks not opening in the desired browser: + - Edit the =case "$tag" in …= block (e.g., use Firefox for both, or flip them) + - Ensure =firefox= / =brave= resolve via =command -v …=; otherwise the script falls back to =xdg-open= +- Rofi layout looks cramped: + - Use a small =.rasi= theme or pass =-theme-str= to adjust list rows, padding, and centering + +That’s it. Each script is intentionally small and easy to modify—tweak fonts, prompts, or behavior to match your setup. Adjust your terminal as needed via =TERMINAL= or the =terminal= variable near the top of each script. diff --git a/repos-dmenu.sh b/repos-dmenu.sh index ebe02a6..f78c693 100755 --- a/repos-dmenu.sh +++ b/repos-dmenu.sh @@ -1,18 +1,20 @@ #!/bin/sh set -eu +# Set your terminal: +terminal="st" + # Pick repo configs="$(ls -1d "$HOME"/repos/*/ 2>/dev/null | xargs -n1 basename)" [ -n "$configs" ] || exit 0 -chosen="$(printf '%s\n' $configs | dmenu -i -c -bw 3 -W 600 -l 30 -h 40 -F -fn 'JetBrainsMono Nerd Font:size=16' -p 'Projects:')" +chosen="$(printf '%s\n' $configs | dmenu -i -vi -c -bw 3 -W 600 -l 30 -h 40 -F -fn 'JetBrainsMono Nerd Font:size=16' -p 'Projects:')" [ -n "$chosen" ] || exit 0 dir="$HOME/repos/$chosen" # Nuke any existing st (since you only use one terminal) -pkill -x st 2>/dev/null || true +pkill -x $terminal 2>/dev/null || true sleep 0.1 # Launch a clean terminal: attach if exists, else create -# exec st -e tmux new-session -As "$chosen" -c "$dir" "nvim ." -exec st -e tmux new-session -As "$chosen" -c "$dir" +exec $terminal -e tmux new-session -As "$chosen" -c "$dir" diff --git a/repos-rofi.sh b/repos-rofi.sh new file mode 100755 index 0000000..628d1d8 --- /dev/null +++ b/repos-rofi.sh @@ -0,0 +1,21 @@ +#!/bin/sh +set -eu + +# Set your terminal: +terminal="st" + +# Pick repo +configs="$(ls -1d "$HOME"/repos/*/ 2>/dev/null | xargs -n1 basename)" +[ -n "$configs" ] || exit 0 +chosen="$(printf '%s\n' $configs | rofi -dmenu -p 'Projects:')" +[ -n "$chosen" ] || exit 0 +dir="$HOME/repos/$chosen" + +# Nuke any existing st (since you only use one terminal) +pkill -x $terminal 2>/dev/null || true +sleep 0.1 + +# Launch a clean terminal: attach if exists, else create +# exec st -e tmux new-session -As "$chosen" -c "$dir" "nvim ." +exec $terminal -e tmux new-session -As "$chosen" -c "$dir" + diff --git a/tmux-dmenu.sh b/tmux-dmenu.sh index 64164e5..8921b88 100755 --- a/tmux-dmenu.sh +++ b/tmux-dmenu.sh @@ -1,41 +1,46 @@ #!/bin/sh set -eu -sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)" -[ -n "${sessions}" ] || exit 0 +TMUX_BIN="$(command -v tmux || { echo 'tmux not found'; exit 1; })" +DMENU_BIN="$(command -v dmenu || { echo 'dmenu not found'; exit 1; })" -dmenu_pick_session() { - # dmenu -i \ - # -c \ - # -vi \ - # -bw 3 \ - # -l 15 \ - # -h 40 \ - # -F \ - # -fn "JetBrainsMono Nerd Font:size=16" \ - # -p 'tmux sessions:' +DMENU_OPTS='-i -vi -c -bw 3 -W 600 -l 30 -h 40 -F -fn JetBrainsMono\ Nerd\ Font:size=16 -p Sessions:' - dmenu -i -c -vi -bw 3 -W 800 -l 30 -h 40 -F -fn "JetBrainsMono Nerd Font:size=16" -p "tmux sessions:" +# List existing sessions (just names) +sessions="$("$TMUX_BIN" list-sessions -F '#{session_name}' 2>/dev/null || true)" - # dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:' -} -chosen="$(printf '%s\n' "$sessions" | dmenu_pick_session)" +# Add a "create new" entry at the top +menu="Create new session…\n$sessions" -[ -n "$chosen" ] || exit 0 +choice="$(printf '%b' "$menu" | eval "$DMENU_BIN $DMENU_OPTS" || true)" +[ -n "$choice" ] || exit 0 -if [ "${TMUX-}" ]; then - exec tmux switch-client -t "${chosen}" +if [ "$choice" = "Create new session…" ]; then + new_name="$(printf '' | eval "$DMENU_BIN -c -bw 3 -W 600 -F -fn JetBrainsMono\\ Nerd\\ Font:size=16 -p 'New session name:'" || true)" + [ -n "$new_name" ] || exit 0 + session="$new_name" + create=1 +else + session="$choice" + create=0 fi -client_tty="$( - tmux list-clients -F '#{client_tty} #{client_termname}' 2>/dev/null \ - | awk '$2 ~ /^st/ { print $1; exit }' || true -)" - -if [ -n "${client_tty}" ]; then - # switch that client to the chosen session - tmux switch-client -c "${client_tty}" -t "${chosen}" - exit 0 +# Inside tmux: create (detached) if needed, then switch (NO exec on create) +if [ -n "${TMUX-}" ]; then + if [ "$create" -eq 1 ] && ! "$TMUX_BIN" has-session -t "$session" 2>/dev/null; then + "$TMUX_BIN" new-session -ds "$session" + fi + exec "$TMUX_BIN" switch-client -t "$session" fi -exec st -e tmux attach -t "${chosen}" +# Outside tmux: one-terminal behavior +pkill -x st 2>/dev/null || true +sleep 0.1 + +if [ "$create" -eq 1 ]; then + # create and attach + exec st -e "$TMUX_BIN" new-session -s "$session" +else + # attach existing + exec st -e "$TMUX_BIN" attach -t "$session" +fi