refactor(lib): use with-memoization

Ref: 2b01166d1d
This commit is contained in:
Henrik Lissner 2022-09-06 23:18:49 +02:00
parent cee89a5d3f
commit 3cfcfc5055
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -3,9 +3,7 @@
;;;###autoload ;;;###autoload
(defun doom-system-distro () (defun doom-system-distro ()
"Return a symbol representing the installed distro." "Return a symbol representing the installed distro."
;; REVIEW Use `with-memoization' when 27.x support is dropped (with-memoization (get 'doom-system-distro 'cached-value)
(or (get 'doom-system-distro 'cached-value)
(put 'doom-system-distro 'cached-value
(cond (IS-WINDOWS 'windows) (cond (IS-WINDOWS 'windows)
(IS-MAC 'macos) (IS-MAC 'macos)
((and (file-exists-p "/etc/os-release") ((and (file-exists-p "/etc/os-release")
@ -23,7 +21,7 @@
(file-directory-p "/run/current-system")) (file-directory-p "/run/current-system"))
(executable-find "guix")) (executable-find "guix"))
'guix) 'guix)
('linux))))) ('linux))))
;;;###autoload ;;;###autoload
(defun doom-system-distro-version () (defun doom-system-distro-version ()
@ -59,9 +57,7 @@
;;;###autoload ;;;###autoload
(defun doom-system-distro-icon () (defun doom-system-distro-icon ()
"Display icon for the installed distro." "Display icon for the installed distro."
;; REVIEW Use `with-memoization' when 27.x support is dropped (with-memoization (get 'doom-system-distro-icon 'cached-value)
(or (get 'doom-system-distro-icon 'cached-value)
(put 'doom-system-distro-icon 'cached-value
(propertize (propertize
(pcase (doom-system-distro) (pcase (doom-system-distro)
(`windows (all-the-icons-faicon "windows")) (`windows (all-the-icons-faicon "windows"))
@ -88,14 +84,13 @@
((or `void `artix) "\uF17c") ((or `void `artix) "\uF17c")
(_ (all-the-icons-faicon "linux"))) (_ (all-the-icons-faicon "linux")))
'face '(:height 1) 'face '(:height 1)
'display '(raise 0))))) 'display '(raise 0))))
;;;###autoload ;;;###autoload
(defun doom-system-cpus () (defun doom-system-cpus ()
"Return the max number of processing units on this system. "Return the max number of processing units on this system.
Tries to be portable. Returns 1 if cannot be determined." Tries to be portable. Returns 1 if cannot be determined."
(or (get 'doom-system-cpus 'cached-value) (with-memoization (get 'doom-system-cpus 'cached-value)
(put 'doom-system-cpus 'cached-value
(let ((cpus (let ((cpus
(cond ((fboundp 'w32-get-nproc) (cond ((fboundp 'w32-get-nproc)
(w32-get-nproc)) (w32-get-nproc))
@ -118,4 +113,4 @@ Tries to be portable. Returns 1 if cannot be determined."
(string-to-number (cdr cpus)) (string-to-number (cdr cpus))
(user-error "Failed to look up number of processors, because:\n\n%s" (user-error "Failed to look up number of processors, because:\n\n%s"
(cdr cpus))))) (cdr cpus)))))
1)))))) 1)))))