1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05:00

🐛 Handle plaintext for dynamic wrapping - fixes #33

This commit is contained in:
makeworld 2020-07-03 17:28:56 -04:00
parent 6a708d339d
commit 92eb544b0a
6 changed files with 58 additions and 35 deletions

View File

@ -111,6 +111,7 @@ func Bookmarks() {
Links: links,
Url: "about:bookmarks",
Width: termW,
Mediatype: structs.TextGemini,
}
setPage(&page)
}

View File

@ -208,6 +208,7 @@ func Init() {
Links: newTabLinks,
Url: "about:newtab",
Width: -1, // Force reformatting on first display
Mediatype: structs.TextGemini,
}
modalInit()

View File

@ -168,8 +168,17 @@ func reformatPage(p *structs.Page) {
// No changes to make
return
}
var rendered string
if p.Mediatype == structs.TextGemini {
// Links are not recorded because they won't change
rendered, _ := renderer.RenderGemini(p.Raw, textWidth(), leftMargin())
rendered, _ = renderer.RenderGemini(p.Raw, textWidth(), leftMargin())
} else if p.Mediatype == structs.TextPlain {
rendered = renderer.RenderPlainText(p.Raw, leftMargin())
} else {
// Rendering this type is not implemented
return
}
p.Content = rendered
p.Width = termW
}

View File

@ -80,6 +80,7 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs
if mediatype == "text/gemini" {
rendered, links := RenderGemini(utfText, width, leftMargin)
return &structs.Page{
Mediatype: structs.TextGemini,
Url: url,
Raw: utfText,
Content: rendered,
@ -87,18 +88,11 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs
}, nil
} else if strings.HasPrefix(mediatype, "text/") {
// Treated as plaintext
// Add left margin
var shifted string
lines := strings.Split(utfText, "\n")
for i := range lines {
shifted += strings.Repeat(" ", leftMargin) + lines[i] + "\n"
}
return &structs.Page{
Mediatype: structs.TextPlain,
Url: url,
Raw: utfText,
Content: shifted,
Content: RenderPlainText(utfText, leftMargin),
Links: []string{},
}, nil
}

View File

@ -13,6 +13,16 @@ import (
"gitlab.com/tslocum/cview"
)
// RenderPlainText should be used to format plain text pages.
func RenderPlainText(s string, leftMargin int) string {
var shifted string
lines := strings.Split(cview.Escape(s), "\n")
for i := range lines {
shifted += strings.Repeat(" ", leftMargin) + lines[i] + "\n"
}
return shifted
}
// wrapLine wraps a line to the provided width, and adds the provided prefix and suffix to each wrapped line.
// It recovers from wrapping panics and should never cause a panic.
// It returns a slice of lines, without newlines at the end.

View File

@ -1,8 +1,16 @@
package structs
type Mediatype string
const (
TextGemini Mediatype = "text/gemini"
TextPlain Mediatype = "text/plain"
)
// Page is for storing UTF-8 text/gemini pages, as well as text/plain pages.
type Page struct {
Url string
Mediatype Mediatype
Raw string // The raw response, as received over the network
Content string // The processed content, NOT raw. Uses cview colour tags. All link/link texts must have region tags. It will also have a left margin.
Links []string // URLs, for each region in the content.