aggrigate results in main
This commit is contained in:
parent
dd2748d8ae
commit
e8d2d2cb59
@ -72,8 +72,66 @@ func main() {
|
|||||||
if len(goopt.Args) == 0 {
|
if len(goopt.Args) == 0 {
|
||||||
fmt.Println(goopt.Help())
|
fmt.Println(goopt.Help())
|
||||||
} else {
|
} else {
|
||||||
|
if !*fWords && !*fChars && !*fLines && !*fBytes && !*fMaxLineLength {
|
||||||
|
*fWords = true
|
||||||
|
*fChars = false
|
||||||
|
*fLines = true
|
||||||
|
*fBytes = true
|
||||||
|
*fMaxLineLength = false
|
||||||
|
}
|
||||||
|
var TotalCounts wc.Counter
|
||||||
for _, a := range goopt.Args {
|
for _, a := range goopt.Args {
|
||||||
wc.Count(a, *fWords, *fChars, *fLines, *fBytes, *fMaxLineLength)
|
count, err := wc.Count(a, *fWords, *fChars, *fLines, *fBytes, *fMaxLineLength)
|
||||||
|
if err == nil {
|
||||||
|
TotalCounts.Lines += count.Lines
|
||||||
|
TotalCounts.Bytes += count.Bytes
|
||||||
|
TotalCounts.Words += count.Words
|
||||||
|
TotalCounts.Chars += count.Chars
|
||||||
|
TotalCounts.MaxLineLength += count.MaxLineLength
|
||||||
|
if *fLines {
|
||||||
|
fmt.Printf("%d", count.Lines)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fWords {
|
||||||
|
fmt.Printf("%d", count.Words)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fChars {
|
||||||
|
fmt.Printf("%d", count.Chars)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fBytes {
|
||||||
|
fmt.Printf("%d", count.Bytes)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fMaxLineLength {
|
||||||
|
fmt.Printf("%d", count.MaxLineLength)
|
||||||
|
}
|
||||||
|
fmt.Printf(" %s\n", a)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if len(goopt.Args) > 1 {
|
||||||
|
if *fLines {
|
||||||
|
fmt.Printf("%d", TotalCounts.Lines)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fWords {
|
||||||
|
fmt.Printf("%d", TotalCounts.Words)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fChars {
|
||||||
|
fmt.Printf("%d", TotalCounts.Chars)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fBytes {
|
||||||
|
fmt.Printf("%d", TotalCounts.Bytes)
|
||||||
|
}
|
||||||
|
fmt.Printf(" ")
|
||||||
|
if *fMaxLineLength {
|
||||||
|
fmt.Printf("%d", TotalCounts.MaxLineLength)
|
||||||
|
}
|
||||||
|
fmt.Println(" total")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,25 +18,18 @@ type Counter struct {
|
|||||||
MaxLineLength int64
|
MaxLineLength int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func Count(filename string, cw, cc, cl, cb, mll bool) {
|
func Count(filename string, cw, cc, cl, cb, mll bool) (Counter, error) {
|
||||||
if !cw && !cc && !cl && !cb && !mll {
|
|
||||||
cw = true
|
|
||||||
cc = false
|
|
||||||
cl = true
|
|
||||||
cb = true
|
|
||||||
mll = false
|
|
||||||
}
|
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return Counter{}, err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
processLine := cw || cc
|
processLine := cw || cc
|
||||||
|
|
||||||
var c = &Counter{}
|
var c = &Counter{}
|
||||||
numWorkers := 2 //runtime.NumCPU()
|
numWorkers := runtime.NumCPU()
|
||||||
|
|
||||||
if cl && !processLine {
|
if cl && !processLine {
|
||||||
c.Lines = CountLines(file, numWorkers)
|
c.Lines = CountLines(file, numWorkers)
|
||||||
@ -48,27 +42,11 @@ func Count(filename string, cw, cc, cl, cb, mll bool) {
|
|||||||
fi, err := file.Stat()
|
fi, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return Counter{}, err
|
||||||
}
|
}
|
||||||
c.Bytes = fi.Size()
|
c.Bytes = fi.Size()
|
||||||
}
|
}
|
||||||
|
return *c, nil
|
||||||
if cl {
|
|
||||||
fmt.Printf("%d ", c.Lines)
|
|
||||||
}
|
|
||||||
if cw {
|
|
||||||
fmt.Printf("%d ", c.Words)
|
|
||||||
}
|
|
||||||
if cc {
|
|
||||||
fmt.Printf("%d ", c.Chars)
|
|
||||||
}
|
|
||||||
if cb {
|
|
||||||
fmt.Printf("%d ", c.Bytes)
|
|
||||||
}
|
|
||||||
if mll {
|
|
||||||
fmt.Printf("%d ", c.MaxLineLength)
|
|
||||||
}
|
|
||||||
fmt.Printf("%s\n", filename)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func CountLines(file *os.File, numWorkers int) int64 {
|
func CountLines(file *os.File, numWorkers int) int64 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user