Skip to content

Commit

Permalink
add truncate wide lines
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Apr 6, 2019
1 parent 9084f27 commit 45a8026
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion change_logs/release_v0.1.3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="https://raw.githubusercontent.com/derailed/popeye/master/assets/popeye.png" align="right" width="200" height="auto"/>

# Release v0.1.2
# Release v0.1.3

## Notes

Expand Down
23 changes: 23 additions & 0 deletions change_logs/release_v0.1.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<img src="https://raw.githubusercontent.com/derailed/popeye/master/assets/popeye.png" align="right" width="200" height="auto"/>

# Release v0.1.4

## Notes

Thank you so much for your support and suggestions to make Popeye better!!

Also if you dig this tool, please make some noise on social! [@kitesurfer](https://twitter.com/kitesurfer)

---

## Change Logs

---

## Resolved Bugs

+ [Issue #4](https://github.com/derailed/popeye/issues/4)

---

<img src="https://raw.githubusercontent.com/derailed/popeye/master/assets/imhotep_logo.png" width="32" height="auto"/> © 2019 Imhotep Software LLC. All materials licensed under [Apache v2.0](http://www.apache.org/licenses/LICENSE-2.0)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mattn/go-runewidth v0.0.4
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/onsi/gomega v1.4.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
2 changes: 2 additions & 0 deletions internal/report/human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func TestTitleForRes(t *testing.T) {
"no": "NODES",
"svc": "SERVICES",
"blee": "BLEES",
"sa": "SERVICEACCOUNTS",
"ns": "NAMESPACES",
}

for k, e := range uu {
Expand Down
22 changes: 14 additions & 8 deletions internal/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/derailed/popeye/internal/linter"
runewidth "github.com/mattn/go-runewidth"
)

const (
Expand Down Expand Up @@ -37,11 +38,12 @@ func Close(w io.Writer) {
func Error(w io.Writer, msg string, err error) {
fmt.Fprintln(w)
msg = msg + ": " + err.Error()
buff := make([]string, 0, len(msg)%reportWidth)

width := reportWidth - 3
for i := 0; len(msg) > width; i += width {
buff = append(buff, msg[i:i+width])
msg = msg[i+width:]
buff := make([]string, 0, len(msg)/width)
for i := 0; len(msg) > width; i++ {
buff = append(buff, msg[:width])
msg = msg[width:]
}
buff = append(buff, msg)
fmt.Fprintf(w, "💥 "+Colorize(strings.Join(buff, "\n"), ColorRed))
Expand Down Expand Up @@ -77,10 +79,9 @@ func Write(w io.Writer, l linter.Level, indent int, msg string) {
spacer := strings.Repeat(" ", tabSize*indent)

if indent == 1 {
dots := reportWidth - len(msg) - tabSize*indent - 3
if dots < 0 {
dots = 1
}
maxWidth := reportWidth - tabSize*indent - 3
msg = truncate(msg, maxWidth)
dots := maxWidth - len(msg)
msg = Colorize(msg, colorForLevel(l)) + Colorize(strings.Repeat(".", dots), ColorGray)
fmt.Fprintf(w, "%s· %s%s\n", spacer, msg, EmojiForLevel(l))
return
Expand All @@ -89,3 +90,8 @@ func Write(w io.Writer, l linter.Level, indent int, msg string) {
msg = Colorize(msg, colorForLevel(l))
fmt.Fprintf(w, "%s%s %s\n", spacer, EmojiForLevel(l), msg)
}

// Truncate a string to the given l and suffix ellipsis if needed.
func truncate(str string, width int) string {
return runewidth.Truncate(str, width, "...")
}
39 changes: 35 additions & 4 deletions internal/report/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,26 @@ func TestComment(t *testing.T) {
}

func TestError(t *testing.T) {
w := bytes.NewBufferString("")
uu := []struct {
err error
e string
}{
{
fmt.Errorf("crapola"),
"\n💥 \x1b[38;5;196;mblee: crapola\x1b[0m\n",
},
{
fmt.Errorf(strings.Repeat("#", 200)),
"\n💥 \x1b[38;5;196;mblee: #######################################################################\n#############################################################################\n####################################################\x1b[0m\n",
},
}

Error(w, "blee", fmt.Errorf("crapola"))
for _, u := range uu {
w := bytes.NewBufferString("")
Error(w, "blee", u.err)

assert.Equal(t, "\n💥 \x1b[38;5;196;mblee: crapola\x1b[0m\n", w.String())
assert.Equal(t, u.e, w.String())
}
}

func TestWrite(t *testing.T) {
Expand All @@ -41,7 +56,7 @@ func TestWrite(t *testing.T) {
{
strings.Repeat("#", reportWidth),
1,
" · \x1b[38;5;155;m" + strings.Repeat("#", reportWidth) + "\x1b[0m\x1b[38;5;250;m.\x1b[0m✅\n",
" · \x1b[38;5;155;m" + strings.Repeat("#", reportWidth-8) + "...\x1b[0m\x1b[38;5;250;m\x1b[0m✅\n",
},
{
"Yo mama",
Expand Down Expand Up @@ -123,3 +138,19 @@ func TestOpenClose(t *testing.T) {

assert.Equal(t, "\n\x1b[38;5;75;mfred\x1b[0m\n\x1b[38;5;75;m┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅\x1b[0m\n\n", w.String())
}

func TestTruncate(t *testing.T) {
uu := []struct {
s string
l int
e string
}{
{"fred", 3, "..."},
{"freddy", 5, "fr..."},
{"fred", 10, "fred"},
}

for _, u := range uu {
assert.Equal(t, u.e, truncate(u.s, u.l))
}
}

0 comments on commit 45a8026

Please sign in to comment.