1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-19 19:25:24 +00: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

@ -106,11 +106,12 @@ func Bookmarks() {
// Render and display
content, links := renderer.RenderGemini(rawContent, textWidth(), leftMargin())
page := structs.Page{
Raw: rawContent,
Content: content,
Links: links,
Url: "about:bookmarks",
Width: termW,
Raw: rawContent,
Content: content,
Links: links,
Url: "about:bookmarks",
Width: termW,
Mediatype: structs.TextGemini,
}
setPage(&page)
}

View File

@ -203,11 +203,12 @@ func Init() {
// Render the default new tab content ONCE and store it for later
renderedNewTabContent, newTabLinks = renderer.RenderGemini(newTabContent, textWidth(), leftMargin())
newTabPage = &structs.Page{
Raw: newTabContent,
Content: renderedNewTabContent,
Links: newTabLinks,
Url: "about:newtab",
Width: -1, // Force reformatting on first display
Raw: newTabContent,
Content: renderedNewTabContent,
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
}
// Links are not recorded because they won't change
rendered, _ := renderer.RenderGemini(p.Raw, textWidth(), leftMargin())
var rendered string
if p.Mediatype == structs.TextGemini {
// Links are not recorded because they won't change
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,26 +80,20 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs
if mediatype == "text/gemini" {
rendered, links := RenderGemini(utfText, width, leftMargin)
return &structs.Page{
Url: url,
Raw: utfText,
Content: rendered,
Links: links,
Mediatype: structs.TextGemini,
Url: url,
Raw: utfText,
Content: rendered,
Links: links,
}, 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{
Url: url,
Raw: utfText,
Content: shifted,
Links: []string{},
Mediatype: structs.TextPlain,
Url: url,
Raw: utfText,
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,14 +1,22 @@
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
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.
Row int // Scroll position
Column int // ditto
Width int // The width of the terminal at the time when the Content was set. This is to know when reformatting should happen.
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.
Row int // Scroll position
Column int // ditto
Width int // The width of the terminal at the time when the Content was set. This is to know when reformatting should happen.
}
// Size returns an approx. size of a Page in bytes.