1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-19 19:25:24 +00:00

Support text/x-ansi - fixes #45

This commit is contained in:
makeworld 2020-07-10 17:45:14 -04:00
parent d5a3f63b45
commit 74d5edd695
5 changed files with 38 additions and 8 deletions

View File

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Wrapped list items are indented to stay behind the bullet (#35)
- Certificate expiry date is stored when the cert IDs match (#39)
- What link was selected is remembered as you browse through history
- Render ANSI codes in `text/x-ansi` pages, or text pages that end with `.ans`
### Changed
- Pages are rewrapped dynamically, whenever the terminal size changes (#33)

View File

@ -127,6 +127,8 @@ func reformatPage(p *structs.Page) {
rendered, _ = renderer.RenderGemini(p.Raw, textWidth(), leftMargin())
} else if p.Mediatype == structs.TextPlain {
rendered = renderer.RenderPlainText(p.Raw, leftMargin())
} else if p.Mediatype == structs.TextAnsi {
rendered = renderer.RenderANSI(p.Raw, leftMargin())
} else {
// Rendering this type is not implemented
return

View File

@ -109,14 +109,25 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs
Links: links,
}, nil
} else if strings.HasPrefix(mediatype, "text/") {
// Treated as plaintext
return &structs.Page{
Mediatype: structs.TextPlain,
Url: url,
Raw: utfText,
Content: RenderPlainText(utfText, leftMargin),
Links: []string{},
}, nil
if mediatype == "text/x-ansi" || strings.HasSuffix(url, ".ans") {
// ANSI
return &structs.Page{
Mediatype: structs.TextAnsi,
Url: url,
Raw: utfText,
Content: RenderANSI(utfText, leftMargin),
Links: []string{},
}, nil
} else {
// Treated as plaintext
return &structs.Page{
Mediatype: structs.TextPlain,
Url: url,
Raw: utfText,
Content: RenderPlainText(utfText, leftMargin),
Links: []string{},
}, nil
}
}
return nil, errors.New("displayable mediatype is not handled in the code, implementation error")

View File

@ -13,6 +13,21 @@ import (
"gitlab.com/tslocum/cview"
)
// RenderANSI renders plain text pages containing ANSI codes.
// Practically, it is used for the text/x-ansi.
func RenderANSI(s string, leftMargin int) string {
s = cview.Escape(s)
if viper.GetBool("a-general.color") {
s = cview.TranslateANSI(s)
}
var shifted string
lines := strings.Split(s, "\n")
for i := range lines {
shifted += strings.Repeat(" ", leftMargin) + lines[i] + "\n"
}
return shifted
}
// RenderPlainText should be used to format plain text pages.
func RenderPlainText(s string, leftMargin int) string {
var shifted string

View File

@ -5,6 +5,7 @@ type Mediatype string
const (
TextGemini Mediatype = "text/gemini"
TextPlain Mediatype = "text/plain"
TextAnsi Mediatype = "text/x-ansi"
)
type PageMode int