forked from sloum/bombadillo
soft wrap between words
This commit is contained in:
parent
984fb4eb2f
commit
dac13e1669
22
page.go
22
page.go
|
@ -3,8 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"tildegit.org/sloum/bombadillo/tdiv"
|
"tildegit.org/sloum/bombadillo/tdiv"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
//------------------------------------------------\\
|
//------------------------------------------------\\
|
||||||
|
@ -86,7 +86,10 @@ func (p *Page) WrapContent(width, maxWidth int, color bool) {
|
||||||
} else if strings.HasSuffix(p.Location.Mime, "gemini") { //gemini document
|
} else if strings.HasSuffix(p.Location.Mime, "gemini") { //gemini document
|
||||||
spacer = " "
|
spacer = " "
|
||||||
}
|
}
|
||||||
for _, ch := range []rune(p.RawContent) {
|
|
||||||
|
runeArr := []rune(p.RawContent)
|
||||||
|
for i := 0; i < len(runeArr); i++ {
|
||||||
|
ch := runeArr[i]
|
||||||
if escape {
|
if escape {
|
||||||
if color {
|
if color {
|
||||||
esc.WriteRune(ch)
|
esc.WriteRune(ch)
|
||||||
|
@ -112,7 +115,7 @@ func (p *Page) WrapContent(width, maxWidth int, color bool) {
|
||||||
counter = 0
|
counter = 0
|
||||||
}
|
}
|
||||||
} else if ch == '\r' || ch == '\v' || ch == '\b' || ch == '\f' || ch == '\a' {
|
} else if ch == '\r' || ch == '\v' || ch == '\b' || ch == '\f' || ch == '\a' {
|
||||||
// Get rid of control characters we dont want
|
// Get rid of control characters we don't want
|
||||||
continue
|
continue
|
||||||
} else if ch == 27 {
|
} else if ch == 27 {
|
||||||
if p.Location.Scheme == "local" {
|
if p.Location.Scheme == "local" {
|
||||||
|
@ -128,7 +131,18 @@ func (p *Page) WrapContent(width, maxWidth int, color bool) {
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
if counter <= width {
|
// peek forward to see if we can render the word without going over
|
||||||
|
j := i
|
||||||
|
for ; j < len(runeArr) && !unicode.IsSpace(runeArr[j]); j++ {
|
||||||
|
if counter+(j-i) > width+1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we can render the rest of the word, write the next letter. else, skip to the next line.
|
||||||
|
// TODO(raidancampbell): optimize this to write out the whole word, this will involve referencing the
|
||||||
|
// above special cases
|
||||||
|
if counter+(j-i) <= width+1 && !(j == i && counter == width+1) {
|
||||||
content.WriteRune(ch)
|
content.WriteRune(ch)
|
||||||
counter++
|
counter++
|
||||||
} else {
|
} else {
|
||||||
|
|
17
page_test.go
17
page_test.go
|
@ -47,6 +47,23 @@ func Test_WrapContent_Wrapped_Line_Length(t *testing.T) {
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"multiple words should wrap at the right point",
|
||||||
|
"01 345 789 123456789 123456789 123456789 123456789\n",
|
||||||
|
[]string{
|
||||||
|
"01 345 789",
|
||||||
|
" 123456789",
|
||||||
|
" 123456789",
|
||||||
|
" 123456789",
|
||||||
|
" 123456789",
|
||||||
|
"",
|
||||||
|
},
|
||||||
|
args{
|
||||||
|
10,
|
||||||
|
10,
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"Long line wrapped to 10 columns",
|
"Long line wrapped to 10 columns",
|
||||||
"0123456789 123456789 123456789 123456789 123456789\n",
|
"0123456789 123456789 123456789 123456789 123456789\n",
|
||||||
|
|
Loading…
Reference in New Issue