From ef3741726fcda3962e29110d49b4105a2ac40e50 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Thu, 17 Feb 2022 16:02:25 -0600 Subject: Beginning work on the CLON based CLI parsing --- build-app.lisp | 12 ++++++ clpmfile | 6 +++ clpmfile.lock | 39 ++++++++++++++++-- oneliners.cli.asd | 4 +- src/lib.lisp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.lisp | 117 ------------------------------------------------------ 6 files changed, 173 insertions(+), 122 deletions(-) create mode 100644 build-app.lisp create mode 100644 src/lib.lisp delete mode 100644 src/main.lisp diff --git a/build-app.lisp b/build-app.lisp new file mode 100644 index 0000000..c5a6f25 --- /dev/null +++ b/build-app.lisp @@ -0,0 +1,12 @@ +(asdf:load-system "oneliners.cli") + +(defpackage #:oneliners.cli.app + (:use #:cl #:net.didierverna.clon)) + +(in-package :oneliners.cli.app) + +(defsynopsis (:postfix "ARGS ...") + (text :contents "This is some arbitrary text.") + (group (:header "Group 1"))) + + diff --git a/clpmfile b/clpmfile index f770b09..82a64a2 100644 --- a/clpmfile +++ b/clpmfile @@ -2,6 +2,12 @@ (:api-version "0.4") (:source "quicklisp" :url "https://beta.quicklisp.org/dist/quicklisp.txt" :type :quicklisp) +(:source "ultralisp" :url "https://clpi.ultralisp.org/" :type :clpi) (:asd "oneliners.cli.asd") (:asd "oneliners.api-client.asd") + +(:system "net.didierverna.clon" :source "ultralisp") +(:system "net.didierverna.clon.core" :source "ultralisp") +(:system "net.didierverna.clon.termio" :source "ultralisp") + diff --git a/clpmfile.lock b/clpmfile.lock index 0521d30..53f4c43 100644 --- a/clpmfile.lock +++ b/clpmfile.lock @@ -13,6 +13,7 @@ (:implicit-file :type :file-system :system-files ("oneliners.cli.asd")) ("quicklisp" :url "https://beta.quicklisp.org/dist/quicklisp.txt" :type :quicklisp) +("ultralisp" :url "https://clpi.ultralisp.org/" :type :clpi) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -20,6 +21,9 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; :requirements +(:system :name "net.didierverna.clon.termio" :source "ultralisp") +(:system :name "net.didierverna.clon.core" :source "ultralisp") +(:system :name "net.didierverna.clon" :source "ultralisp") (:asd-file :name "oneliners.api-client.asd") (:asd-file :name "oneliners.cli.asd") @@ -39,16 +43,23 @@ ("chunga" :version "2020-04-27" :source "quicklisp" :systems ("chunga")) ("cl+ssl" :version "2021-12-30" :source "quicklisp" :systems ("cl+ssl")) ("cl-base64" :version "2020-10-16" :source "quicklisp" :systems ("cl-base64")) +("cl-clon" :version "2021-04-11" :source "quicklisp" :systems + ("net.didierverna.clon" "net.didierverna.clon.core" + "net.didierverna.clon.setup" "net.didierverna.clon.setup/termio")) ("cl-cookie" :version "2019-10-07" :source "quicklisp" :systems ("cl-cookie")) ("cl-ppcre" :version "2019-05-21" :source "quicklisp" :systems ("cl-ppcre")) ("cl-utilities" :version "2010-10-07" :source "quicklisp" :systems ("cl-utilities")) ("dexador" :version "2021-12-09" :source "quicklisp" :systems ("dexador")) +("didierverna/clon" :version "20210424171500" :source "ultralisp" :systems + ("net.didierverna.clon.termio")) ("fast-http" :version "2019-10-07" :source "quicklisp" :systems ("fast-http")) ("fast-io" :version "2020-09-25" :source "quicklisp" :systems ("fast-io")) ("flexi-streams" :version "2021-08-07" :source "quicklisp" :systems ("flexi-streams")) ("local-time" :version "2021-01-24" :source "quicklisp" :systems ("local-time")) +("named-readtables" :version "2021-12-09" :source "quicklisp" :systems + ("named-readtables")) ("oneliners.api-client.asd" :version :newest :source :implicit-file :systems ("oneliners.api-client")) ("oneliners.cli.asd" :version :newest :source :implicit-file :systems @@ -72,7 +83,6 @@ ("trivial-gray-streams")) ("trivial-mimes" :version "2020-07-15" :source "quicklisp" :systems ("trivial-mimes")) -("unix-opts" :version "2021-01-24" :source "quicklisp" :systems ("unix-opts")) ("usocket" :version "2019-12-27" :source "quicklisp" :systems ("usocket")) ("xsubseq" :version "2017-08-30" :source "quicklisp" :systems ("xsubseq")) @@ -110,6 +120,7 @@ ((:system :name "static-vectors") (:system :name "cffi-grovel")) ((:system :name "osicat") (:system :name "cffi")) ((:system :name "osicat") (:system :name "cffi-grovel")) + ((:system :name "net.didierverna.clon.termio") (:system :name "cffi")) ((:system :name "cl+ssl") (:system :name "cffi")) ((:system :name "cffi-toolchain") (:system :name "cffi")) ((:system :name "cffi-grovel") (:system :name "cffi")) @@ -123,6 +134,23 @@ ("cl-base64" ((:system :name "dexador") (:system :name "cl-base64"))) +("cl-clon" + ((:system :name "oneliners.cli") (:system :name "net.didierverna.clon")) + ((:system :name "net.didierverna.clon.termio") + (:system :name "net.didierverna.clon.core")) + ((:system :name "net.didierverna.clon.termio") + (:system :name "net.didierverna.clon.setup/termio")) + ((:system :name "net.didierverna.clon.setup/termio") + (:system :name "net.didierverna.clon.setup")) + ((:system :name "net.didierverna.clon.core") + (:system :name "net.didierverna.clon.setup")) + ((:system :name "net.didierverna.clon") + (:system :name "net.didierverna.clon.core")) + ((:system :name "net.didierverna.clon") + (:system :name "net.didierverna.clon.setup")) + (t (:system :name "net.didierverna.clon.core" :source "ultralisp")) + (t (:system :name "net.didierverna.clon" :source "ultralisp"))) + ("cl-cookie" ((:system :name "dexador") (:system :name "cl-cookie"))) ("cl-ppcre" ((:system :name "dexador") (:system :name "cl-ppcre")) @@ -134,6 +162,9 @@ ("dexador" ((:system :name "oneliners.cli") (:system :name "dexador")) ((:system :name "oneliners.api-client") (:system :name "dexador"))) +("didierverna/clon" + (t (:system :name "net.didierverna.clon.termio" :source "ultralisp"))) + ("fast-http" ((:system :name "dexador") (:system :name "fast-http"))) ("fast-io" ((:system :name "dexador") (:system :name "fast-io"))) @@ -144,6 +175,10 @@ ("local-time" ((:system :name "cl-cookie") (:system :name "local-time"))) +("named-readtables" + ((:system :name "net.didierverna.clon.setup") + (:system :name "named-readtables"))) + ("oneliners.api-client.asd" ((:system :name "oneliners.cli") (:system :name "oneliners.api-client")) (t (:asd-file :name "oneliners.api-client.asd"))) @@ -186,8 +221,6 @@ ("trivial-mimes" ((:system :name "dexador") (:system :name "trivial-mimes"))) -("unix-opts" ((:system :name "oneliners.cli") (:system :name "unix-opts"))) - ("usocket" ((:system :name "dexador") (:system :name "usocket")) ((:system :name "cl+ssl") (:system :name "usocket"))) diff --git a/oneliners.cli.asd b/oneliners.cli.asd index b072e32..10e8f39 100644 --- a/oneliners.cli.asd +++ b/oneliners.cli.asd @@ -5,11 +5,11 @@ :depends-on ("trivial-clipboard" "dexador" "osicat" - "unix-opts" + "net.didierverna.clon" "oneliners.api-client") :components ((:module "src" :components - ((:file "main")))) + ((:file "lib")))) :description "" :in-order-to ((test-op (test-op "oneliners.cli/tests")))) diff --git a/src/lib.lisp b/src/lib.lisp new file mode 100644 index 0000000..20f75ea --- /dev/null +++ b/src/lib.lisp @@ -0,0 +1,117 @@ +;;;; main.lisp -- oneliners.cli entrypoint + +(defpackage oneliners.cli + (:use :cl) + (:local-nicknames (#:api #:oneliners.api-client))) +(in-package :oneliners.cli) + +;;; CLI OPTIONS + +;; (opts:define-opts +;; (:name :add +;; :description "Intaractively add a oneliner to the a wiki." +;; :long "add") +;; (:name :tags +;; :description "A comma separated list of tags to filter search results." +;; :short #\t +;; :long "tags" +;; :arg-parser #'identity +;; :meta-var "'T1, T2, ...'") +;; (:name :limit +;; :description "An integer. The maximum number of results to return." +;; :short #\l +;; :long "limit" +;; :meta-var "N" +;; :default 10 +;; :arg-parser #'parse-integer) +;; (:name :edit +;; :description "An integer, a result number. Interactively edit af command." +;; :long "edit" +;; :meta-var "RESULT" +;; :arg-parser #'parse-integer) +;; (:name :not-flagged +;; :description "Filter flagged oneliners from the search results" +;; :long "not-flagged")) + +(defparameter +help-suffix+ + "Unless RESULT is an integer, search for oneliners that involve each command in COMMANDS. +E.g. + # search for oneliners involving both 'find' and 'grep' + ol find grep + +Otherwise, when RESULT is an integer, run the oneliner with index RESULT from the most +recent search command, and supply ARGS to that oneliner, if provided. +E.g. + # run the third result from the last search with arguments foo and bar + ol 3 foo bar") + +;; (defun help-text () +;; (opts:describe :prefix "Oneliners Wiki Command Line Tool" +;; :args "[[RESULT ARGS | COMMANDS]" +;; :usage-of "ol" +;; :suffix +help-suffix+)) + +;;; CONFIG AND RESULTS FILE LOCATIONS + +(defun config-file () + (merge-pathnames ".config/oneliners.config" (user-homedir-pathname) )) + +(defun last-search-file () + (merge-pathnames ".last_oneliners_search" (user-homedir-pathname))) + +;;; RUNNING COMMANDS + +(defun parent-process-name () + "Prints the name of the parent process of the current process." + (let ((ppidfile (format nil "/proc/~a/status" (osicat-posix:getppid)))) + (first (last + (ppcre:split "\\s" + (with-open-file (input ppidfile) + (read-line input))))))) + +(defmacro wait-until ((&key (timeout 1) (poll-every 0.01)) &body check) + "Run CHECK every POLL-EVERY seconds until either TIMEOUT seconds +have passed or CHECK returns non-nil." + (let ((clockvar (gensym)) + (var (gensym))) + `(loop + for ,clockvar from 0 by ,poll-every to ,timeout + for ,var = (progn ,@check) + when ,var + return ,var + do (sleep ,poll-every)))) + +(defun run-with-shell + (command + &key + (shell-name (parent-process-name)) + (await-output-p 0.8) + (output-stream *standard-output*)) + "run COMMAND, a string, in a fresh shell environment, initialized +with SHELL-NAME. The output from the command read line by line and is +printed to OUTPUT-STREAM. " + (let ((shell + (uiop:launch-program shell-name :input :stream :output :stream))) + (symbol-macrolet ((shell-input (uiop:process-info-input shell)) + (shell-output (uiop:process-info-output shell))) + (write-line command shell-input) + (finish-output shell-input) + (when await-output-p + (wait-until (:timeout await-output-p :poll-every 0.005) + (listen shell-output)) + (loop while (listen shell-output) + do (princ (read-line shell-output) output-stream) + (terpri output-stream)))))) + +;;; main + +;; (defun main () +;; (handler-case +;; (multiple-value-bind (options free-args) (opts:get-opts) +;; (print (list :options options :free-args free-args)) +;; (terpri) +;; (uiop:quit)) +;; (unix-opts:unknown-option (err) +;; (declare (ignore err)) +;; (princ (help-text)) +;; (terpri)))) diff --git a/src/main.lisp b/src/main.lisp deleted file mode 100644 index 0b33a6e..0000000 --- a/src/main.lisp +++ /dev/null @@ -1,117 +0,0 @@ -;;;; main.lisp -- oneliners.cli entrypoint - -(defpackage oneliners.cli - (:use :cl) - (:local-nicknames (#:api #:oneliners.api-client))) -(in-package :oneliners.cli) - -;;; CLI OPTIONS - -(opts:define-opts - (:name :add - :description "Intaractively add a oneliner to the a wiki." - :long "add") - (:name :tags - :description "A comma separated list of tags to filter search results." - :short #\t - :long "tags" - :arg-parser #'identity - :meta-var "'T1, T2, ...'") - (:name :limit - :description "An integer. The maximum number of results to return." - :short #\l - :long "limit" - :meta-var "N" - :default 10 - :arg-parser #'parse-integer) - (:name :edit - :description "An integer, a result number. Interactively edit af command." - :long "edit" - :meta-var "RESULT" - :arg-parser #'parse-integer) - (:name :not-flagged - :description "Filter flagged oneliners from the search results" - :long "not-flagged")) - -(defparameter +help-suffix+ - "Unless RESULT is an integer, search for oneliners that involve each command in COMMANDS. -E.g. - # search for oneliners involving both 'find' and 'grep' - ol find grep - -Otherwise, when RESULT is an integer, run the oneliner with index RESULT from the most -recent search command, and supply ARGS to that oneliner, if provided. -E.g. - # run the third result from the last search with arguments foo and bar - ol 3 foo bar") - -(defun help-text () - (opts:describe :prefix "Oneliners Wiki Command Line Tool" - :args "[[RESULT ARGS | COMMANDS]" - :usage-of "ol" - :suffix +help-suffix+)) - -;;; CONFIG AND RESULTS FILE LOCATIONS - -(defun config-file () - (merge-pathnames ".config/oneliners.config" (user-homedir-pathname) )) - -(defun last-search-file () - (merge-pathnames ".last_oneliners_search" (user-homedir-pathname))) - -;;; RUNNING COMMANDS - -(defun parent-process-name () - "Prints the name of the parent process of the current process." - (let ((ppidfile (format nil "/proc/~a/status" (osicat-posix:getppid)))) - (first (last - (ppcre:split "\\s" - (with-open-file (input ppidfile) - (read-line input))))))) - -(defmacro wait-until ((&key (timeout 1) (poll-every 0.01)) &body check) - "Run CHECK every POLL-EVERY seconds until either TIMEOUT seconds -have passed or CHECK returns non-nil." - (let ((clockvar (gensym)) - (var (gensym))) - `(loop - for ,clockvar from 0 by ,poll-every to ,timeout - for ,var = (progn ,@check) - when ,var - return ,var - do (sleep ,poll-every)))) - -(defun run-with-shell - (command - &key - (shell-name (parent-process-name)) - (await-output-p 0.8) - (output-stream *standard-output*)) - "run COMMAND, a string, in a fresh shell environment, initialized -with SHELL-NAME. The output from the command read line by line and is -printed to OUTPUT-STREAM. " - (let ((shell - (uiop:launch-program shell-name :input :stream :output :stream))) - (symbol-macrolet ((shell-input (uiop:process-info-input shell)) - (shell-output (uiop:process-info-output shell))) - (write-line command shell-input) - (finish-output shell-input) - (when await-output-p - (wait-until (:timeout await-output-p :poll-every 0.005) - (listen shell-output)) - (loop while (listen shell-output) - do (princ (read-line shell-output) output-stream) - (terpri output-stream)))))) - -;;; main - -(defun main () - (handler-case - (multiple-value-bind (options free-args) (opts:get-opts) - (print (list :options options :free-args free-args)) - (terpri) - (uiop:quit)) - (unix-opts:unknown-option (err) - (declare (ignore err)) - (princ (help-text)) - (terpri)))) -- cgit v1.2.3