]> gitweb.factorcode.org Git - factor.git/blob - extra/tools/grep/grep.factor
sequences: Add 1surround which is dup surround
[factor.git] / extra / tools / grep / grep.factor
1
2 USING: kernel fry io io.files io.encodings.ascii sequences
3 regexp command-line namespaces ;
4
5 IN: tools.grep
6
7 ! TODO: getopt
8 ! TODO: color
9 ! TODO: case-insensitive
10
11 : grep-lines ( regexpt -- )
12     '[ dup _ matches? [ print ] [ drop ] if ] each-line ;
13
14 : grep-file ( pattern filename -- )
15     ascii [ grep-lines ] with-file-reader ;
16
17 : grep-usage ( -- )
18     "Usage: factor grep.factor <pattern> [<file>...]" print ;
19
20 : run-grep ( -- )
21     command-line get [
22         grep-usage
23     ] [
24         unclip ".*" 1surround <regexp> swap [
25             grep-lines
26         ] [
27             [ grep-file ] with each
28         ] if-empty
29     ] if-empty ;
30
31 MAIN: run-grep