~sircmpwn/aerc

08574104bc9ba550153888cfdc4ebf28d500ce56 — Drew DeVault 1 year, 2 months ago cb7d7a0
Correct capitalization in quoted_reply
3 files changed, 40 insertions(+), 16 deletions(-)

M doc/aerc-templates.7.scd
M lib/templates/template.go
M templates/quoted_reply
M doc/aerc-templates.7.scd => doc/aerc-templates.7.scd +6 -2
@@ 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)

M lib/templates/template.go => lib/templates/template.go +32 -12
@@ 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{

M templates/quoted_reply => templates/quoted_reply +2 -2
@@ 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 }}