diff options
Diffstat (limited to 'hledger-category-select')
| -rwxr-xr-x | hledger-category-select | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/hledger-category-select b/hledger-category-select new file mode 100755 index 0000000..7b34ff4 --- /dev/null +++ b/hledger-category-select @@ -0,0 +1,35 @@ +#!/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 |
