Skip to content

Commit

Permalink
adds line filter support for other applications to pipe strings to th…
Browse files Browse the repository at this point in the history
…e uni executable (#1)
  • Loading branch information
chrissimpkins committed Nov 13, 2017
1 parent eed31bd commit 3730f4e
Showing 1 changed file with 50 additions and 13 deletions.
63 changes: 50 additions & 13 deletions uni.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ import (
"os"
"strings"
"unicode/utf8"
"bytes"
"io"
)

const (
version = "0.9.0"
usage = "Usage: uni [glyph 1]...[glyph n]\n"
version = "0.10.0"
usage = "Usage: uni [glyph 1]...[glyph n]\nLine Filter Usage: [application] | uni\n"
help = "=================================================\n" +
" uni v" + version + "\n" +
" Copyright 2017 Christopher Simpkins\n" +
" MIT License\n\n" +
" Source: https://github.com/source-foundry/uni\n" +
"=================================================\n\n" +
" Usage:\n" +
" $ uni [glyph 1]...[glyph n]\n\n" +
" - With command line arguments:\n" +
" $ uni [glyph 1]...[glyph n]\n" +
" - As line filter:\n" +
" $ [application] | uni\n\n" +
" Options:\n" +
" -h, --help Application help\n" +
" --usage Application usage\n" +
Expand All @@ -28,13 +33,6 @@ const (

func main() {

// test for at least one argument on command line
if len(os.Args) < 2 {
os.Stderr.WriteString("[Error] Please include at least one argument for your Unicode code point search\n")
os.Stderr.WriteString(usage)
os.Exit(1)
}

// define available command line flags
var versionShort = flag.Bool("v", false, "Application version")
var versionLong = flag.Bool("version", false, "Application version")
Expand All @@ -56,10 +54,43 @@ func main() {
os.Exit(0)
}

stdOutput := unicodeSearch(os.Args[1:])
for _, line := range stdOutput {
fmt.Print(line)
// if there are no arguments to the executable, check std input stream to see if
// this is a line filter request that is piped to executable
if len(os.Args) < 2 {
stdin := os.Stdin
f, err := stdin.Stat()
if err != nil {
handleStdInErrors()
}

size := f.Size()
if size == 0 {
handleStdInErrors()
}

tmp := new(bytes.Buffer)
if _, err := io.Copy(tmp, stdin); err != nil {
os.Stderr.WriteString("[Error] Failed to copy std input stream to memory. " + fmt.Sprintf("%v", err))
os.Exit(1)
}

stdinList := strings.Split(tmp.String(), "")
stdOutput := unicodeSearch(stdinList)
for _, line := range stdOutput {
fmt.Print(line)
}

} else {

// handle command line arguments to the executable
stdOutput := unicodeSearch(os.Args[1:])
for _, line := range stdOutput {
fmt.Print(line)
}

}


}

// writes Unicode code point value(s) to standard output stream for glyphs entered as command line arguments
Expand All @@ -80,3 +111,9 @@ func unicodeSearch(argv []string) []string {
}
return solist
}

func handleStdInErrors() {
os.Stderr.WriteString("[Error] Please include at least one argument or pipe a string to the executable through the stdin stream.\n")
os.Stderr.WriteString(usage)
os.Exit(1)
}

0 comments on commit 3730f4e

Please sign in to comment.