fin-lisp.lisp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. (ql:quickload "cl-store")
  2. (ql:quickload "cl-ppcre")
  3. (ql:quickload "unix-opts")
  4. ;;; Features
  5. ;;; - Import records from old .txt format
  6. ;;; - Interactive prompt to manage expenses
  7. ;;; - Generic expense handling
  8. ;;; TODO
  9. ;;; - Non interactive CLI Interface
  10. ;;; - Upload/download support like perl version
  11. ;;; - Should support encryption/decryption of records
  12. ;;;; Reimplementation of my bills tracker in Lisp
  13. ;;; All records exist in this data structure
  14. ;;; nil on start and loaded in from file
  15. ;;; *records* represents as hash of months,
  16. ;;; where the key is the month stamp, eg 20210701
  17. ;;; and the value is the monthly expenses hash
  18. (defvar *records* (make-hash-table :test 'equalp))
  19. (defun file-test (filename)
  20. (if (probe-file filename) filename (print "Couldn't find filename")))
  21. ;;; Used by "print-month" arg to validate
  22. ;;; the user provided a valid key
  23. (defun check-month (month-key)
  24. (if (stringp month-key) month-key))
  25. (opts:define-opts
  26. (:name :help
  27. :description "Print help text"
  28. :short #\h
  29. :long "help")
  30. (:name :read
  31. :description "Read serialized records file"
  32. :short #\r
  33. :long "read"
  34. :arg-parser #'file-test)
  35. (:name :print-month
  36. :description "Print records for given month"
  37. :short #\p
  38. :long "print-month"
  39. :arg-parser #'check-month)
  40. (:name :interactive-mode
  41. :description "Run in interactive mode"
  42. :short #\i
  43. :long "interactive"))
  44. ;; See: https://github.com/libre-man/unix-opts/blob/master/example/example.lisp
  45. (defmacro when-option ((options opt) &body body)
  46. `(let ((it (getf ,options ,opt)))
  47. (when it
  48. ,@body)))
  49. (defun reload ()
  50. (load "~/Repos/fin-lisp/fin-lisp.lisp"))
  51. (defun reset-records ()
  52. (setf *records* (make-hash-table :test 'equalp)))
  53. ;; Called like: (add-month '202107)
  54. (defun add-month (month-key)
  55. (setf (gethash month-key *records*) (make-hash-table :test 'equalp))
  56. month-key)
  57. ;;; Taken from practical common lisp
  58. (defun prompt-read (prompt)
  59. (format *query-io* "~a: " prompt)
  60. (force-output *query-io*)
  61. (read-line *query-io*))
  62. (defun prompt-for-expense ()
  63. (list
  64. (prompt-read "Enter expense name")
  65. (parse-integer
  66. (prompt-read "Enter expense value"))))
  67. (defun add-expense-to-month (month)
  68. (if (gethash month *records*)
  69. (let ((innerhash (gethash month *records*))
  70. (exp-l (prompt-for-expense)))
  71. (setf (gethash (first exp-l) innerhash) (second exp-l)))
  72. ;;NIL))
  73. (add-expense-to-month (add-month month))))
  74. ;;; Given key for *records* hash,
  75. ;;; print expenses/values for month
  76. (defun dump-month (month-key)
  77. (format t "~a~C" month-key #\linefeed)
  78. (let ((month-hash)
  79. (exp-keys))
  80. (setf month-hash (gethash month-key *records*))
  81. (setf exp-keys (loop for key being the hash-keys of month-hash collect key))
  82. (dolist (exp-key exp-keys)
  83. (format t "~a : ~a~C" exp-key (gethash exp-key month-hash) #\linefeed))))
  84. ;;; Dump all records.
  85. ;;; This will also be used for data serialization at some point
  86. (defun dump-records ()
  87. (let ((record-key-list (loop for key being the hash-keys of *records* collect key)))
  88. (dolist (month-key record-key-list) (dump-month month-key))))
  89. (defun serialize-records (filename)
  90. (cl-store:store *records* filename))
  91. (defun deserialize-records (filename)
  92. ;(setf *records* (cl-store:restore (pathname filename))))
  93. (setf *records* (cl-store:restore filename)))
  94. ;; Import records from old perl version (plaintext file)
  95. (defun import-records (filename)
  96. (let ((old-file-lines
  97. (with-open-file (stream filename)
  98. (loop for line = (read-line stream nil)
  99. while line
  100. collect line)))
  101. (mre (ppcre:create-scanner "^(.*)[0-9]{4}$"))
  102. (ere (ppcre:create-scanner "^([A-Z].*)\ -\ \\\$([0-9]{1,4}) - PAID"))
  103. (cur-mon)
  104. (cur-exp))
  105. (loop for line in old-file-lines
  106. do (progn
  107. (if (ppcre:scan mre line) (setf cur-mon line))
  108. (if (ppcre:scan ere line)
  109. (progn
  110. (setf cur-exp (ppcre:register-groups-bind (first second) (ere line) :sharedp t (list first second)))
  111. (print cur-exp)
  112. (if (gethash cur-mon *records*)
  113. (let ((innerhash (gethash cur-mon *records*)))
  114. (setf (gethash (first cur-exp) innerhash) (second cur-exp))))
  115. (if (not (gethash cur-mon *records*))
  116. (progn
  117. (add-month cur-mon)
  118. (let ((innerhash (gethash cur-mon *records*)))
  119. (setf (gethash (first cur-exp) innerhash) (second cur-exp)))))))))))
  120. (defmacro generic-handler (form error-string)
  121. `(handler-case ,form
  122. (error (e)
  123. (format t "Invalid input: ~a ~%" ,error-string)
  124. (values 0 e))))
  125. ;; Util screen clearer
  126. (defun cls()
  127. (format t "~A[H~@*~A[J" #\escape))
  128. (defun interactive-mode ()
  129. (format t "~%")
  130. (format t "Available options:~%")
  131. (format t "1. Enter expense~%")
  132. (format t "2. Display month~%")
  133. (format t "3. Write records~%")
  134. (format t "4. Read records~%")
  135. (format t "5. Quit~%")
  136. (format t "6. Import Records~%")
  137. (let
  138. ((answer (prompt-read "Select an option")))
  139. (if (string= answer "1")
  140. (generic-handler
  141. (add-expense-to-month (prompt-read "Enter month"))
  142. "Invalid Input"))
  143. (if (string= answer "2")
  144. (generic-handler
  145. (dump-month (prompt-read "Enter month"))
  146. "Invalid month"))
  147. (if (string= answer "3")
  148. (generic-handler
  149. (serialize-records (prompt-read "Enter filename"))
  150. "Serialization error or invalid filename"))
  151. (if (string= answer "4")
  152. (generic-handler
  153. (deserialize-records (prompt-read "Enter filename"))
  154. "Deserialization error or invalid filename"))
  155. (if (string= answer "5")
  156. (quit))
  157. (if (string= answer "6")
  158. (generic-handler
  159. (import-records (prompt-read "Enter filename"))
  160. "Parsing error or invalid filename")))
  161. (interactive-mode))
  162. (defun display-help ()
  163. (format t "foo ~%")
  164. (opts:describe
  165. :prefix "fin-lisp.lisp - Basic expense tracker in lisp"
  166. :usage-of "fin-lisp.lisp"
  167. :args "[FREE-ARGS]")
  168. (quit))
  169. ;; Entry point
  170. (defun main ()
  171. (if (= 1 (length sb-ext:*posix-argv*)) (interactive-mode))
  172. (let ((matches (opts:get-opts)))
  173. (format t "~a ~%" matches)
  174. (when-option (matches :help)
  175. (display-help))
  176. (when-option (matches :print-month)
  177. (when-option (matches :interactive-mode)
  178. (progn
  179. (interactive-mode)
  180. (quit)))))