From f4734014ac5bee7fede76b83f7257b4bab57e59f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20K=C3=A4stner?= Date: Sat, 1 May 2021 12:39:36 +0200 Subject: [PATCH 1/4] Provide more operating system identification methods While `/etc/os-release` is usually available on Linux systems, it might be missing on distribution that don't follow that convention. For those distributions, the commands `hostnamectl` (from systemd) and `lsb_release` might provide an alternative. Unfortunately, those commands might also be missing as well as `/etc/os-release`, so the newly introduced `eshell-info-banner--get-os-information` function falls back to the string "Unknown" (and should never fail). Fixes #1. --- eshell-info-banner.el | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/eshell-info-banner.el b/eshell-info-banner.el index 8b2c7c8..5470289 100644 --- a/eshell-info-banner.el +++ b/eshell-info-banner.el @@ -292,20 +292,46 @@ the warning face with a battery level of 25% or less." (number-to-string percentage) :inherit (eshell-info-banner--get-color-percentage (- 100.0 percentage))))))))) + ; Operating system identification ;;;;;;;;;;;;;;;;;; +(defun eshell-info-banner--get-os-information-from-release-file (&optional release-file) + "Read the operating system from the given RELEASE-FILE. + +If RELEASE-FILE is nil, use '/etc/os-release'." + (setq release-file (or release-file "/etc/os-release")) + (replace-regexp-in-string + ".*\"\\(.+\\)\"" + "\\1" + (car (-filter (lambda (line) + (s-contains? "PRETTY_NAME" line)) + (s-lines (with-temp-buffer + (insert-file-contents release-file) + (buffer-string))))))) + +(defun eshell-info-banner--get-os-information-from-hostnamectl () + "Read the operating system via hostnamectl." + (with-temp-buffer + (call-process "hostnamectl" nil t nil) + (re-search-backward "Operating System: \\(.*\\)") + (match-string 1))) + +(defun eshell-info-banner--get-os-information-from-lsb-release () + "Read the operating system information from lsb_release" + (shell-command-to-string "lsb_release -d -s")) + +(defun eshell-info-banner--get-os-information () + "Get operating system identifying information." + (or (ignore-errors (eshell-info-banner--get-os-information-from-hostnamectl)) + (ignore-errors (eshell-info-banner--get-os-information-from-lsb-release)) + (ignore-errors (eshell-info-banner--get-os-information-from-release)) + "Unkown")) + ; Public functions ;;;;;;;;;;;;;;;;;;;; ;;;###autoload (defun eshell-info-banner () "Banner for Eshell displaying system information." (let* ((partitions (eshell-info-banner--get-mounted-partitions)) - (os (replace-regexp-in-string - ".*\"\\(.+\\)\"" - "\\1" - (car (-filter (lambda (line) - (s-contains? "PRETTY_NAME" line)) - (s-lines (with-temp-buffer - (insert-file-contents "/etc/os-release") - (buffer-string))))))) + (os (eshell-info-banner--get-os-information)) (hostname (system-name)) (uptime (s-chop-prefix "up " (s-trim (shell-command-to-string "uptime -p")))) From 9ccd234a3d9470d743a3a37d935f8ad99ace60f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20K=C3=A4stner?= Date: Sat, 1 May 2021 12:47:29 +0200 Subject: [PATCH 2/4] os: search in temporary buffer instead of string Instead of filtering all lines for `PRETTY_LINE` and then replace the line with only the quoted contents, `re-serach-forward` (or -backward) provide a `match-*` interface. That way we can work solely inside the buffer and return only the matching string part. --- eshell-info-banner.el | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/eshell-info-banner.el b/eshell-info-banner.el index 5470289..7e8e42e 100644 --- a/eshell-info-banner.el +++ b/eshell-info-banner.el @@ -298,14 +298,11 @@ the warning face with a battery level of 25% or less." If RELEASE-FILE is nil, use '/etc/os-release'." (setq release-file (or release-file "/etc/os-release")) - (replace-regexp-in-string - ".*\"\\(.+\\)\"" - "\\1" - (car (-filter (lambda (line) - (s-contains? "PRETTY_NAME" line)) - (s-lines (with-temp-buffer - (insert-file-contents release-file) - (buffer-string))))))) + (with-temp-buffer + (insert-file-contents release-file) + (goto-char (point-min)) + (re-search-forward "PRETTY_NAME=\"\\(.*\\)\"") + (match-string 1))) (defun eshell-info-banner--get-os-information-from-hostnamectl () "Read the operating system via hostnamectl." From c432776b95f4f50c96c451ac43cf7c666bceba81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20K=C3=A4stner?= Date: Sat, 1 May 2021 17:48:15 +0200 Subject: [PATCH 3/4] os: Check for programs/files instead of ignoring errors --- eshell-info-banner.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/eshell-info-banner.el b/eshell-info-banner.el index 7e8e42e..6d09734 100644 --- a/eshell-info-banner.el +++ b/eshell-info-banner.el @@ -317,10 +317,11 @@ If RELEASE-FILE is nil, use '/etc/os-release'." (defun eshell-info-banner--get-os-information () "Get operating system identifying information." - (or (ignore-errors (eshell-info-banner--get-os-information-from-hostnamectl)) - (ignore-errors (eshell-info-banner--get-os-information-from-lsb-release)) - (ignore-errors (eshell-info-banner--get-os-information-from-release)) - "Unkown")) + (cond + ((executable-find "hostnamectl") (eshell-info-banner--get-os-information-from-hostnamectl)) + ((executable-find "lsb_release") (eshell-info-banner--get-os-information-from-lsb-release)) + ((file-exists-p "/etc/os-release") (eshell-info-banner--get-os-information-from-release-file)) + (t "Unknown"))) ; Public functions ;;;;;;;;;;;;;;;;;;;; From 3ac03293a9d6c06266452cd11a70c0a5397d24ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20K=C3=A4stner?= Date: Sat, 1 May 2021 23:46:56 +0200 Subject: [PATCH 4/4] os: Remove fall-back on function argument As `release-file` is used only once in the function, we don't need to set its default value via `setq' if it is `nil`. --- eshell-info-banner.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/eshell-info-banner.el b/eshell-info-banner.el index 6d09734..fdffd3a 100644 --- a/eshell-info-banner.el +++ b/eshell-info-banner.el @@ -297,9 +297,8 @@ the warning face with a battery level of 25% or less." "Read the operating system from the given RELEASE-FILE. If RELEASE-FILE is nil, use '/etc/os-release'." - (setq release-file (or release-file "/etc/os-release")) (with-temp-buffer - (insert-file-contents release-file) + (insert-file-contents (or release-file "/etc/os-release")) (goto-char (point-min)) (re-search-forward "PRETTY_NAME=\"\\(.*\\)\"") (match-string 1)))