Skip to content

Instantly share code, notes, and snippets.

@peterbourgon
Last active July 6, 2023 18:32
Show Gist options
  • Save peterbourgon/45c32f90a0566959043857363df0c7f4 to your computer and use it in GitHub Desktop.
Save peterbourgon/45c32f90a0566959043857363df0c7f4 to your computer and use it in GitHub Desktop.
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