Switch to `get-program-code-output' and explicit destructuring

This commit is contained in:
Gerard Vermeulen 2024-06-17 19:31:20 +02:00
parent 6e0d21f7b7
commit b2f3aa4ef7

View File

@ -905,10 +905,10 @@ output to =stdout=.
#+name: lst:process-utilities
#+begin_src emacs-lisp -n :results silent
;; https://gitlab.com/howardabrams/spacemacs.d/blob/master/layers/ha-org/funcs.el#L418
(defun shell-command-with-exit-code (program &rest args)
"Run PROGRAM with ARGS and return exit-code and output in a list."
(defun get-program-code-output (program &rest args)
"Run PROGRAM with ARGS and return the `cons' of return-code and output."
(with-temp-buffer
(list (apply 'call-process program nil (current-buffer) nil args)
(cons (apply 'call-process program nil (current-buffer) nil args)
(buffer-substring-no-properties (point-min) (point-max)))))
#+end_src
@ -1977,10 +1977,11 @@ configuration objectives:
(defun update-lualatex-opentype-font-name-database ()
"Update the \"OpenType Font\" name database for \"LuaLaTeX\"."
(interactive)
(pcase-let ((`(,exit-code ,output)
(shell-command-with-exit-code
"luaotfload-tool" "-vv" "--update" "--force")))
(if (= 0 exit-code)
(let* ((pair (get-program-code-output
"luaotfload-tool" "-vv" "--update" "--force"))
(return-code (car pair))
(output (cdr pair)))
(if (= 0 return-code)
(message "%s" (string-trim output))
(error "%s" (string-trim output))))))
#+end_src
@ -2642,9 +2643,10 @@ valid directories and files. In an [[https://orgmode.org/][Org-mode]] buffer th
(defun biber-delete-cache ()
"Delete the `Biber' cache to get rid of `Biber' exit code 2."
(interactive)
(pcase-let ((`(,exit-code ,output)
(shell-command-with-exit-code "rm" "-rf" "$(biber --cache)")))
(if (= 0 exit-code)
(let* ((pair (get-program-code-output "rm" "-rf" "$(biber --cache)"))
(return-code (car pair))
(output (cdr pair)))
(if (= 0 return-code)
(message "%s" (string-trim output))
(error "%s" (string-trim output))))))
#+end_src
@ -4871,35 +4873,39 @@ through =cat= to remove escape sequences.
(defun pyenv-root ()
"Return \"pyenv root\" as a directory."
(pcase-let ((`(,exit-code ,output)
(shell-command-with-exit-code "pyenv" "root")))
(if (= 0 exit-code)
(let* ((pair (get-program-code-output "pyenv" "root"))
(return-code (car pair))
(output (cdr pair)))
(if (= 0 return-code)
(file-name-as-directory (string-trim output))
(error "%s" (string-trim output)))))
(defun pyenv-version-name ()
"Return \"pyenv version-name\"."
(pcase-let ((`(,exit-code ,output)
(shell-command-with-exit-code "pyenv" "version-name")))
(if (= 0 exit-code)
(let* ((pair (get-program-code-output "pyenv" "version-name"))
(return-code (car pair))
(output (cdr pair)))
(if (= 0 return-code)
(string-trim output)
(error "%s" (string-trim output)))))
(defun pyenv-versions ()
"Return \"pyenv versions --bare --skip-aliases\" as a list."
(pcase-let ((`(,exit-code ,output)
(shell-command-with-exit-code
"pyenv" "versions" "--bare" "--skip-aliases")))
(if (= 0 exit-code)
(let* ((pair (get-program-code-output
"pyenv" "versions" "--bare" "--skip-aliases"))
(return-code (car pair))
(output (cdr pair)))
(if (= 0 return-code)
(split-string output)
(error "%s" (string-trim output)))))
(defun pyenv-virtualenvs ()
"Return \"pyenv virtualenvs --bare --skip-aliases\" as a list."
(pcase-let ((`(,exit-code ,output)
(shell-command-with-exit-code
"pyenv" "virtualenvs" "--bare" "--skip-aliases")))
(if (= 0 exit-code)
(let* ((pair (get-program-code-output
"pyenv" "virtualenvs" "--bare" "--skip-aliases"))
(return-code (car pair))
(output (cdr pair)))
(if (= 0 return-code)
(split-string output)
(error "%s" (string-trim output))))))
#+end_src