From 6a64ae46206bfcb3fde1780126d59229793fa94c Mon Sep 17 00:00:00 2001 From: jao Date: Fri, 2 Sep 2022 16:39:38 +0100 Subject: jao-shell improvements --- init.el | 46 ++++++++++++++++++++-------------------------- lib/eos/jao-shell.el | 31 ++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/init.el b/init.el index 770688c..ea860aa 100644 --- a/init.el +++ b/init.el @@ -167,12 +167,12 @@ (defun jao-bright-up () (interactive) - (jao-shell-string "brightnessctl -q s 5%%+") + (jao-shell-exec "brightnessctl -q s 5%%+" t) (jao-bright-show)) (defun jao-bright-down () (interactive) - (jao-shell-string "brightnessctl -q s 5%%-") + (jao-shell-exec "brightnessctl -q s 5%%-" t) (jao-bright-show)) ;;;; keyboard @@ -467,26 +467,24 @@ (when jao-wayland-enabled (add-hook 'after-init-hook #'jao-wayland-enable)) -(defun jao-wayland-type (&rest args) - (apply 'jao-shell-string "wtype" args)) +(defsubst jao-wayland-type (&rest args) + (apply #'jao-shell-exec* t "wtype" args)) ;;;; river (defvar jao-river-enabled (jao-shell-running-p "river")) (defun jao-river-enabled-p () jao-river-enabled) -(defun jao-river-to-ws (n) +(defsubst jao-river-to-ws (n) (jao-wayland-type "-M" "win" (format "%s" n))) -(defun jao-river-window-list () - (let ((json-false nil) - (json-null nil)) - (json-read-from-string (shell-command-to-string "lswt -j")))) +(defsubst jao-river-window-list () + (jao-shell-output "lswt -j" (lambda () (let ((json-false nil)) (json-read))))) (defun jao-river-focused () (seq-some (lambda (w) (and (alist-get 'activated w) w)) (jao-river-window-list))) -(defun jao-river-get-focused-title () +(defsubst jao-river-get-focused-title () (alist-get 'title (jao-river-focused))) (defun jao-river-focus-window (title) @@ -495,13 +493,14 @@ ws))) (or (alist-get 'activated w) (seq-some (lambda (_ignored) - (jao-shell-string "riverctl focus-view next") + (jao-shell-exec "riverctl focus-view next" t) (equal title (jao-river-get-focused-title))) (and w ws))))) (defun jao-river-zathura-to-org () (let ((title (jao-river-get-focused-title))) (when (string-match-p "\\.pdf" title) + (message "%s" title) (jao-notify title "Opening ORG counterpart")) (jao-river-to-ws 1) (jao-pdf-goto-zathura-org title t))) @@ -526,8 +525,7 @@ (defun jao-river-restart-i3bar () (interactive) - (when (jao-shell-running-p "i3bar-river") - (jao-shell-string "killall i3bar-river")) + (jao-shell-kill-p "i3bar-river") (jao-shell-exec "i3bar-river") (sit-for 0.2) (jao-tracking-set-log "")) @@ -556,7 +554,7 @@ (let ((n (file-name-nondirectory file))) (jao-swaymsg "workspace number 3") (unless (= 0 (jao-swaymsg (format "[title=\"%s\"] focus" n))) - (jao-shell-exec (jao-pdf-zathura-open-cmd file page ""))) + (jao-shell-exec (jao-pdf-zathura-open-cmd file page))) (when page (sit-for 0.2) (jao-wayland-type (format "%dg" page))))) (defun jao-sway-set-wallpaper (f) @@ -618,8 +616,8 @@ (interactive) (let ((wid (jao-shell-string "xdotool getwindowfocus"))) (if (jao-screensaver-enabled) - (jao-shell-string "xdg-screensaver suspend" wid) - (jao-shell-string "xdg-screensaver resume" wid)) + (jao-shell-exec* "xdg-screensaver" "suspend" wid) + (jao-shell-exec* "xdg-screensaver" wid)) (jao-notify (format "Using '%s'" (jao-shell-string "xdotool getwindownames" wid)) (format "Screensaver %s" @@ -1869,11 +1867,11 @@ (if (string-blank-p id) (progn (when jao-xmonad-enabled - (jao-shell-exec "xdotool set_destktop 2")) - (jao-shell-string (jao-pdf-zathura-open-cmd file page " &"))) + (jao-shell-exec "xdotool set_destktop 2" t)) + (jao-shell-exec (jao-pdf-zathura-open-cmd file page) t)) (let* ((page (if page (format " && xdotool type %dg" page) "")) (cmd (format "xdotool windowactivate %s%s" id page))) - (jao-shell-string cmd))))) + (jao-shell-exec cmd t))))) (defun jao-x11-zathura-goto-org (&optional title no-ask) (let ((title (or title (jao-shell-string "xdotool" @@ -2804,9 +2802,7 @@ (defun jao-toggle-nm-applet () (interactive) - (if (jao-shell-running-p "nm-applet") - (jao-shell-string "killall nm-applet") - (jao-nm-applet))) + (or (jao-shell-kill-p "nm-applet") (jao-nm-applet))) ;;;; enwc (use-package enwc @@ -3069,7 +3065,7 @@ (string-to-number level))))) (defun jao-mixer-set (dev v) - (jao-shell-string "amixer sset" dev v) + (jao-shell-exec* t "amixer" "sset" dev v) (jao-mixer-get-level dev)) (defun jao-mixer-master-toggle () @@ -3096,9 +3092,7 @@ (defun jao-toggle-audio-applet () (interactive) - (if (string-empty-p (jao-shell-string "pidof pasystray")) - (jao-audio-applet) - (jao-shell-string "killall pasystray"))) + (or (jao-shell-kill-p "paystray") (jao-audio-applet))) (global-set-key (kbd "") #'jao-toggle-audio-applet) diff --git a/lib/eos/jao-shell.el b/lib/eos/jao-shell.el index ff1c160..86bf46b 100644 --- a/lib/eos/jao-shell.el +++ b/lib/eos/jao-shell.el @@ -24,25 +24,29 @@ ;;; Code: -(defun jao-shell--quote (x) (shell-quote-argument (format "%s" x))) - -;;;###autoload (defun jao-shell-cmd-lines (cmd &rest args) - (let ((cmd (concat cmd " " (mapconcat #'jao-shell--quote args " ")))) + (let ((cmd (concat cmd " " (combine-and-quote-strings args)))) (split-string (shell-command-to-string cmd) "\n" t))) -;;;###autoload (defun jao-shell-string (cmd &rest args) (string-trim (or (car (apply #'jao-shell-cmd-lines cmd args)) ""))) -;;;###autoload -(defun jao-shell-exec (command) +(defun jao-shell-exec (command &optional wait) (interactive (list (read-shell-command "$ " (if current-prefix-arg (cons (concat " " (buffer-file-name)) 0) "")))) - (start-process-shell-command command nil command)) + (if wait + (call-process-shell-command command) + (start-process-shell-command command nil command))) + +(defun jao-shell-exec* (command-or-wait &rest args) + (let ((wait (and (not (stringp command-or-wait)) command-or-wait)) + (args (if (stringp command-or-wait) (cons command-or-wait args) args))) + (jao-shell-exec (combine-and-quote-strings args) wait))) + +(defun jao-shell-exec-p (command) (eq 0 (jao-shell-exec command t))) (defmacro jao-shell-def-exec (name &rest args) `(defun ,name (&rest other-args) @@ -52,9 +56,14 @@ "*jao-exec - console*" (string-join (append (list ,@args) other-args) " ")))) -;;;###autoload -(defun jao-shell-running-p (pr) - (not (string-blank-p (shell-command-to-string (concat "pidof " pr))))) +(defun jao-shell-output (cmd handler) + (with-temp-buffer + (call-process-shell-command cmd nil (current-buffer)) + (beginning-of-buffer) + (funcall handler))) + +(defun jao-shell-running-p (pr) (eq 0 (jao-shell-exec* t "pidof" pr))) +(defun jao-shell-kill-p (pr) (eq 0 (jao-shell-exec* t "killall" pr))) (provide 'jao-shell) ;;; jao-shell.el ends here -- cgit v1.2.3