mirror of
https://github.com/SqrtMinusOne/biome.git
synced 2025-12-11 14:53:02 +03:00
biome-query: format current state
This commit is contained in:
parent
0feb5ae6cf
commit
14aa641d94
1 changed files with 101 additions and 14 deletions
115
biome-query.el
115
biome-query.el
|
|
@ -41,6 +41,18 @@
|
||||||
:type 'integer
|
:type 'integer
|
||||||
:group 'biome)
|
:group 'biome)
|
||||||
|
|
||||||
|
(defcustom biome-query-date-format "%A, %x"
|
||||||
|
"Format string for date entries.
|
||||||
|
|
||||||
|
By default \"WEEKDAY, DATE\", where DATE is what Emacs thinks is an
|
||||||
|
appropriate way to format days in your language.
|
||||||
|
If the value is a function, the function will be evaluated and the return
|
||||||
|
value will be inserted."
|
||||||
|
:type '(choice
|
||||||
|
(string :tag "String")
|
||||||
|
(function :tag "Function"))
|
||||||
|
:group 'biome)
|
||||||
|
|
||||||
(defcustom biome-query-coords '(("Helsinki, Finland" 60.16952 24.93545)
|
(defcustom biome-query-coords '(("Helsinki, Finland" 60.16952 24.93545)
|
||||||
("Berlin, Germany" 52.52437 13.41053))
|
("Berlin, Germany" 52.52437 13.41053))
|
||||||
"List of locations with their coordinates.
|
"List of locations with their coordinates.
|
||||||
|
|
@ -84,6 +96,9 @@ case, the value is a list of variable names available in the group.")
|
||||||
(defvar biome-query--layout-cache (make-hash-table :test 'equal)
|
(defvar biome-query--layout-cache (make-hash-table :test 'equal)
|
||||||
"Cache for dynamic transient layout.")
|
"Cache for dynamic transient layout.")
|
||||||
|
|
||||||
|
(defvar biome-query--var-names-cache nil
|
||||||
|
"Cache for variable names.")
|
||||||
|
|
||||||
;; TODO delete this
|
;; TODO delete this
|
||||||
(setq biome-query--layout-cache (make-hash-table :test 'equal))
|
(setq biome-query--layout-cache (make-hash-table :test 'equal))
|
||||||
|
|
||||||
|
|
@ -96,9 +111,85 @@ case, the value is a list of variable names available in the group.")
|
||||||
"A dummy method for `biome-query--transient-report'."
|
"A dummy method for `biome-query--transient-report'."
|
||||||
nil)
|
nil)
|
||||||
|
|
||||||
|
(defun biome-query--update-names-cache (sections cache)
|
||||||
|
(cl-loop for section in sections
|
||||||
|
do (when-let (fields (alist-get :fields section))
|
||||||
|
(cl-loop for (api-key . params) in fields
|
||||||
|
do (puthash api-key (alist-get :name params) cache)))
|
||||||
|
do (when-let (children (or (alist-get :sections section)
|
||||||
|
(alist-get :children section)))
|
||||||
|
(biome-query--update-names-cache children cache))))
|
||||||
|
|
||||||
|
(defun biome-query--get-var-names-cache ()
|
||||||
|
(let* ((name (alist-get :name biome-query-current))
|
||||||
|
(cache (alist-get name biome-query--var-names-cache
|
||||||
|
nil nil #'equal)))
|
||||||
|
(if (and (hash-table-p cache) (> (hash-table-count cache) 0)) cache
|
||||||
|
(setq cache (make-hash-table :test #'equal))
|
||||||
|
(biome-query--update-names-cache
|
||||||
|
(alist-get :sections biome-query--current-section)
|
||||||
|
cache)
|
||||||
|
(setf (alist-get name biome-query--var-names-cache
|
||||||
|
nil nil #'equal)
|
||||||
|
cache))))
|
||||||
|
|
||||||
(cl-defmethod transient-format ((_ biome-query--transient-report))
|
(cl-defmethod transient-format ((_ biome-query--transient-report))
|
||||||
"Format the current report."
|
"Format the current report."
|
||||||
(prin1-to-string biome-query-current))
|
(let ((group (alist-get :group biome-query-current))
|
||||||
|
(var-names (biome-query--get-var-names-cache))
|
||||||
|
lat lon group-vars vars)
|
||||||
|
(dolist (item (alist-get :params biome-query-current))
|
||||||
|
(cond
|
||||||
|
((stringp item)
|
||||||
|
(push (gethash item var-names) vars))
|
||||||
|
((equal (car item) group)
|
||||||
|
(setq group-vars (mapcar (lambda (x) (gethash x var-names))
|
||||||
|
(cdr item))))
|
||||||
|
((equal (car item) "latitude")
|
||||||
|
(setq lat (cdr item)))
|
||||||
|
((equal (car item) "longitude")
|
||||||
|
(setq lon (cdr item)))
|
||||||
|
((member (car item) '("end_date" "start_date"))
|
||||||
|
(push
|
||||||
|
(format "%s: %s" (propertize (gethash (car item) var-names)
|
||||||
|
'face 'font-lock-variable-name-face)
|
||||||
|
(propertize
|
||||||
|
(format-time-string biome-query-date-format (cdr item))
|
||||||
|
'face 'transient-value-face))
|
||||||
|
vars))
|
||||||
|
(t (push
|
||||||
|
(format "%s: %s"
|
||||||
|
(propertize
|
||||||
|
(gethash (car item) var-names)
|
||||||
|
'face 'font-lock-variable-name-face)
|
||||||
|
(propertize
|
||||||
|
(prin1-to-string (cdr item))
|
||||||
|
'face 'transient-value-face))
|
||||||
|
vars))))
|
||||||
|
(setq group-vars (nreverse group-vars)
|
||||||
|
vars (nreverse vars))
|
||||||
|
(concat "Location: "
|
||||||
|
(if lat (propertize (number-to-string lat) 'face 'transient-value)
|
||||||
|
(propertize "unset" 'face 'error))
|
||||||
|
" "
|
||||||
|
(if lon (propertize (number-to-string lon) 'face 'transient-value)
|
||||||
|
(propertize "unset" 'face 'error))
|
||||||
|
(when-let ((_ (and lat lon))
|
||||||
|
(loc (seq-find
|
||||||
|
(lambda (x) (equal (cdr x) (list lat lon)))
|
||||||
|
biome-query-coords)))
|
||||||
|
(format " (%s)" (propertize (car loc) 'face 'transient-value)")"))
|
||||||
|
"\n"
|
||||||
|
(when group
|
||||||
|
(format "Group: %s\n"
|
||||||
|
(propertize group 'face 'transient-value)))
|
||||||
|
(when group-vars
|
||||||
|
(format "Group variables: %s\n"
|
||||||
|
(mapconcat (lambda (x) (propertize x 'face 'font-lock-variable-name-face))
|
||||||
|
group-vars "; ")))
|
||||||
|
(when vars
|
||||||
|
(format "Variables: %s\n"
|
||||||
|
(mapconcat #'identity vars "; "))))))
|
||||||
|
|
||||||
(transient-define-infix biome-query--transient-report-infix ()
|
(transient-define-infix biome-query--transient-report-infix ()
|
||||||
:class 'biome-query--transient-report
|
:class 'biome-query--transient-report
|
||||||
|
|
@ -188,6 +279,7 @@ OBJ is an instance of `biome-query--transient-switch-variable'."
|
||||||
%d is formatted using `transient-format-description'.
|
%d is formatted using `transient-format-description'.
|
||||||
%v is formatted using `transient-format-value'."
|
%v is formatted using `transient-format-value'."
|
||||||
(concat
|
(concat
|
||||||
|
" "
|
||||||
(string-pad (transient-format-key obj) 6)
|
(string-pad (transient-format-key obj) 6)
|
||||||
(transient-format-description obj)
|
(transient-format-description obj)
|
||||||
(when (oref obj value)
|
(when (oref obj value)
|
||||||
|
|
@ -230,19 +322,14 @@ OBJ is an instance of `biome-query--transient-select-variable'."
|
||||||
'face 'transient-value)
|
'face 'transient-value)
|
||||||
(propertize "unset" 'face 'transient-inactive-value))))
|
(propertize "unset" 'face 'transient-inactive-value))))
|
||||||
|
|
||||||
(defclass biome-query--transient-date-variable (biome-query--transient-variable)
|
(defclass biome-query--transient-date-variable (biome-query--transient-variable) ()
|
||||||
((reader :initform #'biome-query--transient-date-reader))
|
|
||||||
"A transient class to display a date variable.")
|
"A transient class to display a date variable.")
|
||||||
|
|
||||||
(defun biome-query--transient-date-reader (prompt _initial-input _history)
|
(cl-defmethod transient-infix-read ((obj biome-query--transient-date-variable))
|
||||||
"Read the date with `org-read-date'.
|
(unless (oref obj value)
|
||||||
|
(time-convert
|
||||||
PROMPT is a string to prompt with.
|
(org-read-date nil t nil (concat (oref obj description) " "))
|
||||||
|
'integer)))
|
||||||
Returns a UNIX timestamp."
|
|
||||||
(time-convert
|
|
||||||
(org-read-date nil t nil prompt)
|
|
||||||
'integer))
|
|
||||||
|
|
||||||
(cl-defmethod transient-format-value ((obj biome-query--transient-date-variable))
|
(cl-defmethod transient-format-value ((obj biome-query--transient-date-variable))
|
||||||
"Format the value of OBJ.
|
"Format the value of OBJ.
|
||||||
|
|
@ -253,7 +340,7 @@ OBJ is an instance of `biome-query--transient-date-variable'."
|
||||||
(propertize
|
(propertize
|
||||||
(format-time-string
|
(format-time-string
|
||||||
;; TODO fix
|
;; TODO fix
|
||||||
org-journal-date-format
|
biome-query-date-format
|
||||||
(seconds-to-time
|
(seconds-to-time
|
||||||
value))
|
value))
|
||||||
'face 'transient-value)
|
'face 'transient-value)
|
||||||
|
|
@ -307,7 +394,7 @@ OBJ is an instance of `biome-query--transient-date-variable'."
|
||||||
(integer :initarg :integer :initform nil))
|
(integer :initarg :integer :initform nil))
|
||||||
"A transient class to display a number variable.")
|
"A transient class to display a number variable.")
|
||||||
|
|
||||||
(cl-defmethod transient-infix-read ((obj biome-query--transient-variable))
|
(cl-defmethod transient-infix-read ((obj biome-query--transient-number-variable))
|
||||||
"Read the value of OBJ."
|
"Read the value of OBJ."
|
||||||
(let ((prompt
|
(let ((prompt
|
||||||
(concat
|
(concat
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue