bombadillo/page.go

94 lines
2.1 KiB
Go
Raw Normal View History

package main
2019-09-12 05:53:36 +00:00
import (
"strings"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
type Page struct {
2019-09-12 05:53:36 +00:00
WrappedContent []string
RawContent string
Links []string
Location Url
ScrollPosition int
}
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
2019-09-12 05:53:36 +00:00
func (p *Page) ScrollPositionRange(termHeight int) (int, int) {
termHeight -= 3
if len(p.WrappedContent) - p.ScrollPosition < termHeight {
p.ScrollPosition = len(p.WrappedContent) - termHeight
}
if p.ScrollPosition < 0 {
p.ScrollPosition = 0
}
var end int
if len(p.WrappedContent) < termHeight {
end = len(p.WrappedContent)
} else {
end = p.ScrollPosition + termHeight
}
2019-09-12 05:53:36 +00:00
return p.ScrollPosition, end
}
// Performs a hard wrap to the requested
// width and updates the WrappedContent
// of the Page struct width a string slice
// of the wrapped data
2019-09-12 05:53:36 +00:00
func (p *Page) WrapContent(width int) {
counter := 0
var content strings.Builder
content.Grow(len(p.RawContent))
for _, ch := range p.RawContent {
if ch == '\n' {
content.WriteRune(ch)
counter = 0
} else if ch == '\t' {
if counter + 4 < width {
content.WriteString(" ")
counter += 4
} else {
content.WriteRune('\n')
counter = 0
}
} else if ch == '\r' {
// This handles non-linux line endings...
// to some degree...
continue
2019-09-12 05:53:36 +00:00
} else {
if counter < width {
content.WriteRune(ch)
counter++
} else {
content.WriteRune('\n')
counter = 0
if p.Location.Mime == "1" {
spacer := " "
content.WriteString(spacer)
counter += len(spacer)
2019-09-12 05:53:36 +00:00
}
content.WriteRune(ch)
2019-09-12 05:53:36 +00:00
}
}
}
p.WrappedContent = strings.Split(content.String(), "\n")
2019-09-12 05:53:36 +00:00
}
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
2019-09-12 05:53:36 +00:00
func MakePage(url Url, content string, links []string) Page {
p := Page{make([]string, 0), content, links, url, 0}
return p
}