a17191abd5e9

Add u, update lines
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 02 Feb 2020 21:05:54 -0500
parents de2be2d2c256
children 8d1c68b94471 90aa89efde4f
branches/tags (none)
files bin/u lisp/lines.lisp remote/bin/lines remote/bin/u

Changes

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/u	Sun Feb 02 21:05:54 2020 -0500
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+du -hd1 "$@"
--- a/lisp/lines.lisp	Sun Feb 02 12:43:12 2020 -0500
+++ b/lisp/lines.lisp	Sun Feb 02 21:05:54 2020 -0500
@@ -13,6 +13,7 @@
 (defparameter *version* "1.0.0")
 (defparameter *index-base* 0)
 (defparameter *include-numbers* nil)
+(defparameter *limit* nil)
 
 
 ;;;; Functionality ------------------------------------------------------------
@@ -168,9 +169,11 @@
 
 (defun run-fast (matcher)
   (loop
+    :with limit = (when *limit* (+ *index-base* *limit*))
     :with include-numbers = *include-numbers*
     :for i :from *index-base*
     :for line = (read-line *standard-input* nil)
+    :while (or (null limit) (< i limit))
     :while line
     :when (zerop (mod i 100000))
     :do (progn #+sbcl (sb-ext:gc))
@@ -247,7 +250,7 @@
 (defparameter *option-one-based*
   (adopt:make-option 'one-based
     :result-key 'index-base
-    :help "Start numbering lines at 1."
+    :help "Start numbering lines at 1 (default)."
     :short #\1
     :long "one"
     :reduce (constantly 1)))
@@ -255,8 +258,7 @@
 (defparameter *option-zero-based*
   (adopt:make-option 'zero-based
     :result-key 'index-base
-    :initial-value 0
-    :help "Start numbering lines at 0 (default)."
+    :help "Start numbering lines at 0."
     :short #\0
     :long "zero"
     :reduce (constantly 0)))
@@ -264,6 +266,7 @@
 (defparameter *option-other-base*
   (adopt:make-option 'other-base
     :result-key 'index-base
+    :initial-value 0
     :help "Start numbering lines at N."
     :parameter "N"
     :short #\s
@@ -279,6 +282,24 @@
     :help-no "Do not add line number to the output (default)."))
 
 
+(defparameter *option-limit*
+  (adopt:make-option 'limit
+    :help "Read at most N lines."
+    :parameter "N"
+    :short #\l
+    :long "limit"
+    :key #'parse-integer
+    :reduce #'adopt:last))
+
+(defparameter *option-no-limit*
+  (adopt:make-option 'no-limit
+    :result-key 'limit
+    :help "Read all lines (default)."
+    :short #\L
+    :long "no-limit"
+    :reduce (constantly nil)))
+
+
 (defparameter *option-input-encoding*
   (adopt:make-option 'input-encoding
     :help "Treat input as being encoded with ENC (default utf-8)."
@@ -355,6 +376,8 @@
     :help *help-text*
     :examples *examples*
     :contents (list
+                *option-limit*
+                *option-no-limit*
                 *option-help*
                 *option-version*
                 *option-debug*
@@ -389,6 +412,7 @@
 (defmacro exit-on-error (&body body)
   `(without-debugger
      (handler-case (progn ,@body)
+       #+sbcl (sb-int:broken-pipe () (adopt:exit))
        (error (c) (adopt:print-error-and-exit c)))))
 
 (defmacro exit-on-error-unless (expr &body body)
@@ -421,6 +445,7 @@
                  (error "Unrecognized command line arguments: ~S" more))
                (let ((*index-base* (gethash 'index-base options))
                      (*include-numbers* (gethash 'include-numbers options))
+                     (*limit* (gethash 'limit options))
                      (input-format (determine-external-format
                                      (gethash 'input-encoding options)
                                      (gethash 'input-replacement options)))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/remote/bin/lines	Sun Feb 02 21:05:54 2020 -0500
@@ -0,0 +1,1 @@
+../../lisp/bin/lines
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/remote/bin/u	Sun Feb 02 21:05:54 2020 -0500
@@ -0,0 +1,1 @@
+../../bin/u
\ No newline at end of file