# HG changeset patch # User Mikael Berthe # Date 1494539837 -7200 # Node ID 20d514540f3766b909b69c4ee6459dadcac2e256 # Parent 24ab59ba57b2ea89b0608b4bb15e871a2254c9f1 Improve text wrapping diff -r 24ab59ba57b2 -r 20d514540f37 .travis.yml --- a/.travis.yml Thu May 11 21:03:45 2017 +0200 +++ b/.travis.yml Thu May 11 23:57:17 2017 +0200 @@ -11,12 +11,12 @@ only: - master install: -- go get go/doc - go get github.com/spf13/cobra - go get github.com/spf13/viper - go get github.com/McKael/madon - go get github.com/ghodss/yaml - go get github.com/pkg/errors - go get github.com/jaytaylor/html2text +- go get github.com/kr/text - go get github.com/m0t0k1ch1/gomif - go get github.com/mattn/go-isatty diff -r 24ab59ba57b2 -r 20d514540f37 printer/templateprinter.go --- a/printer/templateprinter.go Thu May 11 21:03:45 2017 +0200 +++ b/printer/templateprinter.go Thu May 11 23:57:17 2017 +0200 @@ -6,10 +6,8 @@ package printer import ( - "bytes" "encoding/json" "fmt" - "go/doc" "io" "os" "reflect" @@ -17,6 +15,7 @@ "text/template" "time" + "github.com/kr/text" "github.com/m0t0k1ch1/gomif" "github.com/mattn/go-isatty" @@ -158,20 +157,6 @@ return colors.ANSICodeString(desc) } -// Wrap text with indent prefix -// Currently paragraph-based (cf. doc.ToText), which is not very good for -// our use case since CRs are ignored. -func wrap(indent string, lineLength int, text string) string { - var buf bytes.Buffer - - width := lineLength - len(indent) - if width < 10 { - width = 10 - } - doc.ToText(&buf, text, indent, indent+" ", width) - return buf.String() -} - // Parse datetime string from RFC3339 (default format in templates because // of implicit conversion to string) and return a local time. func dateToLocal(s string) (time.Time, error) { @@ -181,3 +166,19 @@ } return t.Local(), err } + +// Wrap text with indent prefix +func wrap(indent string, lineLength int, txt string) string { + width := lineLength - len(indent) + if width < 10 { + width = 10 + } + + lines := strings.SplitAfter(txt, "\n") + var out []string + for _, l := range lines { + l = strings.TrimLeft(l, " ") + out = append(out, text.Indent(text.Wrap(l, width), indent)) + } + return strings.Join(out, "\n") +} diff -r 24ab59ba57b2 -r 20d514540f37 templates/ansi-account.tmpl --- a/templates/ansi-account.tmpl Thu May 11 21:03:45 2017 +0200 +++ b/templates/ansi-account.tmpl Thu May 11 23:57:17 2017 +0200 @@ -7,6 +7,6 @@ Followers: {{.followers_count}} Following: {{.following_count}} {{- with .note}}{{if ne . "

"}} - Note: {{color "yellow"}}{{. | fromhtml | wrap " " 80 | trim}}{{color "reset"}}{{end}}{{end}} + Note: {{color "yellow"}}{{. | fromhtml | wrap " " 79 | trim}}{{color "reset"}}{{end}}{{end}} {{- if eq .locked true}} Locked: true{{end}} diff -r 24ab59ba57b2 -r 20d514540f37 templates/ansi-notification.tmpl --- a/templates/ansi-notification.tmpl Thu May 11 21:03:45 2017 +0200 +++ b/templates/ansi-notification.tmpl Thu May 11 23:57:17 2017 +0200 @@ -21,14 +21,14 @@ Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}}{{end}}{{else}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}}{{end}}{{else}} {{- if gt .in_reply_to_id 0.0}} Replying to: {{printf "%.0f" .in_reply_to_id}}{{end}} {{- if .sensitive}} Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}} {{- range .media_attachments}} - Attachment ID: {{printf "%.0f" .id}} {{- if .text_url}} diff -r 24ab59ba57b2 -r 20d514540f37 templates/ansi-status.tmpl --- a/templates/ansi-status.tmpl Thu May 11 21:03:45 2017 +0200 +++ b/templates/ansi-status.tmpl Thu May 11 23:57:17 2017 +0200 @@ -13,7 +13,7 @@ Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}} {{- range .media_attachments}} - Attachment ID: {{printf "%.0f" .id}} {{- if .text_url}} @@ -27,7 +27,7 @@ Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}} {{- range .media_attachments}} - Attachment ID: {{printf "%.0f" .id}} {{- if .text_url}} diff -r 24ab59ba57b2 -r 20d514540f37 templates/themes/ansi/account.tmpl --- a/templates/themes/ansi/account.tmpl Thu May 11 21:03:45 2017 +0200 +++ b/templates/themes/ansi/account.tmpl Thu May 11 23:57:17 2017 +0200 @@ -7,6 +7,6 @@ Followers: {{.followers_count}} Following: {{.following_count}} {{- with .note}}{{if ne . "

"}} - Note: {{color "yellow"}}{{. | fromhtml | wrap " " 80 | trim}}{{color "reset"}}{{end}}{{end}} + Note: {{color "yellow"}}{{. | fromhtml | wrap " " 79 | trim}}{{color "reset"}}{{end}}{{end}} {{- if eq .locked true}} Locked: true{{end}} diff -r 24ab59ba57b2 -r 20d514540f37 templates/themes/ansi/notification.tmpl --- a/templates/themes/ansi/notification.tmpl Thu May 11 21:03:45 2017 +0200 +++ b/templates/themes/ansi/notification.tmpl Thu May 11 23:57:17 2017 +0200 @@ -21,14 +21,14 @@ Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}}{{end}}{{else}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}}{{end}}{{else}} {{- if gt .in_reply_to_id 0.0}} Replying to: {{printf "%.0f" .in_reply_to_id}}{{end}} {{- if .sensitive}} Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}} {{- range .media_attachments}} - Attachment ID: {{printf "%.0f" .id}} {{- if .text_url}} diff -r 24ab59ba57b2 -r 20d514540f37 templates/themes/ansi/status.tmpl --- a/templates/themes/ansi/status.tmpl Thu May 11 21:03:45 2017 +0200 +++ b/templates/themes/ansi/status.tmpl Thu May 11 23:57:17 2017 +0200 @@ -13,7 +13,7 @@ Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}} {{- range .media_attachments}} - Attachment ID: {{printf "%.0f" .id}} {{- if .text_url}} @@ -27,7 +27,7 @@ Sensitive: true{{end}} {{- with .spoiler_text}} Spoiler: {{.}}{{end}} - Message: {{color "blue"}}{{.content | fromhtml | wrap " " 80 | trim}}{{color "reset"}} + Message: {{color "blue"}}{{.content | fromhtml | wrap " " 79 | trim}}{{color "reset"}} {{- range .media_attachments}} - Attachment ID: {{printf "%.0f" .id}} {{- if .text_url}}