aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2020-05-09 14:52:06 -0700
committerEric Biggers <ebiggers@google.com>2020-05-09 15:21:31 -0700
commit1a47ab1e565f7052e34b0677a1df6789e6ecf3a9 (patch)
treea00566615030e2b9efbfa2e1e87cfefecbbea7da
parent1cdefc21b8b07aad7aafeefd05d3124cf93b9216 (diff)
cmd/fscrypt: make wrapText() support code blocks
Allow the input text to contain "code blocks" denoted by lines beginning with ">", e.g.: Foo bar baz: > echo foo > echo bar Instead of squashing these lines together, preserve the line breaks between them and add indentation, e.g.: Foo bar baz: echo foo echo bar
-rw-r--r--cmd/fscrypt/format.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/cmd/fscrypt/format.go b/cmd/fscrypt/format.go
index cc268aa..576d025 100644
--- a/cmd/fscrypt/format.go
+++ b/cmd/fscrypt/format.go
@@ -121,7 +121,8 @@ func longDisplay(f prettyFlag, defaultString ...string) string {
// Takes an input string text, and wraps the text so that each line begins with
// padding spaces (except for the first line), ends with a newline (except the
// last line), and each line has length less than lineLength. If the text
-// contains a word which is too long, that word gets its own line.
+// contains a word which is too long, that word gets its own line. Paragraphs
+// and "code blocks" are preserved.
func wrapText(text string, padding int) string {
// We use a buffer to format the wrapped text so we get O(n) runtime
var buffer bytes.Buffer
@@ -141,10 +142,18 @@ func wrapText(text string, padding int) string {
continue
}
+ codeBlock := (words[0] == ">")
+ if codeBlock {
+ words[0] = " "
+ if filled != 0 {
+ buffer.WriteString("\n")
+ filled = 0
+ }
+ }
for _, word := range words {
wordLen := utf8.RuneCountInString(word)
// Write a newline if needed.
- if filled != 0 && filled+1+wordLen > lineLength {
+ if filled != 0 && filled+1+wordLen > lineLength && !codeBlock {
buffer.WriteString("\n")
filled = 0
}