Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip ANSI codes from page based on config - fixes #79 #86

Merged
merged 6 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func Init() error {
viper.SetDefault("a-general.http", "default")
viper.SetDefault("a-general.search", "gus.guru/search")
viper.SetDefault("a-general.color", true)
viper.SetDefault("a-general.ansi", true)
viper.SetDefault("a-general.bullets", true)
viper.SetDefault("a-general.left_margin", 0.15)
viper.SetDefault("a-general.max_width", 100)
Expand Down
3 changes: 3 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ 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
ansi = true

# Whether to replace list asterisks with unicode bullets
bullets = true

Expand Down
3 changes: 3 additions & 0 deletions default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ 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
ansi = true

# Whether to replace list asterisks with unicode bullets
bullets = true

Expand Down
13 changes: 11 additions & 2 deletions renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package renderer
import (
"fmt"
urlPkg "net/url"
"regexp"
"strconv"
"strings"

Expand All @@ -15,12 +16,17 @@ import (
"gitlab.com/tslocum/cview"
)

// Regex for identifying ANSI color codes
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)

// 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") {
if viper.GetBool("a-general.color") && viper.GetBool("a-general.ansi") {
s = cview.TranslateANSI(s)
} else {
s = ansiRegex.ReplaceAllString(s, "")
}
var shifted string
lines := strings.Split(s, "\n")
Expand Down Expand Up @@ -277,9 +283,12 @@ 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") {
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")

links := make([]string, 0)
Expand Down