Skip to content

Commit

Permalink
Save a function call
Browse files Browse the repository at this point in the history
  • Loading branch information
timdp committed Apr 24, 2018
1 parent 4fc0f03 commit a90fbea
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions cmd/lwc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const DEFAULT_INTERVAL int = 100
const COUNT_FORMAT string = "%8d"
const CARRIAGE_RETURN byte = 13
const LINE_FEED byte = 10
const SPACE byte = 32

type ScanFunc func(*bufio.Scanner, *uint64, *uint64)
Expand Down Expand Up @@ -129,7 +130,7 @@ func openFile(name string) *os.File {
return file
}

func printCounts(counts *[]uint64, label string, cr bool) {
func printCounts(counts *[]uint64, label string, cr bool, lf bool) {
var sb strings.Builder
if cr {
sb.WriteByte(CARRIAGE_RETURN)
Expand All @@ -143,6 +144,9 @@ func printCounts(counts *[]uint64, label string, cr bool) {
sb.WriteByte(SPACE)
sb.WriteString(label)
}
if lf {
sb.WriteByte(LINE_FEED)
}
os.Stdout.WriteString(sb.String())
}

Expand All @@ -152,7 +156,7 @@ func pollCounts(name string, counts *[]uint64, interval time.Duration, done chan
for {
select {
case <-tick.C:
printCounts(counts, name, true)
printCounts(counts, name, true, false)
case <-done:
break
}
Expand Down Expand Up @@ -189,7 +193,7 @@ func processFile(file *os.File, name string, processors []Processor, totals *[]u
counts := make([]uint64, numCounts)

// Write zeroes straightaway in case file is empty
printCounts(&counts, name, false)
printCounts(&counts, name, false, false)

// For each counter, set up a pipe
prs := make([]*io.PipeReader, numCounts)
Expand Down Expand Up @@ -225,8 +229,7 @@ func processFile(file *os.File, name string, processors []Processor, totals *[]u
done <- true

// Write final counts
printCounts(&counts, name, true)
fmt.Println()
printCounts(&counts, name, true, true)
}

func processFiles(config *Config, processors []Processor) {
Expand All @@ -253,7 +256,7 @@ func processFiles(config *Config, processors []Processor) {

// If we were keeping totals, print them now
if totals != nil {
printCounts(totals, "total\n", false)
printCounts(totals, "total\n", false, false)
}
}

Expand Down

0 comments on commit a90fbea

Please sign in to comment.