Skip to content

Commit

Permalink
Remove -previous, rename Total to TotalString, add Total
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jan 24, 2018
1 parent c85b5c5 commit 12fbfe0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 1.2.0
VERSION = 2.0.0

PACKAGES := $(shell go list -f {{.Dir}} ./...)
GOFILES := $(addsuffix /*.go,$(PACKAGES))
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ Or [download the binary](https://github.com/sgreben/ts/releases) from the releas
Usage of ts:
-plain
-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'
-previous
include previous line
-start string
a regex pattern. if given, only lines matching it (re)start the stopwatch
-template string
go template (https://golang.org/pkg/text/template)
-timeformat string
either a go time format string or one of the predefined format names (https://golang.org/pkg/time/#pkg-constants)
```

### JSON output
Expand Down Expand Up @@ -88,7 +87,7 @@ $ (echo Hello; echo World) | ts -template '{{ .I }} {{.TimeSecs}} {{.Text}}'
1 1516649679 World
```

The fields available to the template are specified in the [`line` struct](cmd/ts/main.go#L14).
The fields available to the template are specified in the [`line` struct](cmd/ts/main.go#L15).

## Example

Expand Down
27 changes: 10 additions & 17 deletions cmd/ts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type line struct {
Delta time.Duration `json:"-"`
TotalSecs float64 `json:"totalSecs"`
TotalNanos int64 `json:"totalNanos"`
Total string `json:"total,omitempty"`
TotalString string `json:"total,omitempty"`
Total time.Duration `json:"-"`
Text string `json:"text,omitempty"`
Previous string `json:"previous,omitempty"`
Start string `json:"start,omitempty"`
}

Expand All @@ -36,7 +36,6 @@ type configuration struct {
plain bool // -plain
start string // -start="..."
version string
previous bool
}

var config = configuration{}
Expand Down Expand Up @@ -96,7 +95,6 @@ func init() {
flag.StringVar(&config.timeFormat, "timeformat", "RFC3339", timeFormatsHelp())
flag.BoolVar(&config.plain, "plain", false, "-template='{{.Time}} +{{.DeltaNanos}} {{.Text}}'")
flag.StringVar(&config.start, "start", "", "a regex pattern. if given, only lines matching it (re)start the stopwatch")
flag.BoolVar(&config.previous, "previous", false, "include previous line")
flag.Parse()
if knownFormat, ok := timeFormats[config.timeFormat]; ok {
config.timeFormat = knownFormat
Expand All @@ -116,33 +114,28 @@ func init() {

func main() {
scanner := bufio.NewScanner(os.Stdin)
now := time.Now()
line := line{}
last := now
first := now
previous := ""
line := line{Time: time.Now()}
first := line.Time
last := line.Time
i := uint64(0)
for scanner.Scan() {
now = time.Now()
now := time.Now()
delta := now.Sub(last)
total := now.Sub(first)
line.DeltaSecs = delta.Seconds()
line.DeltaNanos = delta.Nanoseconds()
line.Delta = delta
line.DeltaString = delta.String()
line.Delta = delta
line.TotalSecs = total.Seconds()
line.TotalNanos = total.Nanoseconds()
line.Total = total.String()
line.TotalString = total.String()
line.Total = total
line.TimeSecs = now.Unix()
line.TimeNanos = now.UnixNano()
line.Time = now
line.TimeString = now.Format(config.timeFormat)
line.Time = now
line.Text = scanner.Text()
line.I = i
if config.previous {
line.Previous = previous
previous = line.Text
}
if err := printer(&line); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
}
Expand Down

0 comments on commit 12fbfe0

Please sign in to comment.