From 45717808e4c8861e022e838aacb8215c81dcd327 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 18 Apr 2018 16:14:29 -0400 Subject: refactor the ui code a bit --- jwg.go | 56 ++------------------------------------------------------ 1 file changed, 2 insertions(+), 54 deletions(-) (limited to 'jwg.go') 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} -} -- cgit v1.2.3