openbsd-dotfiles

Base configurations for my Openbsd desktop
git clone https://git.kausban.com/openbsd-dotfiles/raw/.git
Log | Files | Refs

commit b1c8075db826cb4ffb9feb0ea6e69ff9829d2f83
parent 54db43891fa9e18af0d6d877523d15ba272b32b7
Author: KB <mail@kausban.com>
Date:   Thu, 14 Dec 2023 13:21:43 +0100

fixed dmenu-recent

Diffstat:
M.bin/dmenu-recent | 90++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 89 insertions(+), 1 deletion(-)

diff --git a/.bin/dmenu-recent b/.bin/dmenu-recent @@ -1 +1,89 @@ -dmenu_run +#!/bin/sh +set -eu + +DMENU_CMD="dmenu -i -p > -fn monospace:size=12 -nb #222222 -nf #bbbbbb -sb #005577 -sf #eeeeee" +TERMINAL="st -e" +MAX_RECENT=199 # Number of recent commands to track + +CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/dmenu-recent" +RECENT_CACHE="$CACHE_DIR/recent" +REST_CACHE="$CACHE_DIR/all" +KNOWN_TYPES=" background terminal terminal_hold " + +CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/dmenu-recent" +mkdir -p "$CACHE_DIR" +mkdir -p "$CONFIG_DIR" +touch "$RECENT_CACHE" + +IFS=: +if stest -dqr -n "$REST_CACHE" $PATH 2>/dev/null; then + stest -flx $PATH | sort -u | grep -vf "$RECENT_CACHE" > "$REST_CACHE" +fi + +IFS=' ' +CMD=$(cat "$RECENT_CACHE" "$REST_CACHE" | $DMENU_CMD) || exit + +# Sanitize the input +CMD_NAME=$(printf "%s" "$CMD" | cut -f1 -d' ') # Extract the command name +if ! command -v "$CMD_NAME" >/dev/null 2>&1; then + echo "Invalid command: $CMD_NAME" >&2 + exit 1 +fi + +# Ensure input doesn't contain unwanted symbols +if printf "%s" "$CMD" | grep -q '[!$`(){};&|\*?<>]'; then + echo "Invalid characters in command: $CMD" >&2 + exit 1 +fi + +if ! grep -qx "$CMD" "$RECENT_CACHE" >/dev/null 2>&1; then + grep -vx "$CMD" "$REST_CACHE" > "$REST_CACHE.$$" + mv "$REST_CACHE.$$" "$REST_CACHE" +fi + +echo "$CMD" > "$RECENT_CACHE.$$" +grep -vx "$CMD" "$RECENT_CACHE" | head -n "$MAX_RECENT" >> "$RECENT_CACHE.$$" +mv "$RECENT_CACHE.$$" "$RECENT_CACHE" + +WORD0=${CMD%% *} + +MATCH="^$WORD0$" + +get_type() { + TYPE="" + while : ; do + echo "$KNOWN_TYPES" | xargs -n1 | $DMENU_CMD -p 'Type:' && + read -r TYPE_LINE || break + TYPE=$(echo "$TYPE_LINE") + case $KNOWN_TYPES in + *" $TYPE "*) + echo "$WORD0" >> "$CONFIG_DIR/$TYPE" + return + ;; + esac + done +} + +TYPE=$(grep -lx "$MATCH" -R "$CONFIG_DIR" 2>/dev/null || true) + +if [ -z "$TYPE" ]; then + TYPE=$(get_type) +else + TYPE=$(basename "$TYPE") + if ! echo "$KNOWN_TYPES" | grep -q " $TYPE "; then + rm -f "$CONFIG_DIR/$TYPE" + TYPE=$(get_type) + fi +fi + +case "$TYPE" in + background) + exec $CMD + ;; + terminal) + exec $TERMINAL "$CMD" + ;; + terminal_hold) + exec $TERMINAL sh -c "$CMD && echo 'Press Enter to kill me...' && read _" + ;; +esac