From 3cfcfc5055d8c14d1cdbf6d0dfb3be91d5d69727 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 6 Sep 2022 23:18:49 +0200 Subject: [PATCH] refactor(lib): use with-memoization Ref: 2b01166d1d8d --- lisp/lib/system.el | 147 ++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 76 deletions(-) diff --git a/lisp/lib/system.el b/lisp/lib/system.el index e7961103a..0cb357f47 100644 --- a/lisp/lib/system.el +++ b/lisp/lib/system.el @@ -3,27 +3,25 @@ ;;;###autoload (defun doom-system-distro () "Return a symbol representing the installed distro." - ;; REVIEW Use `with-memoization' when 27.x support is dropped - (or (get 'doom-system-distro 'cached-value) - (put 'doom-system-distro 'cached-value - (cond (IS-WINDOWS 'windows) - (IS-MAC 'macos) - ((and (file-exists-p "/etc/os-release") - (with-temp-buffer - (let ((coding-system-for-read 'utf-8-auto)) - (insert-file-contents "/etc/os-release")) - (when (re-search-forward "^ID=\"?\\([^\"\n]+\\)\"?" nil t) - (intern (downcase (match-string 1))))))) - ;; A few redundancies in case os-release fails us - ((file-exists-p "/etc/debian_version") - 'debian) - ((executable-find "nixos-version") - 'nixos) - ((and (or (file-exists-p "/etc/config.scm") - (file-directory-p "/run/current-system")) - (executable-find "guix")) - 'guix) - ('linux))))) + (with-memoization (get 'doom-system-distro 'cached-value) + (cond (IS-WINDOWS 'windows) + (IS-MAC 'macos) + ((and (file-exists-p "/etc/os-release") + (with-temp-buffer + (let ((coding-system-for-read 'utf-8-auto)) + (insert-file-contents "/etc/os-release")) + (when (re-search-forward "^ID=\"?\\([^\"\n]+\\)\"?" nil t) + (intern (downcase (match-string 1))))))) + ;; A few redundancies in case os-release fails us + ((file-exists-p "/etc/debian_version") + 'debian) + ((executable-find "nixos-version") + 'nixos) + ((and (or (file-exists-p "/etc/config.scm") + (file-directory-p "/run/current-system")) + (executable-find "guix")) + 'guix) + ('linux)))) ;;;###autoload (defun doom-system-distro-version () @@ -59,63 +57,60 @@ ;;;###autoload (defun doom-system-distro-icon () "Display icon for the installed distro." - ;; REVIEW Use `with-memoization' when 27.x support is dropped - (or (get 'doom-system-distro-icon 'cached-value) - (put 'doom-system-distro-icon 'cached-value - (propertize - (pcase (doom-system-distro) - (`windows (all-the-icons-faicon "windows")) - (`macos (all-the-icons-faicon "apple")) - (`arch "\uF303") - (`debian "\uF306") - (`raspbian "\uF315") - (`ubuntu "\uF31b") - (`elementary "\uF309") - (`fedora "\uF30a") - (`coreos "\uF305") - (`gentoo "\uF30d") - (`mageia "\uF310") - (`centos "\uF304") - ((or `opensuse `tumbleweed) "\uF314") - (`sabayon "\uF317") - (`slackware "\uF319") - (`linuxmint "\uF30e") - (`alpine "\uF300") - (`aosc "\uF301") - (`nixos "\uF313") - (`devuan "\uF307") - (`manjaro "\uF312") - ((or `void `artix) "\uF17c") - (_ (all-the-icons-faicon "linux"))) - 'face '(:height 1) - 'display '(raise 0))))) + (with-memoization (get 'doom-system-distro-icon 'cached-value) + (propertize + (pcase (doom-system-distro) + (`windows (all-the-icons-faicon "windows")) + (`macos (all-the-icons-faicon "apple")) + (`arch "\uF303") + (`debian "\uF306") + (`raspbian "\uF315") + (`ubuntu "\uF31b") + (`elementary "\uF309") + (`fedora "\uF30a") + (`coreos "\uF305") + (`gentoo "\uF30d") + (`mageia "\uF310") + (`centos "\uF304") + ((or `opensuse `tumbleweed) "\uF314") + (`sabayon "\uF317") + (`slackware "\uF319") + (`linuxmint "\uF30e") + (`alpine "\uF300") + (`aosc "\uF301") + (`nixos "\uF313") + (`devuan "\uF307") + (`manjaro "\uF312") + ((or `void `artix) "\uF17c") + (_ (all-the-icons-faicon "linux"))) + 'face '(:height 1) + 'display '(raise 0)))) ;;;###autoload (defun doom-system-cpus () "Return the max number of processing units on this system. Tries to be portable. Returns 1 if cannot be determined." - (or (get 'doom-system-cpus 'cached-value) - (put 'doom-system-cpus 'cached-value - (let ((cpus - (cond ((fboundp 'w32-get-nproc) - (w32-get-nproc)) - ((getenv "NUMBER_OF_PROCESSORS")) - ((executable-find "nproc") - (doom-call-process "nproc")) - ((executable-find "sysctl") - (doom-call-process "sysctl" "-n" "hw.ncpu"))))) - (max - 1 (or (cl-typecase cpus - (integer cpus) - (string - (condition-case _ - (string-to-number cpus) - (wrong-type-argument - (user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S" - cpus)))) - (cons - (if (zerop (car cpus)) - (string-to-number (cdr cpus)) - (user-error "Failed to look up number of processors, because:\n\n%s" - (cdr cpus))))) - 1)))))) + (with-memoization (get 'doom-system-cpus 'cached-value) + (let ((cpus + (cond ((fboundp 'w32-get-nproc) + (w32-get-nproc)) + ((getenv "NUMBER_OF_PROCESSORS")) + ((executable-find "nproc") + (doom-call-process "nproc")) + ((executable-find "sysctl") + (doom-call-process "sysctl" "-n" "hw.ncpu"))))) + (max + 1 (or (cl-typecase cpus + (integer cpus) + (string + (condition-case _ + (string-to-number cpus) + (wrong-type-argument + (user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S" + cpus)))) + (cons + (if (zerop (car cpus)) + (string-to-number (cdr cpus)) + (user-error "Failed to look up number of processors, because:\n\n%s" + (cdr cpus))))) + 1)))))