aboutsummaryrefslogtreecommitdiff
path: root/hledger-category-select
blob: 7b34ff4176d51f0ef5439e6cb72ae7c752e2e032 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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