]> gitweb.factorcode.org Git - factor.git/commitdiff
tools.grep: adding "grep" tool.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 1 Apr 2015 21:41:41 +0000 (14:41 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 1 Apr 2015 21:41:41 +0000 (14:41 -0700)
extra/tools/grep/deploy.factor [new file with mode: 0644]
extra/tools/grep/grep.factor [new file with mode: 0644]

diff --git a/extra/tools/grep/deploy.factor b/extra/tools/grep/deploy.factor
new file mode 100644 (file)
index 0000000..f9f04c0
--- /dev/null
@@ -0,0 +1,15 @@
+USING: tools.deploy.config ;
+H{
+    { deploy-name "grep" }
+    { deploy-ui? f }
+    { deploy-c-types? t }
+    { deploy-console? t }
+    { deploy-unicode? f }
+    { "stop-after-last-window?" t }
+    { deploy-io 3 }
+    { deploy-reflection 6 }
+    { deploy-word-props? t }
+    { deploy-math? t }
+    { deploy-threads? t }
+    { deploy-word-defs? t }
+}
diff --git a/extra/tools/grep/grep.factor b/extra/tools/grep/grep.factor
new file mode 100644 (file)
index 0000000..e600e0a
--- /dev/null
@@ -0,0 +1,31 @@
+
+USING: kernel fry io io.files io.encodings.ascii sequences
+regexp command-line namespaces ;
+
+IN: tools.grep
+
+! TODO: getopt
+! TODO: color
+! TODO: case-insensitive
+
+: grep-lines ( regexpt -- )
+    '[ dup _ matches? [ print ] [ drop ] if ] each-line ;
+
+: grep-file ( pattern filename -- )
+    ascii [ grep-lines ] with-file-reader ;
+
+: grep-usage ( -- )
+    "Usage: factor grep.factor <pattern> [<file>...]" print ;
+
+: run-grep ( -- )
+    command-line get [
+        grep-usage
+    ] [
+        unclip ".*" dup surround <regexp> swap [
+            grep-lines
+        ] [
+            [ grep-file ] with each
+        ] if-empty
+    ] if-empty ;
+
+MAIN: run-grep