Skip to content

Commit 079bfb1

Browse files
committed
Add -delta-buffer, docker color example
1 parent 1e5fb2c commit 079bfb1

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = 5.0.0
1+
VERSION = 5.0.1
22

33
PACKAGES := $(shell go list -f {{.Dir}} ./...)
44
GOFILES := $(addsuffix /*.go,$(PACKAGES))

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ There are several pre-defined color scales:
150150
| BlueToRed | `#00F -> #F00` |
151151
| CyanToRed | `#0FF -> #F00` |
152152
| GreenToRed | `#0F0 -> #F00` |
153+
| GreenToGreenToRed | `#0F0 -> #0F0 -> #F00` |
153154
| WhiteToPurple | `#FFF -> #F700FF` |
154155
| WhiteToRed | `#FFF -> #F00` |
155156
| WhiteToBlueToRed | `#FFF -> #00F -> #F00` |
@@ -215,7 +216,7 @@ RUN echo Done being slow
215216
```
216217

217218
```bash
218-
docker build . |
219+
$ docker build . |
219220
tj -start ^Step |
220221
jq -s 'max_by(.deltaNanos) | {step:.startText, duration:.delta}'
221222
```
@@ -224,6 +225,15 @@ docker build . |
224225
{"step":"Step 3/4 : RUN sleep 10","duration":"10.602026127s"}
225226
```
226227

228+
Alternatively, using color output and buffering:
229+
230+
```bash
231+
$ docker build . |
232+
tj -start ^Step -template Color -scale GreenToGreenToRed -delta-buffer
233+
```
234+
235+
![Docker colors](docs/images/docker.png)
236+
227237
## Comments
228238

229239
Feel free to [leave a comment](https://github.com/sgreben/tj/issues/1) or create an issue.

cmd/tj/main.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type configuration struct {
4646
colorScale string // -scale
4747
fast time.Duration // -scale-fast
4848
slow time.Duration // -scale-slow
49+
buffer bool // -buffer
4950
version string
5051
}
5152

@@ -86,14 +87,15 @@ var templates = map[string]string{
8687
}
8788

8889
var colorScales = map[string]string{
89-
"GreenToRed": "#0F0 -> #F00",
90-
"BlueToRed": "#00F -> #F00",
91-
"CyanToRed": "#0FF -> #F00",
92-
"WhiteToRed": "#FFF -> #F00",
93-
"WhiteToPurple": "#FFF -> #F700FF",
94-
"BlackToRed": "#000 -> #F00",
95-
"BlackToPurple": "#000 -> #F700FF",
96-
"WhiteToBlueToRed": "#FFF -> #00F -> #F00",
90+
"GreenToRed": "#0F0 -> #F00",
91+
"GreenToGreenToRed": "#0F0 -> #0F0 -> #F00",
92+
"BlueToRed": "#00F -> #F00",
93+
"CyanToRed": "#0FF -> #F00",
94+
"WhiteToRed": "#FFF -> #F00",
95+
"WhiteToPurple": "#FFF -> #F700FF",
96+
"BlackToRed": "#000 -> #F00",
97+
"BlackToPurple": "#000 -> #F700FF",
98+
"WhiteToBlueToRed": "#FFF -> #00F -> #F00",
9799
}
98100

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

192+
func flushLineBuffer(buffer *[]*line, line *line) {
193+
for _, oldLine := range *buffer {
194+
oldLine.DeltaSecs = line.DeltaSecs
195+
oldLine.DeltaNanos = line.DeltaNanos
196+
oldLine.DeltaString = line.DeltaString
197+
oldLine.Delta = line.Delta
198+
if err := printer(oldLine); err != nil {
199+
fmt.Fprintln(os.Stderr, "output error:", err)
200+
}
201+
}
202+
*buffer = (*buffer)[:0]
203+
}
204+
189205
func main() {
190206
scanner := bufio.NewScanner(os.Stdin)
207+
lineBuffer := []*line{}
191208
line := line{Time: time.Now()}
192209
first := line.Time
193210
last := line.Time
@@ -226,21 +243,34 @@ func main() {
226243
match = line.JSONText
227244
}
228245
}
229-
if err := printer(&line); err != nil {
230-
fmt.Fprintln(os.Stderr, "output error:", err)
246+
if !config.buffer {
247+
if err := printer(&line); err != nil {
248+
fmt.Fprintln(os.Stderr, "output error:", err)
249+
}
231250
}
232251
if start != nil {
233252
if start.MatchString(match) {
253+
if config.buffer {
254+
flushLineBuffer(&lineBuffer, &line)
255+
}
234256
last = now
235257
line.StartText = line.Text
236258
line.StartObject = line.Object
237259
}
260+
if config.buffer {
261+
lineCopy := line
262+
lineBuffer = append(lineBuffer, &lineCopy)
263+
}
238264
} else {
239265
last = now
240266
}
241267
i++
242268
}
243269

270+
if config.buffer {
271+
flushLineBuffer(&lineBuffer, &line)
272+
}
273+
244274
if err := scanner.Err(); err != nil {
245275
fmt.Fprintln(os.Stderr, "input error:", err)
246276
os.Exit(1)

docs/images/docker.png

124 KB
Loading

0 commit comments

Comments
 (0)