-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatText.go
145 lines (138 loc) · 4.16 KB
/
formatText.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// formatText.go
package strings
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// An unwanted behavior may occur on string where word's length > max...
func FormatTextQuoteBlankLines(str string) (out string) {
outStrings := strings.Split(str, GetTextEOL([]byte(str)))
for _, line := range outStrings {
if len(line) != 0 {
out += strings.TrimSuffix(strings.TrimPrefix(strconv.Quote(line+"\n"), `"`), `"`)
} else {
out += `\n`
}
}
return `"` + out + `"`
}
// FormatTextParagraphFormatting:
func FormatTextParagraphFormatting(str string, max int, truncateWordOverMaxSize bool, indentFirstLinetr ...string) string {
var tmpParag, tmpFinal, indent string
eol := GetTextEOL([]byte(str))
var addLine = func() {
tmpFinal += FormatText(tmpParag, max, truncateWordOverMaxSize, indent)
}
if len(indentFirstLinetr) != 0 {
indent = indentFirstLinetr[0]
}
lines := strings.Split(str, eol)
for _, line := range lines {
if len(line) != 0 {
tmpParag += line
} else {
addLine()
tmpFinal += eol + eol
tmpParag = tmpParag[:0]
}
}
addLine()
return strings.TrimSuffix(tmpFinal, eol)
}
// FormatText: Format words text to fit (column/windows with limited width) "max"
// chars. An unwanted behavior may occur on string where word's length > max...
func FormatText(str string, max int, truncateWordOverMaxSize bool, indenT ...string) string {
var indent string
if len(indenT) != 0 {
indent = indenT[0]
}
if len(str) > 0 {
eol := GetTextEOL([]byte(str))
var tmpLines, tmpWordTooLong []string
space := regexp.MustCompile(`[[:space:]]`)
var outText []string
var countChar, length int
// in case where string does not contain any [[:space:]] char
var cutLongString = func(inStr string, inMax int, truncate bool) (outSliceText []string) {
if !truncate {
length = len(inStr)
for count := 0; count < length; count = count + inMax {
if count+inMax < length {
outSliceText = append(outSliceText, inStr[count:count+inMax])
} else {
outSliceText = append(outSliceText, inStr[count:length])
}
}
} else {
outSliceText = append(outSliceText, TruncateString(inStr, "...", inMax, 1))
}
return outSliceText
}
text := space.Split(str, -1) // Split str at each blank char
for idxWord := 0; idxWord < len(text); idxWord++ {
length = len(text[idxWord]) + 1
if length >= max {
tmpWordTooLong = cutLongString(text[idxWord], max-1, truncateWordOverMaxSize)
text = append(text[:idxWord], text[idxWord+1:]...) // Remove slice entry
// Insert slices entries
text = append(text[:idxWord], append(tmpWordTooLong, text[idxWord:]...)...)
length = len(text[idxWord]) // Calculate new length
}
if countChar+length <= max {
tmpLines = append(tmpLines, text[idxWord])
countChar += length
} else {
outText = append(outText, indent+strings.Join(tmpLines, " "))
tmpLines = tmpLines[:0] // Clear slice
countChar = 0
idxWord--
}
}
// Get the rest of the text.
outText = append(outText, indent+strings.Join(tmpLines, " "))
return strings.Join(outText, eol)
}
return ""
}
// TruncateString: Reduce string length for display (prefix is separator like: "...",
// option=0 -> put separator at the begining of output string. Option=1 -> center,
// is where separation is placed. option=2 -> line feed, trunc the whole string
// using LF without shorting it. Max, is max char length of the output string.
func TruncateString(inString, prefix string, max, option int) string {
var center, cutAt bool
var outText string
switch option {
case 1:
center = true
cutAt = false
max = max - len(prefix)
case 2:
center = false
cutAt = true
default:
center = false
cutAt = false
max = max - len(prefix)
}
length := len(inString)
if length > max {
if cutAt {
for count := 0; count < length; count = count + max {
if count+max < length {
outText += fmt.Sprintln(inString[count : count+max])
} else {
outText += fmt.Sprintln(inString[count:length])
}
}
return outText
} else if center {
midLength := max / 2
inString = inString[:midLength] + prefix + inString[length-midLength-1:]
} else {
inString = prefix + inString[length-max:]
}
}
return inString
}