From 819023daec1b5f207c8713ecc884936808610c3f Mon Sep 17 00:00:00 2001 From: makeworld Date: Wed, 4 Nov 2020 20:31:04 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Support=20ANSI=20color=20codes?= =?UTF-8?q?=20in=20preformatted=20blocks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + config/default.go | 2 +- default-config.toml | 2 +- renderer/renderer.go | 37 ++++++++++++++++++++++++++----------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23724c14..e878a785 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.go b/config/default.go index 3ba1e7c9..63295390 100644 --- a/config/default.go +++ b/config/default.go @@ -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 diff --git a/default-config.toml b/default-config.toml index aeb22cff..9ac0d215 100644 --- a/default-config.toml +++ b/default-config.toml @@ -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 diff --git a/renderer/renderer.go b/renderer/renderer.go index 3b1e6eba..eabc715a 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -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