Browse Source

Add support for custom labels

* elbank-common.el (elbank-transaction-elt): Return the custom label of the
  transaction when set.
* elbank-report.el (elbank-report-set-custom-label): New command.
* elbank-transaction.el (elbank-transaction--refresh): Display all transaction
  keys.
* test/elbank-common-test.el: New test for custom labels.
master
Nicolas Petton 3 years ago
parent
commit
3fe6802eb7
No known key found for this signature in database GPG Key ID: E8BCD7866AFCF978
4 changed files with 41 additions and 8 deletions
  1. +8
    -2
      elbank-common.el
  2. +19
    -0
      elbank-report.el
  3. +10
    -6
      elbank-transaction.el
  4. +4
    -0
      test/elbank-common-test.el

+ 8
- 2
elbank-common.el View File

@ -130,8 +130,14 @@ If the result is nil, return DEFAULT."
"Return the label of TRANSACTION.
The label is defined as the value at symbol `label' if present,
the value at symbol `raw' if not. If both are nil, return DEFAULT."
(or (map-elt transaction 'label nil)
the value at symbol `raw' if not.
Transactions can optionally have a `custom-label', which takes
priority when set.
If all are nil, return DEFAULT."
(or (map-elt transaction 'custom-label nil)
(map-elt transaction 'label nil)
(map-elt transaction 'raw default)))
(cl-defmethod elbank-transaction-elt (transaction (key (eql account)) &optional default)


+ 19
- 0
elbank-report.el View File

@ -111,6 +111,7 @@ Available columns:
(define-key map (kbd "S") #'elbank-report-sort-by)
(define-key map (kbd "s") #'elbank-report-sort-reverse)
(define-key map (kbd "c") #'elbank-report-set-category)
(define-key map (kbd "l") #'elbank-report-set-custom-label)
(define-key map (kbd "+") #'elbank-report-split-transaction)
(define-key map (kbd "-") #'elbank-report-unsplit-transaction)
map)
@ -352,6 +353,24 @@ point."
(elbank-write-data elbank-data)
(elbank-report-refresh))
(defun elbank-report-set-custom-label (label &optional transaction)
"Set a custom LABEL for TRANSACTION.
When called interactively, prompt for the label.
If LABEL is an empty string, set nil as the custom label.
If TRANSACTION is nil, set the custom label of the transaction at
point."
(interactive (list (read-from-minibuffer "Custom label: ")))
(unless transaction
(setq transaction (elbank-report--transaction-at-point)))
(setf (elbank-transaction-elt transaction 'custom-label)
(if (string-empty-p label)
nil
label))
(elbank-write-data elbank-data)
(elbank-report-refresh))
(defun elbank-report--split-amount (amount)
"Split amount into a list of (CATEGORY-NAME . AMOUNT)."
(let ((amount-left amount)


+ 10
- 6
elbank-transaction.el View File

@ -57,13 +57,17 @@
(defun elbank-transaction--refresh (transaction)
"Populate the current buffer with the details of TRANSACTION."
(let ((inhibit-read-only t)
(width (1+ (seq-reduce (lambda (acc elt)
(max acc (seq-length (symbol-name elt))))
elbank-report-available-columns
0))))
(let* ((inhibit-read-only t)
(keys (map-keys transaction))
(width (1+ (seq-reduce (lambda (acc elt)
(max acc (seq-length (symbol-name elt))))
keys
0))))
(erase-buffer)
(seq-doseq (key elbank-report-available-columns)
(seq-doseq (key (seq-sort (lambda (k1 k2)
(string-lessp (symbol-name k1)
(symbol-name k2)))
keys))
(let ((label (capitalize (format "%s:" key))))
(dotimes (_ (- width (seq-length label)))
(insert " "))


+ 4
- 0
test/elbank-common-test.el View File

@ -71,6 +71,10 @@
(let ((tr '((label . "foo") (raw . "bar"))))
(expect (elbank-transaction-elt tr 'label) :to-equal "foo")))
(it "prefers custom labels over labels"
(let ((tr '((label . "foo") (custom-label . "bar") (raw . "baz"))))
(expect (elbank-transaction-elt tr 'label) :to-equal "bar")))
(it "returns raw if present and label is not"
(let ((tr '((raw . "foo"))))
(expect (elbank-transaction-elt tr 'label) :to-equal "foo")))


Loading…
Cancel
Save