render.lisp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. (in-package #:pta-ledger)
  2. (defvar *posting-length* 46 "Length of posting without amount")
  3. (defun render-date (universal-time)
  4. (when universal-time
  5. (format nil "~{~4,'0D/~2,'0D/~2,'0D~}" (get-date universal-time))))
  6. (defgeneric render (object &optional stream)
  7. (:documentation "Render OBJECT to STREAM in text format."))
  8. (defmethod render ((null null) &optional stream)
  9. (declare (ignore stream)))
  10. (defmethod render ((amount amount) &optional stream)
  11. (format stream "~$ ~A" (amount-quantity amount) (amount-commodity amount)))
  12. (defmethod render ((posting posting) &optional stream)
  13. (with-slots (status virtual account amount unit-price total-price comment) posting
  14. (let* ((amount-text (render amount))
  15. (status-text (format nil "~@[~A ~]" status))
  16. (account (if virtual
  17. (format nil "~A~A~A" virtual account
  18. (ecase virtual
  19. (#\( #\))
  20. (#\[ #\])))
  21. account))
  22. (text (format nil " ~A~vA~@[ ~A~]~@[ @ ~A~]~@[ @@ ~A~]"
  23. status-text
  24. (if amount-text (- *posting-length*
  25. (position #\Space amount-text)
  26. (length status-text)) 0)
  27. account
  28. amount-text
  29. (when unit-price (render unit-price))
  30. (when total-price (render total-price))))
  31. (comments (split comment))
  32. (first-comment (first comments))
  33. (rest-comments (rest comments)))
  34. (format stream "~A~@[ ; ~A~]~{~%~vA ; ~A~}" text first-comment
  35. (loop for comment in rest-comments append (list (length text) "" comment))))))
  36. (defmethod render ((entry entry) &optional stream)
  37. (with-slots (date secondary-date status code description comment postings) entry
  38. (let* ((text (format nil "~A~@[=~A~]~@[ ~A~]~@[ (~A)~]~@[ ~A~]"
  39. (render-date date) (render-date secondary-date)
  40. status code description))
  41. (comments (split comment))
  42. (first-comment (first comments))
  43. (rest-comments (rest comments)))
  44. (format stream "~A~@[ ; ~A~]~{~%~vA ; ~A~}~{~%~A~}" text first-comment
  45. (loop for comment in rest-comments append (list (length text) "" comment))
  46. (loop for posting in postings collect (render posting))))))