@@ -7,19 +7,24 @@ import (
7
7
"os"
8
8
"strings"
9
9
"unicode/utf8"
10
+ "bytes"
11
+ "io"
10
12
)
11
13
12
14
const (
13
- version = "0.9 .0"
14
- usage = "Usage: uni [glyph 1]...[glyph n]\n "
15
+ version = "0.10 .0"
16
+ usage = "Usage: uni [glyph 1]...[glyph n]\n Line Filter Usage: [application] | uni \ n "
15
17
help = "=================================================\n " +
16
18
" uni v" + version + "\n " +
17
19
" Copyright 2017 Christopher Simpkins\n " +
18
20
" MIT License\n \n " +
19
21
" Source: https://github.com/source-foundry/uni\n " +
20
22
"=================================================\n \n " +
21
23
" Usage:\n " +
22
- " $ uni [glyph 1]...[glyph n]\n \n " +
24
+ " - With command line arguments:\n " +
25
+ " $ uni [glyph 1]...[glyph n]\n " +
26
+ " - As line filter:\n " +
27
+ " $ [application] | uni\n \n " +
23
28
" Options:\n " +
24
29
" -h, --help Application help\n " +
25
30
" --usage Application usage\n " +
@@ -28,13 +33,6 @@ const (
28
33
29
34
func main () {
30
35
31
- // test for at least one argument on command line
32
- if len (os .Args ) < 2 {
33
- os .Stderr .WriteString ("[Error] Please include at least one argument for your Unicode code point search\n " )
34
- os .Stderr .WriteString (usage )
35
- os .Exit (1 )
36
- }
37
-
38
36
// define available command line flags
39
37
var versionShort = flag .Bool ("v" , false , "Application version" )
40
38
var versionLong = flag .Bool ("version" , false , "Application version" )
@@ -56,10 +54,43 @@ func main() {
56
54
os .Exit (0 )
57
55
}
58
56
59
- stdOutput := unicodeSearch (os .Args [1 :])
60
- for _ , line := range stdOutput {
61
- fmt .Print (line )
57
+ // if there are no arguments to the executable, check std input stream to see if
58
+ // this is a line filter request that is piped to executable
59
+ if len (os .Args ) < 2 {
60
+ stdin := os .Stdin
61
+ f , err := stdin .Stat ()
62
+ if err != nil {
63
+ handleStdInErrors ()
64
+ }
65
+
66
+ size := f .Size ()
67
+ if size == 0 {
68
+ handleStdInErrors ()
69
+ }
70
+
71
+ tmp := new (bytes.Buffer )
72
+ if _ , err := io .Copy (tmp , stdin ); err != nil {
73
+ os .Stderr .WriteString ("[Error] Failed to copy std input stream to memory. " + fmt .Sprintf ("%v" , err ))
74
+ os .Exit (1 )
75
+ }
76
+
77
+ stdinList := strings .Split (tmp .String (), "" )
78
+ stdOutput := unicodeSearch (stdinList )
79
+ for _ , line := range stdOutput {
80
+ fmt .Print (line )
81
+ }
82
+
83
+ } else {
84
+
85
+ // handle command line arguments to the executable
86
+ stdOutput := unicodeSearch (os .Args [1 :])
87
+ for _ , line := range stdOutput {
88
+ fmt .Print (line )
89
+ }
90
+
62
91
}
92
+
93
+
63
94
}
64
95
65
96
// writes Unicode code point value(s) to standard output stream for glyphs entered as command line arguments
@@ -80,3 +111,9 @@ func unicodeSearch(argv []string) []string {
80
111
}
81
112
return solist
82
113
}
114
+
115
+ func handleStdInErrors () {
116
+ os .Stderr .WriteString ("[Error] Please include at least one argument or pipe a string to the executable through the stdin stream.\n " )
117
+ os .Stderr .WriteString (usage )
118
+ os .Exit (1 )
119
+ }
0 commit comments