Skip to content

Commit

Permalink
🐛 Support ANSI color codes in preformatted blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
makew0rld committed Nov 5, 2020
1 parent b7efbda commit 819023d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support paths with spaces in HTTP browser config setting (#77)
- Clicking "Change" on an existing bookmark without changing the text no longer removes it (#91)
- Display HTTP Error if "Open In Portal" fails (#81)
- Support ANSI color codes again, but only in preformatted blocks (#59)


## [v1.5.0] - 2020-09-01
Expand Down
2 changes: 1 addition & 1 deletion config/default.go
Expand Up @@ -43,7 +43,7 @@ search = "gemini://gus.guru/search"
# Whether colors will be used in the terminal
color = true
# Whether ANSI codes from the page content should be rendered
# Whether ANSI color codes from the page content should be rendered
ansi = true
# Whether to replace list asterisks with unicode bullets
Expand Down
2 changes: 1 addition & 1 deletion default-config.toml
Expand Up @@ -40,7 +40,7 @@ search = "gemini://gus.guru/search"
# Whether colors will be used in the terminal
color = true

# Whether ANSI codes from the page content should be rendered
# Whether ANSI color codes from the page content should be rendered
ansi = true

# Whether to replace list asterisks with unicode bullets
Expand Down
37 changes: 26 additions & 11 deletions renderer/renderer.go
Expand Up @@ -283,11 +283,6 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
// If it's not a gemini:// page, set this to true.
func RenderGemini(s string, width, leftMargin int, proxied bool) (string, []string) {
s = cview.Escape(s)
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
s = cview.TranslateANSI(s)
} else {
s = ansiRegex.ReplaceAllString(s, "")
}

lines := strings.Split(s, "\n")

Expand All @@ -302,13 +297,22 @@ func RenderGemini(s string, width, leftMargin int, proxied bool) (string, []stri
if pre {
// In a preformatted block, so add the text as is
// Don't add the current line with backticks
rendered += tagLines(
buf,
fmt.Sprintf("[%s]", config.GetColorString("preformatted_text")),
"[-]",
)

// Support ANSI color codes in preformatted blocks - see #59
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
buf = cview.TranslateANSI(buf)
} else {
buf = ansiRegex.ReplaceAllString(buf, "")
}

rendered += fmt.Sprintf("[%s]", config.GetColorString("preformatted_text")) +
buf + "[-]"
} else {
// Not preformatted, regular text

// ANSI not allowed in regular text - see #59
buf = ansiRegex.ReplaceAllString(buf, "")

ren, lks := convertRegularGemini(buf, len(links), width, proxied)
links = append(links, lks...)
rendered += ren
Expand All @@ -323,10 +327,21 @@ func RenderGemini(s string, width, leftMargin int, proxied bool) (string, []stri
// Gone through all the lines, but there still is likely a block in the buffer
if pre {
// File ended without closing the preformatted block
rendered += buf
// Same code as in the loop above

if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
buf = cview.TranslateANSI(buf)
} else {
buf = ansiRegex.ReplaceAllString(buf, "")
}
rendered += fmt.Sprintf("[%s]", config.GetColorString("preformatted_text")) +
buf + "[-]"
} else {
// Not preformatted, regular text
// Same code as in the loop above

buf = ansiRegex.ReplaceAllString(buf, "")

ren, lks := convertRegularGemini(buf, len(links), width, proxied)
links = append(links, lks...)
rendered += ren
Expand Down

0 comments on commit 819023d

Please sign in to comment.