Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement pulse #36

Merged
merged 5 commits into from
Apr 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "")
mfridman marked this conversation as resolved.
Show resolved Hide resolved
)

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, ".")
mfridman marked this conversation as resolved.
Show resolved Hide resolved

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
}
}