Conduct a bit of refactoring for ledger to Org table converter

This commit is contained in:
Daniel Borchmann 2024-07-01 20:52:40 +02:00
parent e7c08b088d
commit 6f4baa885e
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625

View File

@ -1085,40 +1085,47 @@ list is converted in such a way that Org can display it as table,
where the rows are all accounts contained in the result of where the rows are all accounts contained in the result of
COMMAND, and where the columns are the individual transactions, COMMAND, and where the columns are the individual transactions,
headlined with their date." headlined with their date."
;; XXX: this implementation is certainly not ideal ;; XXX: this implementation is certainly not ideal
(let* ((data (->> command (let* (transactions dates accounts result)
shell-command-to-string
read-from-string ; XXX: this needs some error handling ;; Call command and catch result
car (setq transactions (condition-case err
;; Format time and remove unnecessary data (car (read-from-string (shell-command-to-string command)))
(-map #'(lambda (row) ((error debug) (error "Failed to run ledger command: %s" err))))
;; Format time and remove unnecessary transactions
(setq transactions (-map #'(lambda (row)
(list (format-time-string "%F" (nth 2 row)) (list (format-time-string "%F" (nth 2 row))
(-map #'(lambda (entry) (-map #'(lambda (entry)
(list (nth 1 entry) (list (nth 1 entry)
(nth 2 entry))) (nth 2 entry)))
(-drop 5 row))))))) (-drop 5 row))))
(months (-map #'-first-item data)) transactions))
funds
result)
(dolist (row data) (setq dates (-map #'-first-item transactions))
(let ((month (-first-item row)))
;; Transfer list output into alist for later direct access; `accounts' will map accounts to alists
;; mapping dates to values
(dolist (row transactions)
(let ((date (-first-item row)))
(dolist (entry (-second-item row)) (dolist (entry (-second-item row))
(let ((fund (-first-item entry)) (let ((fund (-first-item entry))
(value (-second-item entry))) (value (-second-item entry)))
(setf (alist-get month (alist-get fund funds nil nil #'string=) nil nil #'string=) (setf (alist-get date (alist-get fund accounts nil nil #'string=) nil nil #'string=)
value))))) value)))))
(push (cl-list* "" months) result) ;; Build up final result (in reverse order)
(push (cl-list* " " (-repeat (length data) "<r>")) result) (push (cl-list* "" dates) result)
(push (cl-list* " " (-repeat (length transactions) "<r>")) result)
(push 'hline result) (push 'hline result)
(dolist (fund (-sort #'string< (-map #'-first-item funds))) (dolist (fund (-sort #'string< (-map #'-first-item accounts)))
(push (cl-list* fund (push (cl-list* fund
(-map #'(lambda (month) (-map #'(lambda (date)
(alist-get month (alist-get fund funds nil nil #'string=) (alist-get date (alist-get fund accounts nil nil #'string=)
"0.00€" nil #'string=)) "0.00€" nil #'string=))
months)) dates))
result)) result))
(reverse result))) (reverse result)))