mirror of
https://github.com/makeworld-the-better-one/amfora.git
synced 2024-09-11 14:55:51 +03:00
✨ Support text/x-ansi - fixes #45
This commit is contained in:
parent
d5a3f63b45
commit
74d5edd695
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
|
@ -5,6 +5,7 @@ type Mediatype string
|
||||
const (
|
||||
TextGemini Mediatype = "text/gemini"
|
||||
TextPlain Mediatype = "text/plain"
|
||||
TextAnsi Mediatype = "text/x-ansi"
|
||||
)
|
||||
|
||||
type PageMode int
|
||||
|
Loading…
Reference in New Issue
Block a user