From a26b7a8116556349ee3a53fc329fcf73e5655dff Mon Sep 17 00:00:00 2001 From: jao Date: Fri, 18 Mar 2022 21:32:19 +0000 Subject: jao-shell: a couple functions moved there --- init.org | 79 +++++++++++++++++++++------------------------------- lib/eos/jao-shell.el | 33 ++++++++++++++++++---- 2 files changed, 59 insertions(+), 53 deletions(-) diff --git a/init.org b/init.org index 86f8865..a0200c0 100644 --- a/init.org +++ b/init.org @@ -135,27 +135,10 @@ #+end_src *** Process runners #+begin_src emacs-lisp - (defun jao-exec-string (fmt &rest args) - (string-trim (shell-command-to-string (apply 'format fmt args)))) - - (defun jao-exec (command) - (interactive - (list (read-shell-command "$ " - (if current-prefix-arg - (cons (concat " " (buffer-file-name)) 0) - "")))) - (start-process-shell-command command nil command)) - - (defmacro jao-def-exec (name &rest args) - `(defun ,name (&rest other-args) - (interactive) - (start-process-shell-command - ,(car args) - "* jao-exec - console *" - (string-join (append (list ,@args) other-args) " ")))) - - (defun jao-exec-running-p (pr) - (not (string-blank-p (shell-command-to-string (concat "pidof " pr))))) + (use-package jao-shell + :demand t + :config (jao-shell-def-exec jao-trayer "trayer.sh") + :bind (("s-r" . jao-shell-exec))) #+end_src *** App launcher @@ -166,24 +149,24 @@ #+end_src *** Brightness control #+begin_src emacs-lisp - (jao-def-exec jao-bright-set-up "brightnessctl" "-q" "s" "5%+") - (jao-def-exec jao-bright-set-down "brightnessctl" "-q" "s" "5%-") + (jao-shell-def-exec jao-bright-set-up "brightnessctl" "-q" "s" "5%+") + (jao-shell-def-exec jao-bright-set-down "brightnessctl" "-q" "s" "5%-") (defun jao-bright-show () (interactive) - (message "%s" (thread-first (jao-exec-string "brightnessctl") + (message "%s" (thread-first (jao-shell-string "brightnessctl") (split-string "\n") (cadr) (string-trim)))) (defun jao-bright-up () (interactive) - (jao-exec-string "brightnessctl -q s 5%%+") + (jao-shell-string "brightnessctl -q s 5%%+") (jao-bright-show)) (defun jao-bright-down () (interactive) - (jao-exec-string "brightnessctl -q s 5%%-") + (jao-shell-string "brightnessctl -q s 5%%-") (jao-bright-show)) #+end_src *** Keyboard @@ -251,7 +234,7 @@ suffixes))) #+end_src -*** Dirs +*** Disk #+begin_src emacs-lisp (use-package jao-dirmon) #+end_src @@ -530,14 +513,14 @@ (make-symbolic-link f "~/.wallpaper.sway" t)) (defun jao-sway-run-or-focus (cmd &optional ws) - (if (jao-exec-running-p "firefox") + (if (jao-shell-running-p "firefox") (jao-swaymsg (format "[app_id=%s] focus" cmd)) (jao-swaymsg (format "workspace %s" (or ws 2))) (start-process-shell-command cmd nil cmd))) (defun jao-sway-run-or-focus-deezer () (interactive) - (if (jao-exec-running-p "deezer-desktop") + (if (jao-shell-running-p "deezer-desktop") (jao-swaymsg "[app_id=Deezer] scratchpad show") (start-process-shell-command "deezer" nil "deezer &") (jao-sway-run-or-focus-deezer))) @@ -604,22 +587,22 @@ *** screensaver and lock #+begin_src emacs-lisp (defun jao-screensaver-enabled () - (string= (jao-exec-string "xdg-screensaver status") "enabled")) + (string= (jao-shell-string "xdg-screensaver status") "enabled")) (defun jao-screensaver-toggle () (interactive) - (let ((wid (jao-exec-string "xdotool getwindowfocus"))) + (let ((wid (jao-shell-string "xdotool getwindowfocus"))) (if (jao-screensaver-enabled) - (jao-exec-string "xdg-screensaver suspend %s" wid) - (jao-exec-string "xdg-screensaver resume %s" wid)) + (jao-shell-string "xdg-screensaver suspend" wid) + (jao-shell-string "xdg-screensaver resume" wid)) (jao-notify (format "Using '%s'" - (jao-exec-string "xdotool getwindowname %s" wid)) + (jao-shell-string "xdotool getwindownames" wid)) (format "Screensaver %s" - (jao-exec-string "xdg-screensaver status"))))) + (jao-shell-string "xdg-screensaver status"))))) - (jao-def-exec jao-xlock-screen "xdg-screensaver" "activate") - (jao-def-exec jao-suspend "sudo" "systemctl" "suspend") - (jao-def-exec jao-poweroff "sudo" "systemctl" "poweroff") + (jao-shell-def-exec jao-xlock-screen "xdg-screensaver" "activate") + (jao-shell-def-exec jao-suspend "sudo" "systemctl" "suspend") + (jao-shell-def-exec jao-poweroff "sudo" "systemctl" "poweroff") (defun jao-lock-screen () (interactive) @@ -887,8 +870,8 @@ (interactive "P") (if (not wide) (message "%s" - (jao-exec-string "curl -s 'https://wttr.in/?format=%s'" - "%l++%m++%C+%c+%t+%w++%p")) + (jao-shell-string "curl -s" + "https://wttr.in/?format=%l++%m++%C+%c+%t+%w++%p")) (jao-afio--goto-scratch) (if-let ((b (get-buffer "*wttr*"))) (progn (pop-to-buffer b) @@ -2700,13 +2683,13 @@ * Network *** nm applet #+begin_src emacs-lisp - (jao-def-exec jao-nm-applet "nm-applet") + (jao-shell-def-exec jao-nm-applet "nm-applet") (defun jao-toggle-nm-applet () (interactive) - (if (string-empty-p (jao-exec-string "pidof nm-applet")) - (jao-nm-applet) - (jao-exec-string "killall nm-applet"))) + (if (jao-shell-running-p "nm-applet") + (jao-shell-string "killall nm-applet") + (jao-nm-applet))) #+end_src *** enwc #+begin_src emacs-lisp @@ -2953,7 +2936,7 @@ (string-to-number level))))) (defun jao-mixer-set (dev v) - (jao-exec-string "amixer sset %s %s" dev v) + (jao-shell-string "amixer sset" dev v) (jao-mixer-get-level dev)) (defun jao-mixer-master-toggle () @@ -2976,13 +2959,13 @@ (interactive) (jao-mixer-set "Capture" "10%-")) - (jao-def-exec jao-audio-applet "pasystray") + (jao-shell-def-exec jao-audio-applet "pasystray") (defun jao-toggle-audio-applet () (interactive) - (if (string-empty-p (jao-exec-string "pidof pasystray")) + (if (string-empty-p (jao-shell-string "pidof pasystray")) (jao-audio-applet) - (jao-exec-string "killall pasystray"))) + (jao-shell-string "killall pasystray"))) (global-set-key (kbd "") #'jao-toggle-audio-applet) diff --git a/lib/eos/jao-shell.el b/lib/eos/jao-shell.el index f3ce438..ff1c160 100644 --- a/lib/eos/jao-shell.el +++ b/lib/eos/jao-shell.el @@ -24,14 +24,37 @@ ;;; 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 #'shell-quote-argument args " ")))) + (let ((cmd (concat cmd " " (mapconcat #'jao-shell--quote args " ")))) (split-string (shell-command-to-string cmd) "\n" t))) - -(defun jao-shell-cmd-line (cmd &rest args) - (car (apply #'jao-shell-cmd-lines cmd args))) +;;;###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) + (interactive + (list (read-shell-command "$ " + (if current-prefix-arg + (cons (concat " " (buffer-file-name)) 0) + "")))) + (start-process-shell-command command nil command)) + +(defmacro jao-shell-def-exec (name &rest args) + `(defun ,name (&rest other-args) + (interactive) + (start-process-shell-command + ,(car args) + "*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))))) (provide 'jao-shell) ;;; jao-shell.el ends here -- cgit v1.2.3