render.lisp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 account amount unit-price total-price comment) posting
  14. (let* ((amount-text (render amount))
  15. (status-text (format nil "~@[~A ~]" status))
  16. (text (format nil " ~A~vA~@[ ~A~]~@[ @ ~A~]~@[ @@ ~A~]"
  17. status-text
  18. (if amount-text (- *posting-length*
  19. (position #\Space amount-text)
  20. (length status-text)) 0)
  21. account
  22. amount-text
  23. (when unit-price (render unit-price))
  24. (when total-price (render total-price))))
  25. (comments (split comment))
  26. (first-comment (first comments))
  27. (rest-comments (rest comments)))
  28. (format stream "~A~@[ ; ~A~]~{~%~vA ; ~A~}" text first-comment
  29. (loop for comment in rest-comments append (list (length text) "" comment))))))
  30. (defmethod render ((entry entry) &optional stream)
  31. (with-slots (date secondary-date status code description comment postings) entry
  32. (let* ((text (format nil "~A~@[=~A~]~@[ ~A~]~@[ (~A)~]~@[ ~A~]"
  33. (render-date date) (render-date secondary-date)
  34. status code description))
  35. (comments (split comment))
  36. (first-comment (first comments))
  37. (rest-comments (rest comments)))
  38. (format stream "~A~@[ ; ~A~]~{~%~vA ; ~A~}~{~%~A~}" text first-comment
  39. (loop for comment in rest-comments append (list (length text) "" comment))
  40. (loop for posting in postings collect (render posting))))))