]> gitweb.factorcode.org Git - factor.git/commitdiff
tools.wc: remove dependency on formatting, handle not found
authorJohn Benediktsson <mrjbq7@gmail.com>
Mon, 28 Mar 2022 00:35:25 +0000 (17:35 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 28 Mar 2022 00:35:25 +0000 (17:35 -0700)
extra/tools/wc/wc.factor

index 94d8d3f1dfd17984ea7042a97d563ddd55101e7d..52f6403fa1110099c63a2cc9ba42f8e319fc06aa 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2016 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: alien.data command-line formatting io io.encodings
+USING: alien.data command-line io io.encodings
 io.encodings.binary io.files kernel math math.bitwise
-math.vectors math.vectors.simd namespaces sequences
+math.parser math.vectors math.vectors.simd namespaces sequences
 specialized-arrays ;
 
 SPECIALIZED-ARRAY: uchar-16
@@ -30,6 +30,10 @@ IN: tools.wc
     input-stream get binary re-decode
     [ count-lines ] with-input-stream* ;
 
+: print-wc ( n name/f -- )
+    [ number>string 8 CHAR: \s pad-head write ]
+    [ bl [ write ] when* ] bi* nl ;
+
 PRIVATE>
 
 : wc ( path -- n )
@@ -37,10 +41,15 @@ PRIVATE>
 
 : run-wc ( -- )
     command-line get [
-        wc-stdin "%8d\n" printf
+        wc-stdin f print-wc
     ] [
-        [ [ wc ] keep dupd "%8d %s\n" printf ] map
-        dup length 1 > [ sum "%8d total\n" printf ] [ drop ] if
+        [
+            dup file-exists? [
+                [ wc ] keep dupd print-wc
+            ] [
+                write ": not found" print flush f
+            ] if
+        ] map sift dup length 1 > [ sum "total" print-wc ] [ drop ] if
     ] if-empty ;
 
 MAIN: run-wc