Skip to content

Commit 58bfe76

Browse files
committed
Add -start
1 parent 6ac2659 commit 58bfe76

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = 1.1.0
1+
VERSION = 1.2.0
22

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

Diff for: README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ Or [download the binary](https://github.com/sgreben/ts/releases) from the releas
1717
`ts` reads from stdin and writes to stdout.
1818

1919
```text
20-
Usage of ts:
21-
-previous
22-
include previous line
20+
Usage of ts:
2321
-plain
24-
-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'
22+
-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'
23+
-previous
24+
include previous line
25+
-start string
26+
a regex pattern. if given, only lines matching it (re)start the stopwatch
2527
-template string
26-
go template (https://golang.org/pkg/text/template)
28+
go template (https://golang.org/pkg/text/template)
2729
-timeformat string
28-
either a go time format string or one of the predefined format names (https://golang.org/pkg/time/#pkg-constants)
2930
```
3031

3132
### JSON output

Diff for: cmd/ts/main.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"flag"
88
"fmt"
99
"os"
10+
"regexp"
1011
"text/template"
1112
"time"
1213
)
@@ -26,12 +27,14 @@ type line struct {
2627
Total string `json:"total,omitempty"`
2728
Text string `json:"text,omitempty"`
2829
Previous string `json:"previous,omitempty"`
30+
Start string `json:"start,omitempty"`
2931
}
3032

3133
type configuration struct {
3234
timeFormat string // -timeformat="..."
3335
template string // -template="..."
3436
plain bool // -plain
37+
start string // -start="..."
3538
version string
3639
previous bool
3740
}
@@ -90,6 +93,7 @@ func init() {
9093
flag.StringVar(&config.template, "template", "", "go template (https://golang.org/pkg/text/template)")
9194
flag.StringVar(&config.timeFormat, "timeformat", "RFC3339", timeFormatsHelp())
9295
flag.BoolVar(&config.plain, "plain", false, "-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'")
96+
flag.StringVar(&config.start, "start", "", "a regex pattern. if given, only lines matching it (re)start the stopwatch")
9397
flag.BoolVar(&config.previous, "previous", false, "include previous line")
9498
flag.Parse()
9599
if knownFormat, ok := timeFormats[config.timeFormat]; ok {
@@ -113,6 +117,10 @@ func main() {
113117
first := now
114118
previous := ""
115119
i := uint64(0)
120+
var start *regexp.Regexp
121+
if config.start != "" {
122+
start = regexp.MustCompile(config.start)
123+
}
116124
for scanner.Scan() {
117125
now = time.Now()
118126
delta := now.Sub(last)
@@ -137,7 +145,10 @@ func main() {
137145
if err := printer(&line); err != nil {
138146
fmt.Fprintln(os.Stderr, "output error:", err)
139147
}
140-
last = now
148+
if start != nil && start.MatchString(line.Text) {
149+
last = now
150+
line.Start = line.Text
151+
}
141152
i++
142153
}
143154

0 commit comments

Comments
 (0)