Skip to content

Commit 6ac2659

Browse files
committed
Add -previous and example
1 parent cddbc5b commit 6ac2659

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

Makefile

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

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

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Or [download the binary](https://github.com/sgreben/ts/releases) from the releas
1818

1919
```text
2020
Usage of ts:
21+
-previous
22+
include previous line
2123
-plain
2224
-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'
2325
-template string
@@ -85,4 +87,27 @@ $ (echo Hello; echo World) | ts -template '{{ .I }} {{.TimeSecs}} {{.Text}}'
8587
1 1516649679 World
8688
```
8789

88-
The fields available to the template are specified in the [`line` struct](cmd/ts/main.go#L14).
90+
The fields available to the template are specified in the [`line` struct](cmd/ts/main.go#L14).
91+
92+
## Example
93+
94+
Finding the slowest step in a `docker build` (using `jq`):
95+
96+
```bash
97+
$ cat Dockerfile
98+
FROM alpine
99+
RUN echo About to be slow...
100+
RUN sleep 10
101+
RUN echo Done being slow
102+
```
103+
104+
```bash
105+
docker build . |
106+
grep --line-buffered -E -e '^(Step|Successfully built)' |
107+
ts -previous |
108+
jq -s 'max_by(.deltaNanos) | {step:.previous, duration:.delta}'
109+
```
110+
111+
```json
112+
{"step":"Step 3/4 : RUN sleep 10","duration":"10.602026127s"}
113+
```

cmd/ts/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ type line struct {
2525
TotalNanos int64 `json:"totalNanos"`
2626
Total string `json:"total,omitempty"`
2727
Text string `json:"text,omitempty"`
28+
Previous string `json:"previous,omitempty"`
2829
}
2930

3031
type configuration struct {
3132
timeFormat string // -timeformat="..."
3233
template string // -template="..."
3334
plain bool // -plain
3435
version string
36+
previous bool
3537
}
3638

3739
var config = configuration{}
@@ -88,6 +90,7 @@ func init() {
8890
flag.StringVar(&config.template, "template", "", "go template (https://golang.org/pkg/text/template)")
8991
flag.StringVar(&config.timeFormat, "timeformat", "RFC3339", timeFormatsHelp())
9092
flag.BoolVar(&config.plain, "plain", false, "-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'")
93+
flag.BoolVar(&config.previous, "previous", false, "include previous line")
9194
flag.Parse()
9295
if knownFormat, ok := timeFormats[config.timeFormat]; ok {
9396
config.timeFormat = knownFormat
@@ -108,6 +111,7 @@ func main() {
108111
line := line{}
109112
last := now
110113
first := now
114+
previous := ""
111115
i := uint64(0)
112116
for scanner.Scan() {
113117
now = time.Now()
@@ -126,6 +130,10 @@ func main() {
126130
line.TimeString = now.Format(config.timeFormat)
127131
line.Text = scanner.Text()
128132
line.I = i
133+
if config.previous {
134+
line.Previous = previous
135+
previous = line.Text
136+
}
129137
if err := printer(&line); err != nil {
130138
fmt.Fprintln(os.Stderr, "output error:", err)
131139
}

0 commit comments

Comments
 (0)