Skip to content

Commit 3730f4e

Browse files
committed
adds line filter support for other applications to pipe strings to the uni executable (#1)
1 parent eed31bd commit 3730f4e

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

uni.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ import (
77
"os"
88
"strings"
99
"unicode/utf8"
10+
"bytes"
11+
"io"
1012
)
1113

1214
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]\nLine Filter Usage: [application] | uni\n"
1517
help = "=================================================\n" +
1618
" uni v" + version + "\n" +
1719
" Copyright 2017 Christopher Simpkins\n" +
1820
" MIT License\n\n" +
1921
" Source: https://github.com/source-foundry/uni\n" +
2022
"=================================================\n\n" +
2123
" 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" +
2328
" Options:\n" +
2429
" -h, --help Application help\n" +
2530
" --usage Application usage\n" +
@@ -28,13 +33,6 @@ const (
2833

2934
func main() {
3035

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-
3836
// define available command line flags
3937
var versionShort = flag.Bool("v", false, "Application version")
4038
var versionLong = flag.Bool("version", false, "Application version")
@@ -56,10 +54,43 @@ func main() {
5654
os.Exit(0)
5755
}
5856

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+
6291
}
92+
93+
6394
}
6495

6596
// 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 {
80111
}
81112
return solist
82113
}
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

Comments
 (0)