summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--doc/aerc-templates.7.scd8
-rw-r--r--lib/templates/template.go44
-rw-r--r--templates/quoted_reply4
3 files changed, 40 insertions, 16 deletions
diff --git a/doc/aerc-templates.7.scd b/doc/aerc-templates.7.scd
index 8504a60..3c8b123 100644
--- a/doc/aerc-templates.7.scd
+++ b/doc/aerc-templates.7.scd
@@ -72,12 +72,16 @@ available always.
 	{{wrapText .OriginalText 72}}
 	```
 
-	_quote_ function prepends each line with "> " and wraps the text to
-	72 characters pre line.
+	_quote_ function prepends each line with "> ".
 	```
 	{{quote .OriginalText}}
 	```
 
+	All of the above can be chained together if needed, for example
+	```
+	{{wrapText .OriginalText 72 | quote}}
+	```
+
 # SEE ALSO
 
 *aerc*(1) *aerc-config*(5)
diff --git a/lib/templates/template.go b/lib/templates/template.go
index e19b5ae..a2f35e2 100644
--- a/lib/templates/template.go
+++ b/lib/templates/template.go
@@ -70,39 +70,59 @@ func wrapLine(text string, lineWidth int) string {
 	if len(words) == 0 {
 		return text
 	}
-	wrapped := words[0]
-	spaceLeft := lineWidth - len(wrapped)
+	var wrapped strings.Builder
+	wrapped.WriteString(words[0])
+	spaceLeft := lineWidth - wrapped.Len()
 	for _, word := range words[1:] {
 		if len(word)+1 > spaceLeft {
-			wrapped += "\n" + word
+			wrapped.WriteRune('\n')
+			wrapped.WriteString(word)
 			spaceLeft = lineWidth - len(word)
 		} else {
-			wrapped += " " + word
+			wrapped.WriteRune(' ')
+			wrapped.WriteString(word)
 			spaceLeft -= 1 + len(word)
 		}
 	}
 
-	return wrapped
+	return wrapped.String()
 }
 
 func wrapText(text string, lineWidth int) string {
 	text = strings.ReplaceAll(text, "\r\n", "\n")
 	lines := strings.Split(text, "\n")
-	var wrapped string
+	var wrapped strings.Builder
 
 	for _, line := range lines {
-		wrapped += wrapLine(line, lineWidth) + "\n"
+		switch {
+		case line == "":
+			// deliberately left blank
+		case line[0] == '>':
+			// leave quoted text alone
+			wrapped.WriteString(line)
+		default:
+			wrapped.WriteString(wrapLine(line, lineWidth))
+		}
+		wrapped.WriteRune('\n')
 	}
-	return wrapped
+	return wrapped.String()
 }
 
-// Wraping lines at 70 so that with the "> " of the quote it is under 72
+// quote prepends "> " in front of every line in text
 func quote(text string) string {
 	text = strings.ReplaceAll(text, "\r\n", "\n")
+	lines := strings.Split(text, "\n")
+	var quoted strings.Builder
+	for _, line := range lines {
+		if line == "" {
+			quoted.WriteString(">\n")
+		}
+		quoted.WriteString("> ")
+		quoted.WriteString(line)
+		quoted.WriteRune('\n')
+	}
 
-	quoted := "> " + wrapText(text, 70)
-	quoted = strings.ReplaceAll(quoted, "\n", "\n> ")
-	return quoted
+	return quoted.String()
 }
 
 var templateFuncs = template.FuncMap{
diff --git a/templates/quoted_reply b/templates/quoted_reply
index ee4e1f7..f22c2f3 100644
--- a/templates/quoted_reply
+++ b/templates/quoted_reply
@@ -1,2 +1,2 @@
-on {{dateFormat .OriginalDate "Mon Jan 2, 2006 at 3:04 PM"}}, {{(index .OriginalFrom 0).Name}} wrote:
-{{quote .OriginalText}}
+On {{dateFormat .OriginalDate "Mon Jan 2, 2006 at 3:04 PM"}}, {{(index .OriginalFrom 0).Name}} wrote:
+{{wrapText .OriginalText 72 | quote }}