Skip to content

Commit

Permalink
Merge pull request #36 from egonelbre/pulse
Browse files Browse the repository at this point in the history
implement pulse
  • Loading branch information
mfridman authored Apr 13, 2019
2 parents 76a0846 + ea5bd10 commit 1e6a0f4
Showing 1 changed file with 50 additions and 11 deletions.
61 changes: 50 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"context"
"flag"
"fmt"
"io"
Expand All @@ -10,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/mfridman/tparse/parse"
"github.com/mfridman/tparse/version"
Expand All @@ -21,17 +23,18 @@ import (

// Flags.
var (
vPtr = flag.Bool("v", false, "")
versionPtr = flag.Bool("version", false, "")
allPtr = flag.Bool("all", false, "")
passPtr = flag.Bool("pass", false, "")
skipPtr = flag.Bool("skip", false, "")
showNoTestsPtr = flag.Bool("notests", false, "")
dumpPtr = flag.Bool("dump", false, "") // TODO(mf): rename this to -replay with v1
smallScreenPtr = flag.Bool("smallscreen", false, "")
topPtr = flag.Bool("top", false, "") // TODO(mf): rename this to -reverse with v1
noColorPtr = flag.Bool("nocolor", false, "")
slowPtr = flag.Int("slow", 0, "")
vPtr = flag.Bool("v", false, "")
versionPtr = flag.Bool("version", false, "")
allPtr = flag.Bool("all", false, "")
passPtr = flag.Bool("pass", false, "")
skipPtr = flag.Bool("skip", false, "")
showNoTestsPtr = flag.Bool("notests", false, "")
dumpPtr = flag.Bool("dump", false, "") // TODO(mf): rename this to -replay with v1
smallScreenPtr = flag.Bool("smallscreen", false, "")
topPtr = flag.Bool("top", false, "") // TODO(mf): rename this to -reverse with v1
noColorPtr = flag.Bool("nocolor", false, "")
slowPtr = flag.Int("slow", 0, "")
pulseIntervalPtr = flag.Duration("pulse", 0, "")
)

var usage = `Usage:
Expand All @@ -51,6 +54,7 @@ Options:
-top Display summary table towards top.
-slow Number of slowest tests to display. Default is 0, display all.
-nocolor Disable all colors.
-pulse d Print "." every interval d, specified as a time.Duration. Default is 0, disabled.
`

type consoleWriter struct {
Expand Down Expand Up @@ -80,6 +84,8 @@ func main() {
var replayBuf bytes.Buffer
tr := io.TeeReader(r, &replayBuf)

stopPulse := newPulse(*pulseIntervalPtr, ".")

pkgs, err := parse.Process(tr)
if err != nil {
switch err {
Expand All @@ -98,6 +104,8 @@ func main() {
os.Exit(1)
}

stopPulse()

if len(pkgs) == 0 {
fmt.Fprintf(os.Stdout, "tparse: no go packages to parse\n\n")
parse.ReplayOutput(os.Stderr, &replayBuf)
Expand Down Expand Up @@ -516,3 +524,34 @@ func colorize(s string, color int, enabled bool) string {
}
return fmt.Sprintf("\x1b[1;%dm%s\x1b[0m", color, s)
}

func newPulse(interval time.Duration, symbol string) (stop func()) {
if interval <= 0 {
return func() {}
}

done := make(chan struct{})

ctx, cancel := context.WithCancel(context.Background())
go func() {
defer close(done)

ticker := time.NewTicker(*pulseIntervalPtr)
defer ticker.Stop()
defer fmt.Println()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
fmt.Print(".")
}
}
}()

return func() {
cancel()
<-done
}
}

0 comments on commit 1e6a0f4

Please sign in to comment.