|
| 1 | +;;; core/command.el --- Run custom command -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;;; Commentary: |
| 4 | +;; |
| 5 | +;; Command use to run custom command |
| 6 | +;; |
| 7 | +;; $ eask command [names..] |
| 8 | +;; |
| 9 | +;; |
| 10 | +;; Positionals: |
| 11 | +;; |
| 12 | +;; [names..] name of the function command |
| 13 | +;; |
| 14 | + |
| 15 | +;;; Code: |
| 16 | + |
| 17 | +(let ((dir (file-name-directory (nth 1 (member "-scriptload" command-line-args))))) |
| 18 | + (load (expand-file-name "_prepare.el" |
| 19 | + (locate-dominating-file dir "_prepare.el")) |
| 20 | + nil t)) |
| 21 | + |
| 22 | +(defun eask--command-desc (name) |
| 23 | + "Return command's description by its command's NAME." |
| 24 | + (car (split-string (documentation name) "\n"))) |
| 25 | + |
| 26 | +(defun eask--print-commands () |
| 27 | + "Print all available commands." |
| 28 | + (eask-msg "available via `eask command`") |
| 29 | + (eask-msg "") |
| 30 | + (let* ((keys (reverse eask-commands)) |
| 31 | + (offset (eask-seq-str-max keys)) |
| 32 | + (fmt (concat " %-" (eask-2str offset) "s %s"))) |
| 33 | + (dolist (key keys) |
| 34 | + (eask-msg fmt key (eask--command-desc key))) |
| 35 | + (eask-msg "") |
| 36 | + (eask-info "(Total of %s available script%s)" (length keys) |
| 37 | + (eask--sinr keys "" "s")))) |
| 38 | + |
| 39 | +(defun eask--execute-command (name) |
| 40 | + "Execute the command by NAME." |
| 41 | + (eask-info "[RUN]: %s" name) |
| 42 | + (funcall (eask-intern name))) |
| 43 | + |
| 44 | +(defun eask--unmatched-commands (commands) |
| 45 | + "Return a list of COMMANDS that cannot be found in `eask-commands'." |
| 46 | + (let (unmatched) |
| 47 | + (dolist (command commands) |
| 48 | + (unless (memq (eask-intern command) eask-commands) |
| 49 | + (push command unmatched))) |
| 50 | + unmatched)) |
| 51 | + |
| 52 | +(eask-start |
| 53 | + (cond ((null eask-commands) |
| 54 | + (eask-info "(No command specified)") |
| 55 | + (eask-help "core/command")) |
| 56 | + ((eask-all-p) |
| 57 | + (dolist (name (reverse eask-commands)) |
| 58 | + (eask--execute-command name))) |
| 59 | + ((when-let ((commands (eask-args))) |
| 60 | + (if-let ((unmatched (eask--unmatched-commands commands))) |
| 61 | + (progn ; if there are unmatched commands, don't even try to execute |
| 62 | + (eask-info "(Missing command%s: `%s`)" |
| 63 | + (eask--sinr unmatched "" "s") |
| 64 | + (mapconcat #'identity unmatched ", ")) |
| 65 | + (eask-msg "") |
| 66 | + (eask--print-commands)) |
| 67 | + (dolist (command commands) |
| 68 | + (eask--execute-command command)) |
| 69 | + t))) |
| 70 | + (t (eask--print-commands)))) |
| 71 | + |
| 72 | +;;; core/command.el ends here |
0 commit comments