diff --git a/go.mod b/go.mod index b835919..8534260 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/k1574/goblin -go 1.17 +go 1.18 require github.com/k1574/gomtool v0.0.0-20220616060224-023d1559d777 // indirect diff --git a/m/cmd/goblin/main.go b/m/cmd/goblin/main.go index 5590c17..81a0bcb 100644 --- a/m/cmd/goblin/main.go +++ b/m/cmd/goblin/main.go @@ -2,24 +2,25 @@ package main import( "github.com/k1574/gomtool/m/multitool" - "github.com/k1574/goblin/m/sep/cat" - "github.com/k1574/goblin/m/sep/echo" - "github.com/k1574/goblin/m/sep/mkdir" - "github.com/k1574/goblin/m/sep/gtrue" - "github.com/k1574/goblin/m/sep/gfalse" - "github.com/k1574/goblin/m/sep/sort" - "github.com/k1574/goblin/m/sep/tac" - "github.com/k1574/goblin/m/sep/ls" - "github.com/k1574/goblin/m/sep/yes" - "github.com/k1574/goblin/m/sep/date" - "github.com/k1574/goblin/m/sep/uniq" - "github.com/k1574/goblin/m/sep/quote" - "github.com/k1574/goblin/m/sep/urlprs" - "github.com/k1574/goblin/m/sep/noext" - "github.com/k1574/goblin/m/sep/mergelbl" - "github.com/k1574/goblin/m/sep/basename" - "github.com/k1574/goblin/m/sep/ec" - "github.com/k1574/goblin/m/sep/read" + "github.com/k1574/goblin/m/tool/cat" + "github.com/k1574/goblin/m/tool/echo" + "github.com/k1574/goblin/m/tool/mkdir" + "github.com/k1574/goblin/m/tool/gtrue" + "github.com/k1574/goblin/m/tool/gfalse" + "github.com/k1574/goblin/m/tool/sort" + "github.com/k1574/goblin/m/tool/tac" + "github.com/k1574/goblin/m/tool/ls" + "github.com/k1574/goblin/m/tool/yes" + "github.com/k1574/goblin/m/tool/date" + "github.com/k1574/goblin/m/tool/uniq" + "github.com/k1574/goblin/m/tool/quote" + "github.com/k1574/goblin/m/tool/urlprs" + "github.com/k1574/goblin/m/tool/noext" + "github.com/k1574/goblin/m/tool/mergelbl" + "github.com/k1574/goblin/m/tool/basename" + "github.com/k1574/goblin/m/tool/ec" + "github.com/k1574/goblin/m/tool/read" + "github.com/k1574/goblin/m/tool/wc" ) func main() { @@ -42,6 +43,7 @@ func main() { "mergelbl" : mergelbl.Run, "ec" : ec.Run, "read" : read.Run, + "wc" : wc.Run, } multitool.Main("goblin", tools) diff --git a/m/gen/input/read.go b/m/input/read.go similarity index 100% rename from m/gen/input/read.go rename to m/input/read.go diff --git a/m/sep/basename/run.go b/m/tool/basename/run.go similarity index 100% rename from m/sep/basename/run.go rename to m/tool/basename/run.go diff --git a/m/sep/cat/cat.go b/m/tool/cat/cat.go similarity index 100% rename from m/sep/cat/cat.go rename to m/tool/cat/cat.go diff --git a/m/sep/date/date.go b/m/tool/date/date.go similarity index 100% rename from m/sep/date/date.go rename to m/tool/date/date.go diff --git a/m/sep/ec/run.go b/m/tool/ec/run.go similarity index 100% rename from m/sep/ec/run.go rename to m/tool/ec/run.go diff --git a/m/sep/echo/echo.go b/m/tool/echo/echo.go similarity index 100% rename from m/sep/echo/echo.go rename to m/tool/echo/echo.go diff --git a/m/sep/gfalse/false.go b/m/tool/gfalse/false.go similarity index 100% rename from m/sep/gfalse/false.go rename to m/tool/gfalse/false.go diff --git a/m/sep/gtrue/true.go b/m/tool/gtrue/true.go similarity index 100% rename from m/sep/gtrue/true.go rename to m/tool/gtrue/true.go diff --git a/m/sep/ls/ls.go b/m/tool/ls/ls.go similarity index 100% rename from m/sep/ls/ls.go rename to m/tool/ls/ls.go diff --git a/m/sep/mergelbl/main.go b/m/tool/mergelbl/main.go similarity index 100% rename from m/sep/mergelbl/main.go rename to m/tool/mergelbl/main.go diff --git a/m/sep/mkdir/mkdir.go b/m/tool/mkdir/mkdir.go similarity index 100% rename from m/sep/mkdir/mkdir.go rename to m/tool/mkdir/mkdir.go diff --git a/m/sep/noext/noext.go b/m/tool/noext/noext.go similarity index 100% rename from m/sep/noext/noext.go rename to m/tool/noext/noext.go diff --git a/m/sep/quote/quote.go b/m/tool/quote/quote.go similarity index 100% rename from m/sep/quote/quote.go rename to m/tool/quote/quote.go diff --git a/m/sep/read/main.go b/m/tool/read/main.go similarity index 100% rename from m/sep/read/main.go rename to m/tool/read/main.go diff --git a/m/sep/sort/sort.go b/m/tool/sort/sort.go similarity index 87% rename from m/sep/sort/sort.go rename to m/tool/sort/sort.go index 1c2b862..bc117a6 100644 --- a/m/sep/sort/sort.go +++ b/m/tool/sort/sort.go @@ -5,7 +5,7 @@ import ( "os" "sort" "flag" - "github.com/k1574/goblin/m/gen/input" + "github.com/k1574/goblin/m/input" ) func Run(args []string) { diff --git a/m/sep/tac/tac.go b/m/tool/tac/tac.go similarity index 100% rename from m/sep/tac/tac.go rename to m/tool/tac/tac.go diff --git a/m/sep/uniq/uniq.go b/m/tool/uniq/uniq.go similarity index 100% rename from m/sep/uniq/uniq.go rename to m/tool/uniq/uniq.go diff --git a/m/sep/urlprs/urlprs.go b/m/tool/urlprs/urlprs.go similarity index 100% rename from m/sep/urlprs/urlprs.go rename to m/tool/urlprs/urlprs.go diff --git a/m/tool/wc/main.go b/m/tool/wc/main.go new file mode 100644 index 0000000..ea8f9af --- /dev/null +++ b/m/tool/wc/main.go @@ -0,0 +1,101 @@ +package wc + +import ( + "os" + "io" + "bufio" + "fmt" + "unicode" + "flag" + "strconv" +) + +var ( + vals [lastFlag]int + flags [lastFlag]bool + flagList []int +) + +const ( + wordsFlag = iota + runesFlag + charsFlag + linesFlag + lastFlag +) + +func rdRune(rd *bufio.Reader) (rune, int) { + r, siz, err := rd.ReadRune() + if err == io.EOF { + finish() + } else if err != nil { + panic(err) + } + return r, siz +} + +func finish() { + str := "" + for i, v := range flagList { + str += strconv.Itoa(vals[v]) + if i != ( len(flagList)-1 ) { + str += "\t" + } + } + fmt.Print(str) + os.Exit(0) +} + +func Run(args []string) { + var ( + r rune + siz int + ) + arg0 := args[0] + args = args[1:] + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) + + flagSet.BoolVar(&flags[charsFlag], "c", false, "print amount of chars(bytes)") + flagSet.BoolVar(&flags[runesFlag], "r", false, "print amount of runes in UTF stream") + flagSet.BoolVar(&flags[wordsFlag], "w", false, "print amount of words") + flagSet.BoolVar(&flags[linesFlag], "l", false, "print amount of lines") + + flagSet.Parse(args) + + for i, v := range flags { + if v { + break; + } else if i == (len(flags) - 1){ + flagSet.Usage() + os.Exit(1) + } + } + + for i, v := range flags { + if v { + flagList = append(flagList, i) + } + } + + rd := bufio.NewReader(os.Stdin) + inWord := false + for { + r, siz = rdRune(rd) + if inWord { + if unicode.IsSpace(r) { + inWord = false + } + } else { + if !unicode.IsSpace(r) { + vals[wordsFlag]++ + inWord = true + } + } + vals[runesFlag]++ + vals[charsFlag] += siz + if r == rune('\n') { + vals[linesFlag]++ + } + } + finish() +} diff --git a/m/sep/yes/yes.go b/m/tool/yes/yes.go similarity index 96% rename from m/sep/yes/yes.go rename to m/tool/yes/yes.go index 5ab47d4..4acb2f6 100644 --- a/m/sep/yes/yes.go +++ b/m/tool/yes/yes.go @@ -6,7 +6,7 @@ import( "fmt" "flag" "strings" - "github.com/k1574/goblin/m/gen/input" + "github.com/k1574/goblin/m/input" ) var( nArg int