Skip to content

Commit

Permalink
Add -delta-buffer, docker color example
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jan 27, 2018
1 parent 1e5fb2c commit 079bfb1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 5.0.0
VERSION = 5.0.1

PACKAGES := $(shell go list -f {{.Dir}} ./...)
GOFILES := $(addsuffix /*.go,$(PACKAGES))
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ There are several pre-defined color scales:
| BlueToRed | `#00F -> #F00` |
| CyanToRed | `#0FF -> #F00` |
| GreenToRed | `#0F0 -> #F00` |
| GreenToGreenToRed | `#0F0 -> #0F0 -> #F00` |
| WhiteToPurple | `#FFF -> #F700FF` |
| WhiteToRed | `#FFF -> #F00` |
| WhiteToBlueToRed | `#FFF -> #00F -> #F00` |
Expand Down Expand Up @@ -215,7 +216,7 @@ RUN echo Done being slow
```

```bash
docker build . |
$ docker build . |
tj -start ^Step |
jq -s 'max_by(.deltaNanos) | {step:.startText, duration:.delta}'
```
Expand All @@ -224,6 +225,15 @@ docker build . |
{"step":"Step 3/4 : RUN sleep 10","duration":"10.602026127s"}
```

Alternatively, using color output and buffering:

```bash
$ docker build . |
tj -start ^Step -template Color -scale GreenToGreenToRed -delta-buffer
```

![Docker colors](docs/images/docker.png)

## Comments

Feel free to [leave a comment](https://github.com/sgreben/tj/issues/1) or create an issue.
50 changes: 40 additions & 10 deletions cmd/tj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type configuration struct {
colorScale string // -scale
fast time.Duration // -scale-fast
slow time.Duration // -scale-slow
buffer bool // -buffer
version string
}

Expand Down Expand Up @@ -86,14 +87,15 @@ var templates = map[string]string{
}

var colorScales = map[string]string{
"GreenToRed": "#0F0 -> #F00",
"BlueToRed": "#00F -> #F00",
"CyanToRed": "#0FF -> #F00",
"WhiteToRed": "#FFF -> #F00",
"WhiteToPurple": "#FFF -> #F700FF",
"BlackToRed": "#000 -> #F00",
"BlackToPurple": "#000 -> #F700FF",
"WhiteToBlueToRed": "#FFF -> #00F -> #F00",
"GreenToRed": "#0F0 -> #F00",
"GreenToGreenToRed": "#0F0 -> #0F0 -> #F00",
"BlueToRed": "#00F -> #F00",
"CyanToRed": "#0FF -> #F00",
"WhiteToRed": "#FFF -> #F00",
"WhiteToPurple": "#FFF -> #F700FF",
"BlackToRed": "#000 -> #F00",
"BlackToPurple": "#000 -> #F700FF",
"WhiteToBlueToRed": "#FFF -> #00F -> #F00",
}

var templateFuncs = template.FuncMap{
Expand Down Expand Up @@ -159,6 +161,7 @@ func init() {
flag.StringVar(&config.colorScale, "scale", "BlueToRed", colorScalesHelp())
flag.DurationVar(&config.fast, "scale-fast", 100*time.Millisecond, "the lower bound for the color scale")
flag.DurationVar(&config.slow, "scale-slow", 2*time.Second, "the upper bound for the color scale")
flag.BoolVar(&config.buffer, "delta-buffer", false, "buffer lines between -start matches, copy delta values from final line to buffered lines")
flag.Parse()
if knownFormat, ok := timeFormats[config.timeFormat]; ok {
config.timeFormat = knownFormat
Expand Down Expand Up @@ -186,8 +189,22 @@ func init() {
}
}

func flushLineBuffer(buffer *[]*line, line *line) {
for _, oldLine := range *buffer {
oldLine.DeltaSecs = line.DeltaSecs
oldLine.DeltaNanos = line.DeltaNanos
oldLine.DeltaString = line.DeltaString
oldLine.Delta = line.Delta
if err := printer(oldLine); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
}
}
*buffer = (*buffer)[:0]
}

func main() {
scanner := bufio.NewScanner(os.Stdin)
lineBuffer := []*line{}
line := line{Time: time.Now()}
first := line.Time
last := line.Time
Expand Down Expand Up @@ -226,21 +243,34 @@ func main() {
match = line.JSONText
}
}
if err := printer(&line); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
if !config.buffer {
if err := printer(&line); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
}
}
if start != nil {
if start.MatchString(match) {
if config.buffer {
flushLineBuffer(&lineBuffer, &line)
}
last = now
line.StartText = line.Text
line.StartObject = line.Object
}
if config.buffer {
lineCopy := line
lineBuffer = append(lineBuffer, &lineCopy)
}
} else {
last = now
}
i++
}

if config.buffer {
flushLineBuffer(&lineBuffer, &line)
}

if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "input error:", err)
os.Exit(1)
Expand Down
Binary file added docs/images/docker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 079bfb1

Please sign in to comment.