🐛 Support ANSI color codes in preformatted blocks

This commit is contained in:
makeworld 2020-11-04 20:31:04 -05:00
parent b7efbdaeea
commit 819023daec
4 changed files with 29 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")
@ -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
@ -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