mirror of https://github.com/antonmedv/fx
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
1.4 KiB
Go
89 lines
1.4 KiB
Go
10 months ago
|
package model
|
||
1 year ago
|
|
||
|
import (
|
||
|
"unicode/utf8"
|
||
|
|
||
|
"github.com/mattn/go-runewidth"
|
||
|
)
|
||
|
|
||
1 year ago
|
func dropWrapAll(n *node) {
|
||
|
for n != nil {
|
||
|
if n.value != nil && n.value[0] == '"' {
|
||
|
n.dropChunks()
|
||
|
}
|
||
12 months ago
|
if n.isCollapsed() {
|
||
|
n = n.collapsed
|
||
|
} else {
|
||
|
n = n.next
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
func wrapAll(n *node, termWidth int) {
|
||
1 year ago
|
if termWidth <= 0 {
|
||
|
return
|
||
|
}
|
||
1 year ago
|
for n != nil {
|
||
|
if n.value != nil && n.value[0] == '"' {
|
||
|
n.dropChunks()
|
||
|
lines, count := doWrap(n, termWidth)
|
||
|
if count > 1 {
|
||
|
n.chunk = lines[0]
|
||
|
for i := 1; i < count; i++ {
|
||
|
child := &node{
|
||
|
directParent: n,
|
||
|
depth: n.depth,
|
||
|
chunk: lines[i],
|
||
|
}
|
||
|
if n.comma && i == count-1 {
|
||
|
child.comma = true
|
||
|
}
|
||
12 months ago
|
n.insertChunk(child)
|
||
1 year ago
|
}
|
||
|
}
|
||
|
}
|
||
12 months ago
|
if n.isCollapsed() {
|
||
|
n = n.collapsed
|
||
|
} else {
|
||
|
n = n.next
|
||
|
}
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
func doWrap(n *node, termWidth int) ([][]byte, int) {
|
||
|
lines := make([][]byte, 0, 1)
|
||
|
width := int(n.depth) * 2
|
||
|
|
||
|
if n.key != nil {
|
||
|
for _, ch := range string(n.key) {
|
||
|
width += runewidth.RuneWidth(ch)
|
||
|
}
|
||
|
width += 2 // for ": "
|
||
|
}
|
||
|
|
||
|
linesCount := 0
|
||
|
start, end := 0, 0
|
||
|
b := n.value
|
||
|
|
||
|
for len(b) > 0 {
|
||
|
r, size := utf8.DecodeRune(b)
|
||
|
w := runewidth.RuneWidth(r)
|
||
|
if width+w > termWidth {
|
||
1 year ago
|
lines = append(lines, n.value[start:end])
|
||
1 year ago
|
start = end
|
||
|
width = int(n.depth) * 2
|
||
|
linesCount++
|
||
|
}
|
||
1 year ago
|
width += w
|
||
1 year ago
|
end += size
|
||
|
b = b[size:]
|
||
|
}
|
||
|
|
||
|
if start < end {
|
||
|
lines = append(lines, n.value[start:])
|
||
|
linesCount++
|
||
|
}
|
||
|
|
||
|
return lines, linesCount
|
||
|
}
|