Skip to content

Commit

Permalink
Unicode code point to glyph search support added
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimpkins committed Jan 9, 2018
1 parent 8752fe8 commit 3454594
Showing 1 changed file with 62 additions and 21 deletions.
83 changes: 62 additions & 21 deletions uni.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// uni is a command line executable that displays Unicode code points for glyph arguments
// uni is a command line executable that performs Unicode code point to glyph and glyph to Unicode code point searches
package main

import (
Expand All @@ -7,13 +7,14 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"
"unicode/utf8"
)

const (
version = "0.10.1"
usage = "Usage: uni [glyph 1]...[glyph n]\nLine Filter Usage: [application command] | uni\n"
version = "0.11.0"
usage = "Usage: uni (options) [arg 1]...[arg n]\nLine Filter Usage: [application command] | uni (options)\n"
help = "=================================================\n" +
" uni v" + version + "\n" +
" Copyright 2018 Christopher Simpkins\n" +
Expand All @@ -22,16 +23,18 @@ const (
"=================================================\n\n" +
" Usage:\n" +
" - With command line arguments:\n" +
" $ uni [glyph 1]...[glyph n]\n" +
" $ uni (options) [arg 1]...[arg n]\n" +
" - As line filter:\n" +
" $ [application command] | uni\n\n" +
" $ [application command] | uni (options)\n\n" +
" Options:\n" +
" -g, --glyph Search for glyph with Unicode code point\n" +
" -h, --help Application help\n" +
" --usage Application usage\n" +
" -v, --version Application version\n\n"
)

var versionShort, versionLong, helpShort, helpLong, usageLong *bool
var versionShort, versionLong, helpShort, helpLong, usageLong, glyphShort, glyphLong *bool
var isGlyphSearch = false

func init() {
// define available command line flags
Expand All @@ -40,6 +43,8 @@ func init() {
helpShort = flag.Bool("h", false, "Help")
helpLong = flag.Bool("help", false, "Help")
usageLong = flag.Bool("usage", false, "Usage")
glyphShort = flag.Bool("g", false, "Glyph")
glyphLong = flag.Bool("glyph", false, "Glyph")
}

func main() {
Expand All @@ -59,9 +64,15 @@ func main() {
os.Exit(0)
}

// 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 {
// create a flag for Unicode code point to glyph search command line requests
if *glyphShort || *glyphLong {
isGlyphSearch = true
}

// create a flag for stdin stream search based upon number of arguments and use of glyph search flag
isStdinSearch := (!isGlyphSearch && len(os.Args) < 2) || (isGlyphSearch && len(os.Args) < 3)

if isStdinSearch {

if !stdinValidates(os.Stdin) {
handleStdInErrors()
Expand All @@ -73,18 +84,48 @@ func main() {
os.Exit(1)
}

stdinList := strings.Split(tmp.String(), "") // split the stdin string by glyph to a slice
stdOutput := unicodeSearch(stdinList)
for _, line := range stdOutput {
fmt.Print(line)
// stdin stream search for glyph from Unicode code point search request
if isGlyphSearch {
stdinList := strings.Split(tmp.String(), ` `)
for _, arg := range stdinList {
i, err := strconv.ParseInt(arg, 16, 32)
if err != nil {
os.Stderr.WriteString("[Error] Unable to parse the Unicode code point value '" + arg + "'\n")
os.Exit(1)
}
// TODO: add check for printable glyph range integer value
r := rune(i)
stdoutString := "U+" + arg + " '" + string(r) + "'"
fmt.Println(stdoutString)
}

} else { // stdin stream search for Unicode code point from glyph search request
stdinList := strings.Split(tmp.String(), "") // split the stdin string by glyph to a slice
stdOutput := unicodeSearch(stdinList)
for _, line := range stdOutput {
fmt.Println(line)
}
}

} else {

// handle command line arguments to the executable
stdOutput := unicodeSearch(os.Args[1:])
for _, line := range stdOutput {
fmt.Print(line)
// argument search for glyph from Unicode code point search request
if isGlyphSearch {
for _, arg := range os.Args[2:] {
i, err := strconv.ParseInt(arg, 16, 32)
if err != nil {
os.Stderr.WriteString("[Error] Unable to parse the Unicode code point value '" + arg + "'\n")
os.Exit(1)
}
// TODO: add check for printable glyph range integer value
r := rune(i)
stdoutString := "U+" + arg + " '" + string(r) + "'"
fmt.Println(stdoutString)
}
} else { // argument search for Unicode code point from glyph search request
stdOutput := unicodeSearch(os.Args[1:])
for _, line := range stdOutput {
fmt.Println(line)
}
}

}
Expand Down Expand Up @@ -112,19 +153,19 @@ func handleStdInErrors() {
os.Exit(1)
}

// writes Unicode code point value(s) to standard output stream for glyphs entered as command line arguments
// unicodeSearch decodes runs in command line requests to formatted Unicode code point values for stdout stream print
func unicodeSearch(argv []string) []string {
var solist []string
for i := 0; i < len(argv); i++ {
if len(argv[i]) > 1 { // handle single argument that includes multiple glyphs
charList := strings.Split(argv[i], "")
for x := 0; x < len(charList); x++ {
r, _ := utf8.DecodeRuneInString(charList[x])
solist = append(solist, fmt.Sprintf("%#U\n", r))
solist = append(solist, fmt.Sprintf("%#U", r))
}
} else { // handle multiple individual arguments
r, _ := utf8.DecodeRuneInString(argv[i])
solist = append(solist, fmt.Sprintf("%#U\n", r))
solist = append(solist, fmt.Sprintf("%#U", r))
}

}
Expand Down

0 comments on commit 3454594

Please sign in to comment.