Skip to content

Commit

Permalink
✨ Support text/x-ansi - fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
makew0rld committed Jul 10, 2020
1 parent d5a3f63 commit 74d5edd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions display/private.go
Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions renderer/page.go
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions renderer/renderer.go
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions structs/structs.go
Expand Up @@ -5,6 +5,7 @@ type Mediatype string
const (
TextGemini Mediatype = "text/gemini"
TextPlain Mediatype = "text/plain"
TextAnsi Mediatype = "text/x-ansi"
)

type PageMode int
Expand Down

0 comments on commit 74d5edd

Please sign in to comment.