From c5e3f4d632c1f33d8e717dd327404fb86195de6b Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 1 Mar 2021 11:02:07 -0500 Subject: [PATCH] New autoload/system.el core library --- core/autoload/debug.el | 21 +------- core/autoload/process.el | 28 ----------- core/autoload/system.el | 106 +++++++++++++++++++++++++++++++++++++++ core/core.el | 2 +- 4 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 core/autoload/system.el diff --git a/core/autoload/debug.el b/core/autoload/debug.el index 8099ada26..6507950b8 100644 --- a/core/autoload/debug.el +++ b/core/autoload/debug.el @@ -125,26 +125,7 @@ ready to be pasted in a bug report on github." (concat " -> " (file-truename file)) "")))) `((system - (info . ,(cons - (cond - (IS-WINDOWS "Windows") - (IS-MAC (format "MacOS "(sh "sw_vers" "-productVersion"))) - ((executable-find "lsb_release") - (sh "lsb_release" "-s" "-d")) - ((executable-find "nixos-version") - (format "NixOS %s" (sh "nixos-version"))) - ((file-exists-p "/etc/os-release") - (let ((release (cat "/etc/os-release"))) - (when (string-match "^PRETTY_NAME=\"\\([^\"]+\\)\"" release) - (match-string 1 release)))) - ((file-exists-p "/etc/debian_version") - (format "Debian %s" (car "/etc/debian_version"))) - ((when-let (files (doom-glob "/etc/*-release")) - (truncate-string-to-width - (replace-regexp-in-string "\n" " " (cat (car files) 73) nil t) - 64 nil nil "..."))) - ((concat "Unknown " (sh "uname" "-v")))) - (sh "uname" "-msr"))) + (info . ,(cons (doom-system-distro-version) (sh "uname" "-msr"))) (shell . ,(abbrev-path shell-file-name)) (path . ,(mapcar #'abbrev-path exec-path))) (emacs diff --git a/core/autoload/process.el b/core/autoload/process.el index 243863161..66d5d6a8c 100644 --- a/core/autoload/process.el +++ b/core/autoload/process.el @@ -40,31 +40,3 @@ Warning: freezes indefinitely on any stdin prompt." (sit-for 0.1)) (process-exit-status process)) (string-trim (buffer-string))))) - -(defvar doom--num-cpus nil) -;;;###autoload -(defun doom-num-cpus () - "Return the max number of processing units on this system. -Tries to be portable. Returns 1 if cannot be determined." - (or doom--num-cpus - (setq doom--num-cpus - (let ((cpus - (cond ((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 - (string - (condition-case _ - (string-to-number cpus) - (wrong-type-argument - (user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S" - cpus)))) - (consp - (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)))))) diff --git a/core/autoload/system.el b/core/autoload/system.el new file mode 100644 index 000000000..46a5dbfe5 --- /dev/null +++ b/core/autoload/system.el @@ -0,0 +1,106 @@ +;;; core/autoload/system.el -*- lexical-binding: t; -*- + +;;;###autoload +(defun doom-system-distro () + "Return a symbol representing the installed distro." + (cond (IS-WINDOWS 'windows) + (IS-MAC 'macos) + ((file-exists-p "/etc/os-release") + (with-temp-buffer + (insert-file-contents "/etc/os-release") + (when (re-search-forward "^ID=\\([^\"$]+\\)" 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 () + "Return a distro name and version string." + (letf! (defun sh (&rest args) (cdr (apply #'doom-call-process args))) + (let ((disto (doom-system-distro))) + (cond + ((eq distro 'windows) + (format "Windows %s" "Unknown")) ; TODO + ((eq distro 'macos) + (format "MacOS %s" (sh "sw_vers" "-productVersion"))) + ((executable-find "lsb_release") + (sh "lsb_release" "-s" "-d")) + ((executable-find "nixos-version") + (format "NixOS %s" (sh "nixos-version"))) + ((file-exists-p "/etc/os-release") + (with-temp-buffer + (insert-file-contents "/etc/os-release") + (when (re-search-forward "^PRETTY_NAME=\"\\([^\"]+\\)\"" nil t) + (intern (downcase (match-string 1)))))) + ((when-let (files (doom-glob "/etc/*-release")) + (truncate-string-to-width + (replace-regexp-in-string "\n" " " (cat (car files) 73) nil t) + 64 nil nil "..."))) + ((concat "Unknown " (sh "uname" "-v"))))))) + +;;;###autoload +(defun doom-system-distro-icon () + "Display icon for the installed distro." + (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))) + +(defvar doom--system-cpus nil) +;;;###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 doom--system-cpus + (setq doom--system-cpus + (let ((cpus + (cond ((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 + (string + (condition-case _ + (string-to-number cpus) + (wrong-type-argument + (user-error "NUMBER_OF_PROCESSORS contains an invalid value: %S" + cpus)))) + (consp + (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)))))) diff --git a/core/core.el b/core/core.el index 77b620883..ef5b50edc 100644 --- a/core/core.el +++ b/core/core.el @@ -167,7 +167,7 @@ users).") ;; things ahead-of-time in a non-interactive session. (defun doom--comp-use-all-cores-a () (if (zerop comp-async-jobs-number) - (setq comp-num-cpus (doom-num-cpus)) + (setq comp-num-cpus (doom-system-cpus)) comp-async-jobs-number)) (advice-add #'comp-effective-async-max-jobs :override #'doom--comp-use-all-cores-a))