From: John Benediktsson Date: Mon, 28 Mar 2022 00:35:25 +0000 (-0700) Subject: tools.wc: remove dependency on formatting, handle not found X-Git-Tag: 0.99~1464 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=5219bcf400922c32f56dc0bd1219bcbc42459496 tools.wc: remove dependency on formatting, handle not found --- diff --git a/extra/tools/wc/wc.factor b/extra/tools/wc/wc.factor index 94d8d3f1df..52f6403fa1 100644 --- a/extra/tools/wc/wc.factor +++ b/extra/tools/wc/wc.factor @@ -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