mirror of
https://github.com/makew0rld/amfora.git
synced 2024-09-25 22:55:55 -04: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)
|
- Wrapped list items are indented to stay behind the bullet (#35)
|
||||||
- Certificate expiry date is stored when the cert IDs match (#39)
|
- Certificate expiry date is stored when the cert IDs match (#39)
|
||||||
- What link was selected is remembered as you browse through history
|
- 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
|
### Changed
|
||||||
- Pages are rewrapped dynamically, whenever the terminal size changes (#33)
|
- 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())
|
rendered, _ = renderer.RenderGemini(p.Raw, textWidth(), leftMargin())
|
||||||
} else if p.Mediatype == structs.TextPlain {
|
} else if p.Mediatype == structs.TextPlain {
|
||||||
rendered = renderer.RenderPlainText(p.Raw, leftMargin())
|
rendered = renderer.RenderPlainText(p.Raw, leftMargin())
|
||||||
|
} else if p.Mediatype == structs.TextAnsi {
|
||||||
|
rendered = renderer.RenderANSI(p.Raw, leftMargin())
|
||||||
} else {
|
} else {
|
||||||
// Rendering this type is not implemented
|
// Rendering this type is not implemented
|
||||||
return
|
return
|
||||||
|
@ -109,14 +109,25 @@ func MakePage(url string, res *gemini.Response, width, leftMargin int) (*structs
|
|||||||
Links: links,
|
Links: links,
|
||||||
}, nil
|
}, nil
|
||||||
} else if strings.HasPrefix(mediatype, "text/") {
|
} else if strings.HasPrefix(mediatype, "text/") {
|
||||||
// Treated as plaintext
|
if mediatype == "text/x-ansi" || strings.HasSuffix(url, ".ans") {
|
||||||
return &structs.Page{
|
// ANSI
|
||||||
Mediatype: structs.TextPlain,
|
return &structs.Page{
|
||||||
Url: url,
|
Mediatype: structs.TextAnsi,
|
||||||
Raw: utfText,
|
Url: url,
|
||||||
Content: RenderPlainText(utfText, leftMargin),
|
Raw: utfText,
|
||||||
Links: []string{},
|
Content: RenderANSI(utfText, leftMargin),
|
||||||
}, nil
|
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")
|
return nil, errors.New("displayable mediatype is not handled in the code, implementation error")
|
||||||
|
@ -13,6 +13,21 @@ import (
|
|||||||
"gitlab.com/tslocum/cview"
|
"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.
|
// RenderPlainText should be used to format plain text pages.
|
||||||
func RenderPlainText(s string, leftMargin int) string {
|
func RenderPlainText(s string, leftMargin int) string {
|
||||||
var shifted string
|
var shifted string
|
||||||
|
@ -5,6 +5,7 @@ type Mediatype string
|
|||||||
const (
|
const (
|
||||||
TextGemini Mediatype = "text/gemini"
|
TextGemini Mediatype = "text/gemini"
|
||||||
TextPlain Mediatype = "text/plain"
|
TextPlain Mediatype = "text/plain"
|
||||||
|
TextAnsi Mediatype = "text/x-ansi"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PageMode int
|
type PageMode int
|
||||||
|
Loading…
Reference in New Issue
Block a user