aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2021-09-11 14:04:11 +0200
committerMartin Polden <mpolden@mpolden.no>2021-09-12 09:51:57 +0200
commitcfb4e7e874e16491683b48f5ec7e56093a0680bd (patch)
tree61c0c8ffe3f827ba49dd309a98802bfebe89d15e
parentd32ea40e94af9f057e9731f24281c47693a8f79e (diff)
cmd: Print only unique log lines
-rw-r--r--cmd/unp/main.go11
-rw-r--r--go.mod4
-rw-r--r--go.sum5
-rw-r--r--logutil/logutil.go40
-rw-r--r--logutil/logutil_test.go25
5 files changed, 82 insertions, 3 deletions
diff --git a/cmd/unp/main.go b/cmd/unp/main.go
index 0b08820..f868a8a 100644
--- a/cmd/unp/main.go
+++ b/cmd/unp/main.go
@@ -3,14 +3,25 @@ package main
import (
"flag"
"fmt"
+ "io"
"log"
+ "os"
+ "github.com/mattn/go-isatty"
+ "github.com/mpolden/unp/logutil"
"github.com/mpolden/unp/watcher"
)
func init() {
+ var out io.Writer
+ if stderr := os.Stderr; isatty.IsTerminal(stderr.Fd()) {
+ out = logutil.NewUniqueWriter(stderr)
+ } else {
+ out = stderr
+ }
log.SetPrefix("unp: ")
log.SetFlags(0)
+ log.SetOutput(out)
}
func main() {
diff --git a/go.mod b/go.mod
index 05b96fd..0ba9196 100644
--- a/go.mod
+++ b/go.mod
@@ -3,9 +3,9 @@ module github.com/mpolden/unp
go 1.17
require (
+ github.com/mattn/go-isatty v0.0.14
github.com/mpolden/sfv v0.9.0
github.com/nwaples/rardecode v1.1.0
github.com/rjeczalik/notify v0.9.2
+ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
-
-require golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 // indirect
diff --git a/go.sum b/go.sum
index b558f35..456e6e5 100644
--- a/go.sum
+++ b/go.sum
@@ -1,8 +1,11 @@
+github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
+github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mpolden/sfv v0.9.0 h1:POHC8Js30xxOgMvgNLUEkJZh2fhOtx5NwK1pj7g9VvQ=
github.com/mpolden/sfv v0.9.0/go.mod h1:EymWriacbRB9ZKQ21Vj+ahcIV8aq8G0FNluX6UNCcVk=
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
-golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 h1:bit1t3mgdR35yN0cX0G8orgLtOuyL9Wqxa1mccLB0ig=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
+golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
diff --git a/logutil/logutil.go b/logutil/logutil.go
new file mode 100644
index 0000000..467e57d
--- /dev/null
+++ b/logutil/logutil.go
@@ -0,0 +1,40 @@
+package logutil
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+)
+
+const (
+ cursorUp = "\033[1A"
+ eraseEOL = "\033[K"
+)
+
+type UniqueWriter struct {
+ w io.Writer
+ prev []byte
+ count int64
+}
+
+func NewUniqueWriter(w io.Writer) *UniqueWriter { return &UniqueWriter{w: w} }
+
+func (uw *UniqueWriter) Write(p []byte) (n int, err error) {
+ if len(p) == 0 {
+ return 0, nil
+ }
+ if bytes.Equal(uw.prev, p) {
+ uw.count++
+ if p[len(p)-1] == '\n' {
+ p = p[:len(p)-1]
+ }
+ space := ""
+ if len(p) > 0 {
+ space = " "
+ }
+ return fmt.Fprintf(uw.w, "%s%s%s%s[repeated %d times]\n", cursorUp, eraseEOL, p, space, uw.count)
+ }
+ uw.prev = p
+ uw.count = 1
+ return uw.w.Write(p)
+}
diff --git a/logutil/logutil_test.go b/logutil/logutil_test.go
new file mode 100644
index 0000000..29f8849
--- /dev/null
+++ b/logutil/logutil_test.go
@@ -0,0 +1,25 @@
+package logutil
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestWrite(t *testing.T) {
+ var sb strings.Builder
+ w := NewUniqueWriter(&sb)
+
+ w.Write([]byte("l1\n"))
+ w.Write([]byte("l1\n"))
+ w.Write([]byte("l2\n"))
+ w.Write([]byte("\n"))
+ w.Write([]byte("\n"))
+
+ want := "l1\n" +
+ "\033[1A\033[Kl1 [repeated 2 times]\n" +
+ "l2\n" +
+ "\n\033[1A\033[K[repeated 2 times]\n"
+ if got := sb.String(); got != want {
+ t.Errorf("got %q, want %q", got, want)
+ }
+}