Improve text wrapping
authorMikael Berthe <mikael@lilotux.net>
Thu, 11 May 2017 23:57:17 +0200
changeset 124 20d514540f37
parent 123 24ab59ba57b2
child 125 d436b88d137b
Improve text wrapping
.travis.yml
printer/templateprinter.go
templates/ansi-account.tmpl
templates/ansi-notification.tmpl
templates/ansi-status.tmpl
templates/themes/ansi/account.tmpl
templates/themes/ansi/notification.tmpl
templates/themes/ansi/status.tmpl
--- 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
--- 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")
+}
--- 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 . "<p></p>"}}
-  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}}
--- 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}}
--- 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}}
--- 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 . "<p></p>"}}
-  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}}
--- 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}}
--- 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}}