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,27 +3,25 @@
;;;###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) (cond (IS-WINDOWS 'windows)
(put 'doom-system-distro 'cached-value (IS-MAC 'macos)
(cond (IS-WINDOWS 'windows) ((and (file-exists-p "/etc/os-release")
(IS-MAC 'macos) (with-temp-buffer
((and (file-exists-p "/etc/os-release") (let ((coding-system-for-read 'utf-8-auto))
(with-temp-buffer (insert-file-contents "/etc/os-release"))
(let ((coding-system-for-read 'utf-8-auto)) (when (re-search-forward "^ID=\"?\\([^\"\n]+\\)\"?" nil t)
(insert-file-contents "/etc/os-release")) (intern (downcase (match-string 1)))))))
(when (re-search-forward "^ID=\"?\\([^\"\n]+\\)\"?" nil t) ;; A few redundancies in case os-release fails us
(intern (downcase (match-string 1))))))) ((file-exists-p "/etc/debian_version")
;; A few redundancies in case os-release fails us 'debian)
((file-exists-p "/etc/debian_version") ((executable-find "nixos-version")
'debian) 'nixos)
((executable-find "nixos-version") ((and (or (file-exists-p "/etc/config.scm")
'nixos) (file-directory-p "/run/current-system"))
((and (or (file-exists-p "/etc/config.scm") (executable-find "guix"))
(file-directory-p "/run/current-system")) 'guix)
(executable-find "guix")) ('linux))))
'guix)
('linux)))))
;;;###autoload ;;;###autoload
(defun doom-system-distro-version () (defun doom-system-distro-version ()
@ -59,63 +57,60 @@
;;;###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) (propertize
(put 'doom-system-distro-icon 'cached-value (pcase (doom-system-distro)
(propertize (`windows (all-the-icons-faicon "windows"))
(pcase (doom-system-distro) (`macos (all-the-icons-faicon "apple"))
(`windows (all-the-icons-faicon "windows")) (`arch "\uF303")
(`macos (all-the-icons-faicon "apple")) (`debian "\uF306")
(`arch "\uF303") (`raspbian "\uF315")
(`debian "\uF306") (`ubuntu "\uF31b")
(`raspbian "\uF315") (`elementary "\uF309")
(`ubuntu "\uF31b") (`fedora "\uF30a")
(`elementary "\uF309") (`coreos "\uF305")
(`fedora "\uF30a") (`gentoo "\uF30d")
(`coreos "\uF305") (`mageia "\uF310")
(`gentoo "\uF30d") (`centos "\uF304")
(`mageia "\uF310") ((or `opensuse `tumbleweed) "\uF314")
(`centos "\uF304") (`sabayon "\uF317")
((or `opensuse `tumbleweed) "\uF314") (`slackware "\uF319")
(`sabayon "\uF317") (`linuxmint "\uF30e")
(`slackware "\uF319") (`alpine "\uF300")
(`linuxmint "\uF30e") (`aosc "\uF301")
(`alpine "\uF300") (`nixos "\uF313")
(`aosc "\uF301") (`devuan "\uF307")
(`nixos "\uF313") (`manjaro "\uF312")
(`devuan "\uF307") ((or `void `artix) "\uF17c")
(`manjaro "\uF312") (_ (all-the-icons-faicon "linux")))
((or `void `artix) "\uF17c") 'face '(:height 1)
(_ (all-the-icons-faicon "linux"))) 'display '(raise 0))))
'face '(:height 1)
'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)) ((getenv "NUMBER_OF_PROCESSORS"))
((getenv "NUMBER_OF_PROCESSORS")) ((executable-find "nproc")
((executable-find "nproc") (doom-call-process "nproc"))
(doom-call-process "nproc")) ((executable-find "sysctl")
((executable-find "sysctl") (doom-call-process "sysctl" "-n" "hw.ncpu")))))
(doom-call-process "sysctl" "-n" "hw.ncpu"))))) (max
(max 1 (or (cl-typecase cpus
1 (or (cl-typecase cpus (integer cpus)
(integer cpus) (string
(string (condition-case _
(condition-case _ (string-to-number cpus)
(string-to-number cpus) (wrong-type-argument
(wrong-type-argument (user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S"
(user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S" cpus))))
cpus)))) (cons
(cons (if (zerop (car cpus))
(if (zerop (car cpus)) (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))))))