#!/usr/bin/env bash set -euo pipefail FILE="${FILE:-$HOME/docs/priv/finance/hledger_main/accounts.journal}" DMENU="${DMENU:-dmenu -i -l 20 -p 'Account:'}" CLIP="${CLIP:-xclip -selection clipboard}" if [[ ! -r "$FILE" ]]; then printf 'ERROR: cannot read %s\n' "$FILE" >&2 exit 1 fi # Extract account names: # - only lines starting with: account ... # - strip the leading "account " part # - sort uniquely choices="$( awk ' /^[[:space:]]*account[[:space:]]+/ { sub(/^[[:space:]]*account[[:space:]]+/, "", $0) print } ' "$FILE" | sort -u )" [[ -z "$choices" ]] && { printf 'ERROR: no account entries found in %s\n' "$FILE" >&2 exit 1 } picked="$(printf '%s\n' "$choices" | eval "$DMENU" || true)" [[ -z "$picked" ]] && exit 0 printf '%s' "$picked" | eval "$CLIP" printf 'Copied: %s\n' "$picked" >&2