f592e6093609 default tip

Merge.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 13 Apr 2020 21:29:44 -0400
parents 1d2776f7fa4a (current diff) da80130c3bbf (diff)
children (none)
branches/tags default tip
files Makefile brows.asd src/build.lisp src/main.lisp

Changes

--- a/brows.asd	Mon Apr 13 21:23:33 2020 -0400
+++ b/brows.asd	Mon Apr 13 21:29:44 2020 -0400
@@ -8,6 +8,7 @@
 
   :depends-on (
 
+               :adopt
                :boots
                :cl-ppcre
                :external-program
--- a/src/build.lisp	Mon Apr 13 21:23:33 2020 -0400
+++ b/src/build.lisp	Mon Apr 13 21:29:44 2020 -0400
@@ -1,4 +1,7 @@
-(ql:quickload :brows)
+(ql:quickload :brows :silent t)
+
+(with-open-file (stream "bin/brows.1" :direction :output :if-exists :supersede)
+  (adopt:print-manual brows::*ui* :stream stream))
 
 #+sbcl
 (progn
--- a/src/main.lisp	Mon Apr 13 21:23:33 2020 -0400
+++ b/src/main.lisp	Mon Apr 13 21:29:44 2020 -0400
@@ -108,17 +108,42 @@
         ((#\j :down) (incf-pos 1))
         (t nil)))))
 
-(defmacro catch-and-spew-errors (&body body)
-  `(handler-case (progn ,@body)
-     (t (c) (format t "Error: ~A" c))))
+(defun run ()
+  (catch 'done
+    (with-open-file (input "/dev/tty" :direction :input)
+      (with-open-file (output "/dev/tty" :direction :output :if-exists :append)
+        (boots/terminals/ansi:with-ansi-terminal (terminal :input-stream input :output-stream output)
+          (boots:with-screen (screen terminal :root (boots:make-canvas :draw #'draw))
+            (init)
+            (main)))))))
+
+
+;;;; CLI ----------------------------------------------------------------------
+(adopt:define-string *documentation*
+  "Brows is a utility for finding links in a chunk of text and presenting a ~
+  nice text-based UI for opening them.  It's written (and customizable) in ~
+  Common Lisp.")
+
+(defparameter *ui*
+  (adopt:make-interface
+    :name "brows"
+    :usage "[OPTIONS]"
+    :summary "Find links and present a menu for opening them in a browser."
+    :help *documentation*
+    :examples '(("Present a menu for opening some links:" .
+                 "curl http://stevelosh.com/ | brows"))
+    :contents (list (adopt:make-option 'help
+                      :help "Display help and exit."
+                      :long "help"
+                      :short #\h
+                      :reduce (constantly t)))))
 
 (defun toplevel ()
-  (catch-and-spew-errors
-    (catch 'done
-      (with-open-file (input "/dev/tty" :direction :input)
-        (with-open-file (output "/dev/tty" :direction :output :if-exists :append)
-          (boots/terminals/ansi:with-ansi-terminal (terminal :input-stream input :output-stream output)
-            (boots:with-screen (screen terminal :root (boots:make-canvas :draw #'draw))
-              (init)
-              (main))))))))
-
+  (handler-case
+      (multiple-value-bind (arguments options) (adopt:parse-options *ui*)
+        (when (gethash 'help options)
+          (adopt:print-help-and-exit *ui*))
+        (when arguments
+          (error "Unrecognized command line arguments: ~S" arguments))
+        (run))
+    (error (c) (adopt:print-error-and-exit c))))