Add the defunit macro as a helper to define units

This commit is contained in:
Sameer Rahmani 2023-06-18 18:16:47 +01:00
parent 114a9599ac
commit 74d49748d4
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
2 changed files with 46 additions and 24 deletions

View File

@ -27,6 +27,7 @@
(require 'seq)
(require 'posframe)
(defvar noether/views ()
"A list of views that noether should manage.
@ -93,6 +94,24 @@ the show function."
(defmacro defunit (name docs &rest props)
"Define a unit with the given NAME, DOCS and a set of PROPS.
It will define a function with the given NAME that accepts any
parameter in form of key/values that will override any original
key/value from the original definition."
(declare (doc-string 2))
(let* ((parsed-body (noether/-extract-props props))
;; For now we don't have any use for the body
(_ (car parsed-body))
(orig-props (cdr parsed-body)))
`(defun ,name (&rest f-props)
,docs
(append '(:name ,(intern (format ":%s" name)))
f-props (list ,@orig-props)))))
(defun noether/show (view)
"Draw the given VIEW on the screen."

View File

@ -48,6 +48,31 @@
"Just return the current time."
v)
(defunit line-unit
"Show the line number."
:label "L: "
:len 4
:init (lambda ()
(add-hook 'post-command-hook #'noether/-update-line))
:deinit (lambda ()
(remove-hook 'post-command-hook #'noether/-update-line))
:var 'noether/-line
:fn #'noether/-line-format)
(defunit time-unit
"just the time for your bar."
:label "T: "
:len 8
:init (lambda ()
(setq noether/-timer
(run-with-timer 1 1 #'noether/-set-time)))
:deinit (lambda ()
(when noether/-timer
(cancel-timer noether/-timer)))
:var 'noether/-time
:fn #'noether/-time-format)
(defview example-bar
"Just a test view"
:managed? t
@ -56,30 +81,8 @@
:separator "|"
:units
(list
(list
:label "L: "
:name :line
:len 4
:init (lambda ()
(add-hook 'post-command-hook #'noether/-update-line))
:deinit (lambda ()
(remove-hook 'post-command-hook #'noether/-update-line))
:var 'noether/-line
:fn #'noether/-line-format)
(list
:label "T: "
:name :time
:len 8
:init (lambda ()
(setq noether/-timer
(run-with-timer 1 1 #'noether/-set-time)))
:deinit (lambda ()
(when noether/-timer
(cancel-timer noether/-timer)))
:var 'noether/-time
:fn #'noether/-time-format)
))
(line-unit)
(time-unit :label "<B>")))
(setq noether/views (list example-bar))