diff options
author | Luke Shumaker <lukeshu@beefcake.parabola.nu> | 2018-06-05 12:44:45 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@beefcake.parabola.nu> | 2018-06-05 12:44:45 -0400 |
commit | 1ff3ecb5b08b40686b0e03cf79c4233c5bf99396 (patch) | |
tree | 833b3f6ff19f0b5ae8314a850e9c3b7cc0329540 | |
parent | 8caefed85ae0253a1ea5b95a5a0ad25146b5d6d7 (diff) |
fixup statusline
-rw-r--r-- | go/src/cow-dedupe/dedupe.go | 39 | ||||
-rw-r--r-- | go/src/lib/statusline/log.go | 21 | ||||
-rw-r--r-- | go/src/lib/statusline/ratelimit.go | 18 | ||||
-rw-r--r-- | go/src/lib/statusline/statusline.go | 27 | ||||
-rw-r--r-- | go/src/lib/statusline/tty.go | 28 |
5 files changed, 92 insertions, 41 deletions
diff --git a/go/src/cow-dedupe/dedupe.go b/go/src/cow-dedupe/dedupe.go index 4913f3d..0c40d4a 100644 --- a/go/src/cow-dedupe/dedupe.go +++ b/go/src/cow-dedupe/dedupe.go @@ -27,22 +27,40 @@ func errhandle(err error) { } } -func myStatusLine() statusline.StatusLine { - var outs []statusline.StatusLine +var myStatusLine = func() func() statusline.StatusLine { + var outs []func() statusline.StatusLine if C.isatty(2) != 0 { - outs = append(outs, statusline.StopWatch(statusline.RateLimit(statusline.New(os.Stderr), time.Second/2), time.Second)) + fmt.Println("Output: TTY") + outs = append(outs, func() statusline.StatusLine { + return statusline.StopWatch(statusline.RateLimit(statusline.TTY(os.Stderr), time.Second/2), time.Second) + }) } if os.Getenv("NOTIFY_SOCKET") != "" { - outs = append(outs, statusline.RateLimit(statusline.DaemonStatus(0), time.Second/2)) + fmt.Println("Output: SD") + outs = append(outs, func() statusline.StatusLine { + return statusline.RateLimit(statusline.DaemonStatus(0), time.Second/2) + }) + outs = append(outs, func() statusline.StatusLine { + return statusline.RateLimit(statusline.Log(os.Stderr), time.Minute) + }) } if len(outs) == 0 { - outs = append(outs, statusline.New(os.Stderr)) + fmt.Println("Output: ERR") + outs = append(outs, func() statusline.StatusLine { + return statusline.Log(os.Stderr) + }) } if len(outs) == 1 { return outs[0] } - return statusline.Tee(outs...) -} + return func() statusline.StatusLine { + var sls []statusline.StatusLine + for _, fn := range outs { + sls = append(sls, fn()) + } + return statusline.Tee(sls...) + } +}() func getFiemaps(paths []string) map[string][]string { var err error @@ -216,7 +234,12 @@ func main() { fiemap2filenames := getFiemaps(os.Args[1:]) - sl := statusline.StopWatch(statusline.New(os.Stderr), time.Second) + var sl statusline.StatusLine + if C.isatty(2) != 0 { + sl = statusline.StopWatch(statusline.TTY(os.Stderr), time.Second) + } else { + sl = statusline.Log(os.Stderr) + } sl.Put("Building list of spanning files...") filename2fiemap := map[string]string{} diff --git a/go/src/lib/statusline/log.go b/go/src/lib/statusline/log.go new file mode 100644 index 0000000..2026cd4 --- /dev/null +++ b/go/src/lib/statusline/log.go @@ -0,0 +1,21 @@ +package statusline + +import ( + "fmt" + "io" +) + +type log struct { + out io.Writer +} + +func Log(out io.Writer) StatusLine { + return &log{out: out} +} + +func (sl *log) Put(line string) { + fmt.Fprintln(sl.out, line) +} + +func (sl *log) End(keep bool) { +} diff --git a/go/src/lib/statusline/ratelimit.go b/go/src/lib/statusline/ratelimit.go index 970f8e5..087466d 100644 --- a/go/src/lib/statusline/ratelimit.go +++ b/go/src/lib/statusline/ratelimit.go @@ -20,19 +20,25 @@ func RateLimit(sl StatusLine, d time.Duration) StatusLine { ticker := time.NewTicker(d) var oldLine string var newLine string - dirty := false + first := true for { select { case <-ticker.C: - if dirty && newLine != oldLine { + if newLine != oldLine { sl.Put(newLine) + oldLine = newLine } - dirty = false case line := <-ret.lines: - newLine = line - dirty = true + if first { + first = false + oldLine = line + newLine = line + sl.Put(line) + } else { + newLine = line + } case keep := <-ret.end1: - if keep && dirty && newLine != oldLine { + if newLine != oldLine { sl.Put(newLine) } sl.End(keep) diff --git a/go/src/lib/statusline/statusline.go b/go/src/lib/statusline/statusline.go index bedf952..03b64bd 100644 --- a/go/src/lib/statusline/statusline.go +++ b/go/src/lib/statusline/statusline.go @@ -1,33 +1,6 @@ package statusline -import ( - "fmt" - "io" -) - type StatusLine interface { Put(line string) End(keep bool) } - -type statusLine struct { - out io.Writer - prevLen int -} - -func New(out io.Writer) StatusLine { - return &statusLine{out: out} -} - -func (sl *statusLine) Put(line string) { - fmt.Fprintf(sl.out, "\r%-[1]*[2]s", sl.prevLen, line) - sl.prevLen = len(line) -} - -func (sl *statusLine) End(keep bool) { - if keep { - io.WriteString(sl.out, "\n") - } else { - fmt.Fprintf(sl.out, "\r%-[1]*[2]s\r", sl.prevLen, "") - } -} diff --git a/go/src/lib/statusline/tty.go b/go/src/lib/statusline/tty.go new file mode 100644 index 0000000..72bbb57 --- /dev/null +++ b/go/src/lib/statusline/tty.go @@ -0,0 +1,28 @@ +package statusline + +import ( + "fmt" + "io" +) + +type statusLine struct { + out io.Writer + prevLen int +} + +func TTY(out io.Writer) StatusLine { + return &statusLine{out: out} +} + +func (sl *statusLine) Put(line string) { + fmt.Fprintf(sl.out, "\r%-[1]*[2]s", sl.prevLen, line) + sl.prevLen = len(line) +} + +func (sl *statusLine) End(keep bool) { + if keep { + io.WriteString(sl.out, "\n") + } else { + fmt.Fprintf(sl.out, "\r%-[1]*[2]s\r", sl.prevLen, "") + } +} |