summaryrefslogtreecommitdiff
path: root/hledger-fadd
blob: 36e762a92ad0e9743ae6115e7c918a36ec4e1e1e (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash

function fz {
    readarray -t lines < <(fzf \
        --height 50% --reverse --expect alt-enter --print-query "$@" \
        --bind "pgup:preview-page-up" --bind "pgdn:preview-page-down"
    )
    query=${lines[0]}
    enter=${lines[1]}
    match=${lines[2]}
    if [[ "$enter" == "alt-enter" ]]
    then
        echo "$query"
    else
        echo "$match"
    fi
}

accounts=$(cache hledger accounts)
payees=$(cache hledger payees)

date=$(for n in $(seq 0 31); do date -d"$n days ago" "+%Y/%m/%d (%a)"; done \
    | fzf --height 50% --reverse --delimiter '/' \
      --preview="gcal -H yes %{1}{2}{3}" \
      --preview-window 'noborder,top,8' \
      --header-first --header "Today is $(date '+%Y/%m/%d (%a)')" \
    | cut -d ' ' -f1
)
echo "date: $date"

payee=$(fz --prompt="payee: " --preview='hledger print payee:{..}' <<< $payees)
echo "payee: $payee"

# store past txs in a file, in the background, so I don't have to wait for the
# command to re-run each time
past_txs=$(mktemp)
hledger print payee:"$payee" > $past_txs &

read -rep "to amount: " to_amount
to_account=$(fz --prompt="to account: " --preview="cat $past_txs" <<< $accounts)
echo "to account: $to_account"


from_account=$(fz --prompt="from account: " --preview="cat $past_txs" <<< $accounts)
echo "from account: $from_account"
read -rep "from amount: " -i "-$to_amount" from_amount


read -r -d '' TX <<EOF
$date * $payee
    $to_account        $to_amount USD
    $from_account      $from_amount USD
EOF

tmp=$(mktemp)
printf "\n%s\n" "$TX" >> "$tmp"
cat "$tmp"

read -rep "all good? Y/e/n " ok
case $ok in
    n)
        exit 1
        ;;
    e)
        vim "$tmp" && cat "$tmp" >> ~/org/fund/ledger.journal
        ;;
    *)
        cat "$tmp" >> ~/org/fund/ledger.journal
        ;;
esac