summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2018-04-18 16:14:29 -0400
committerLuke Shumaker <lukeshu@lukeshu.com>2018-04-18 16:14:29 -0400
commit45717808e4c8861e022e838aacb8215c81dcd327 (patch)
tree2a9e7d6d02a8979adc5346d5cc3fc838f0ce8a38
parentc78cb15705d6e4fb1c8a46a129391bb79e78d9a1 (diff)
refactor the ui code a bit
-rw-r--r--jwg.go56
-rw-r--r--main.go52
2 files changed, 53 insertions, 55 deletions
diff --git a/jwg.go b/jwg.go
index 2bde209..a7fd343 100644
--- a/jwg.go
+++ b/jwg.go
@@ -2,9 +2,7 @@ package main
import (
"fmt"
- "os"
"sort"
- "strings"
"sync"
"time"
)
@@ -39,8 +37,9 @@ func (jwg *JobWaitGroup) Do(name string, fn func()) {
}()
}
-func (jwg *JobWaitGroup) Wait() {
+func (jwg *JobWaitGroup) Wait() map[string]time.Duration {
jwg.wg.Wait()
+ return jwg.jobs
}
func (jwg *JobWaitGroup) Status() (int, []string) {
@@ -58,54 +57,3 @@ func (jwg *JobWaitGroup) Status() (int, []string) {
return n, jobs
}
-
-func (jwg *JobWaitGroup) Watch(d time.Duration) {
- ticker := time.NewTicker(d)
- done := make(chan struct{})
- go func() {
- jwg.Wait()
- ticker.Stop()
- done <- struct{}{}
- }()
- for {
- select {
- case <-ticker.C:
- n, jobs := jwg.Status()
- if len(jobs) == 0 {
- panic("no active jobs, but wg still waiting")
- }
- line := fmt.Sprintf("%d/%d : %v", len(jobs), n, strings.Join(jobs, ", "))
- if len(line) > 70 {
- line = line[:67] + "..."
- }
- fmt.Fprintf(os.Stderr, "\r%-70s", line)
- case <-done:
- fmt.Fprintf(os.Stderr, "\r%-70s\n", "done")
- jwg.lock.RLock()
- defer jwg.lock.RUnlock()
- s := newSortHelper(jwg.jobs)
- sort.Sort(s)
- for _, job := range s.StringSlice {
- fmt.Fprintln(os.Stderr, s.times[job], job)
- }
- return
- }
- }
-}
-
-type sortHelper struct {
- times map[string]time.Duration
- sort.StringSlice
-}
-
-func (s sortHelper) Less(i, j int) bool {
- return s.times[s.StringSlice[i]] < s.times[s.StringSlice[j]]
-}
-
-func newSortHelper(jobs map[string]time.Duration) sortHelper {
- slice := make([]string, 0, len(jobs))
- for job := range jobs {
- slice = append(slice, job)
- }
- return sortHelper{times: jobs, StringSlice: slice}
-}
diff --git a/main.go b/main.go
index 3bbac78..a89dc46 100644
--- a/main.go
+++ b/main.go
@@ -6,6 +6,7 @@ import (
"net"
"os"
"path/filepath"
+ "sort"
"strings"
"sync"
"time"
@@ -122,5 +123,54 @@ func main() {
for _, fname := range os.Args[1:] {
DoHostfile(fname)
}
- jwg.Watch(time.Second)
+ watch(time.Second)
+}
+
+func watch(d time.Duration) {
+ ticker := time.NewTicker(d)
+ done := make(chan map[string]time.Duration)
+ go func() {
+ jobs := jwg.Wait()
+ ticker.Stop()
+ done <- jobs
+ }()
+ for {
+ select {
+ case <-ticker.C:
+ n, jobs := jwg.Status()
+ if len(jobs) == 0 {
+ panic("no active jobs, but wg still waiting")
+ }
+ line := fmt.Sprintf("%d/%d : %v", len(jobs), n, strings.Join(jobs, ", "))
+ if len(line) > 70 {
+ line = line[:67] + "..."
+ }
+ fmt.Fprintf(os.Stderr, "\r%-70s", line)
+ case jobs := <-done:
+ fmt.Fprintf(os.Stderr, "\r%-70s\n", "done")
+ s := newSortHelper(jobs)
+ sort.Sort(s)
+ for _, job := range s.StringSlice {
+ fmt.Fprintln(os.Stderr, s.times[job], job)
+ }
+ return
+ }
+ }
+}
+
+type sortHelper struct {
+ times map[string]time.Duration
+ sort.StringSlice
+}
+
+func (s sortHelper) Less(i, j int) bool {
+ return s.times[s.StringSlice[i]] < s.times[s.StringSlice[j]]
+}
+
+func newSortHelper(jobs map[string]time.Duration) sortHelper {
+ slice := make([]string, 0, len(jobs))
+ for job := range jobs {
+ slice = append(slice, job)
+ }
+ return sortHelper{times: jobs, StringSlice: slice}
}