-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgt.el
163 lines (141 loc) · 5.99 KB
/
bgt.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; bgt.el --- Record and view blood glucose level -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Md Arif Shaikh
;; Author: Md Arif Shaikh <arifshaikh.astro@gmail.com>
;; Homepage: https://github.com/md-arif-shaikh/bgt
;; Version: 0.0.1
;; Package-Requires: ((emacs "26.1") (dash "2.19.1"))
;; Keywords: convenience, glucose monitoring
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This is simple package to help you keep your glucose level
;; with Emacs and export data to csv or even plot the data to see the trend.
;; Plotting would require python with pandas and matplotlib installed on your
;; machine.
;;; Code:
(require 'org)
(require 'dash)
(defcustom bgt-file-name "~/bgt.org"
"Org file name to save the blood glucose data."
:type 'string
:group 'bgt)
(defcustom bgt-csv-file-name "~/bgt.csv"
"CSV file name for exporting the table data from org file."
:type 'string
:group 'bgt)
(defcustom bgt-python-file "~/bgt.py"
"Path to the python script for plotting."
:type 'string
:group 'bgt)
(defcustom bgt-python-path "~/miniconda/bin/python"
"Path to python."
:type 'string
:group 'bgt)
(defcustom bgt-plot-file "~/bgt.pdf"
"File name to save the plot."
:type 'string
:group 'bgt)
(defun bgt-create-initial-file ()
"Create the data file with initial inputs when the file does not exist."
(unless (string-equal "org" (file-name-extension bgt-file-name))
(user-error "`bgt-file-name` should be an `org` file!"))
(with-current-buffer (generate-new-buffer bgt-file-name)
(insert "#+TITLE: Blood Glucose Table\n")
(insert (format "#+AUTHOR: %s\n\n" user-full-name))
(insert "* BGT\n")
(insert ":PROPERTIES:\n")
(insert (format ":TABLE_EXPORT_FILE: %s\n" bgt-csv-file-name))
(insert ":TABLE_EXPORT_FORMAT: orgtbl-to-csv\n")
(insert ":END:\n\n")
(insert "#+TBLNAME: bgt\n")
(insert "|--|--|--|--|--|--|\n")
(insert "|Date |BG | Category | Test | Lab| Weight|\n")
(insert "|--|--|--|--|--|--|\n")
(write-file bgt-file-name)
(kill-buffer (-last-item (split-string bgt-file-name "/"))))
(message "Created initial file."))
(defun bgt--goto-table-begin (name)
"Go to begining of table named NAME if point is not in any table."
(unless (org-at-table-p)
(let ((org-babel-results-keyword "NAME"))
(org-babel-goto-named-result name)
(forward-line 2)
(goto-char (org-table-begin)))))
(defun bgt-get-lab-names (data-file)
"Get the lab names for completion from the data in DATA-FILE.
DATA-FILE is the `org` file where the data of glucose levels are stored."
(if (not (file-exists-p data-file))
'()
(let ((buff-name (concat (temporary-file-directory) "bgt-labs.org"))
(lab-names '()))
(with-current-buffer (generate-new-buffer buff-name)
(insert-buffer-substring (find-file-noselect data-file))
(bgt--goto-table-begin "bgt")
(forward-line 2)
(while (org-at-table-p)
(push (string-trim (org-table-get-field 5)) lab-names)
(forward-line))
(kill-buffer buff-name)
(kill-buffer (-last-item (split-string data-file "/")))
(delete-file buff-name)
(delete-dups lab-names)))))
(defun bgt-add-entry ()
"Add entry to blood glucose table."
(interactive)
(let* ((date (org-read-date 'with-time nil nil "Date and time: "))
(bg-level (read-number "Glucose level: "))
(bg-category (completing-read "Test category: " '("Fasting" "Random" "Post-prandial" "HbA1c" "Mean-Blood-Glucose")))
(bg-sample (completing-read "Blood sample: " '("Plasma" "Capillary")))
(bg-lab (completing-read "Lab name: " (bgt-get-lab-names bgt-file-name)))
(bg-weight (completing-read "Weight: " (number-sequence 50 90))))
(unless (file-exists-p bgt-file-name)
(bgt-create-initial-file))
(with-temp-buffer
(insert (format "|%s |%.1f |%s |%s |%s |%.1f|\n" date bg-level bg-category bg-sample bg-lab (string-to-number bg-weight)))
(append-to-file (point-min) (point-max) bgt-file-name))
(when (string-equal (completing-read "Add another entry? " '("no" "yes")) "yes")
(bgt-add-entry))
(with-current-buffer (find-file-noselect bgt-file-name)
(goto-char (point-max))
(forward-line -1)
(org-table-align)
(write-file bgt-file-name))))
(defun bgt-view-entry ()
"View the glucose table."
(interactive)
(find-file-other-window bgt-file-name))
(defun bgt-export-to-csv ()
"Export bgt data to a csv file."
(interactive)
(with-current-buffer (find-file-noselect bgt-file-name)
(goto-char (point-max))
(forward-line -2)
(org-table-export)))
(defun bgt-plot (start-date end-date)
"Plot BG data from START-DATE to END-DATE."
(interactive
(list (org-read-date t nil nil "Start Date: ")
(org-read-date t nil nil "End Date: ")))
(bgt-export-to-csv)
(unless bgt-python-file
(user-error "`bgt-python-file` can not be nil. 'setq bgt-python-file'!"))
(unless bgt-plot-file
(setq bgt-plot-file (concat (temporary-file-directory) "bgt.pdf")))
(when (file-exists-p bgt-plot-file)
(delete-file bgt-plot-file))
(if (file-exists-p bgt-python-file)
(shell-command (format "%s %s --data_file %s --plot_file %s --start_date %s --end_date %s"
(or bgt-python-path "python") bgt-python-file bgt-csv-file-name bgt-plot-file start-date end-date))
(message "python file not found. Set the python file path with 'setq bgt-python-file'."))
(when (file-exists-p bgt-plot-file)
(find-file-other-window bgt-plot-file)))
(provide 'bgt)
;;; bgt.el ends here