Skip to content

Instantly share code, notes, and snippets.

@lmas
Forked from peterbourgon/golf.go
Last active July 3, 2023 15:10
Show Gist options
  • Save lmas/4f044439a900e5db038ae86446b6ff19 to your computer and use it in GitHub Desktop.
Save lmas/4f044439a900e5db038ae86446b6ff19 to your computer and use it in GitHub Desktop.
Separate chunks of live log output (like spaceror tailer)
package main
import (
"fmt"
"io"
"os"
"strings"
"time"
)
func main() {
timer := time.NewTimer(0)
timer.Stop() // don't let the timer fire yet
go func() {
last := time.Now()
for ts := range timer.C {
fmt.Fprintf(os.Stderr,
"%s %s %s\n",
ts.Format(time.RFC3339),
ts.Sub(last).Truncate(100*time.Millisecond),
strings.Repeat("-", 20),
)
last = ts
}
}()
io.Copy(
writeFunc(func(p []byte) (int, error) {
timer.Reset(3 * time.Second)
return os.Stdout.Write(p)
}),
os.Stdin,
)
}
type writeFunc func(p []byte) (int, error)
func (wf writeFunc) Write(p []byte) (int, error) { return wf(p) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment