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 --- main.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'main.go') 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} } -- cgit v1.2.3