From 6666ef2584eecffb6c5aba133e72279cf39f9c74 Mon Sep 17 00:00:00 2001 From: makeworld Date: Fri, 3 Dec 2021 17:58:16 -0500 Subject: [PATCH] Text and element colors of default theme change to be black on white terminals Ref #181 --- CHANGELOG.md | 3 +- NOTES.md | 1 - config/config.go | 15 +++++++++- config/theme.go | 70 ++++++++++++++++++++++++++++++++++++++-------- display/display.go | 4 +-- go.mod | 1 + go.sum | 4 +++ 7 files changed, 80 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10253a5..1748c44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Specifying `default` in the theme config uses the terminal's default background color, including transparency (#244, #245) - Redirects occur automatically if it only adds a trailing slash (#271) - Non-gemini links are underlined by default to help color blind users (#189) +- Text and element colors of default theme change to be black on white terminals (#181) ### Changed - Bookmarks are stored using XML in the XBEL format, old bookmarks are transferred (#68) @@ -24,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The user's terminal theme colors are used by default (#181) ## Removed -- Favicon support removed (#199) +- Favicon support (#199) - The default Amfora theme, get it back [here](https://github.com/makeworld-the-better-one/amfora/blob/master/contrib/themes/amfora.toml) ### Fixed diff --git a/NOTES.md b/NOTES.md index 6836011..d321e3f 100644 --- a/NOTES.md +++ b/NOTES.md @@ -6,7 +6,6 @@ ## Upstream Bugs - Bookmark keys aren't deleted, just set to `""` - Waiting on [this viper PR](https://github.com/spf13/viper/pull/519) to be merged -- [cview.Styles not being used](https://code.rocketnine.space/tslocum/cview/issues/47) - issue is circumvented in Amfora - [ANSI conversion is messed up](https://code.rocketnine.space/tslocum/cview/issues/48) - [WordWrap is broken in some cases](https://code.rocketnine.space/tslocum/cview/issues/27) - close #156 if this is fixed - [Prevent panic when reformatting](https://code.rocketnine.space/tslocum/cview/issues/50) - can't reliably reproduce or debug diff --git a/config/config.go b/config/config.go index 250ce25..8f65056 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/makeworld-the-better-one/amfora/cache" homedir "github.com/mitchellh/go-homedir" + "github.com/muesli/termenv" "github.com/rkoesters/xdg/basedir" "github.com/rkoesters/xdg/userdirs" "github.com/spf13/viper" @@ -59,6 +60,11 @@ var MediaHandlers = make(map[string]MediaHandler) // Defaults to ScrollBarAuto on an invalid value var ScrollBar cview.ScrollBarVisibility +// Whether the user's terminal is dark or light +// Defaults to dark, but is determined in Init() +// Used to prevent white text on a white background with the default theme +var hasDarkTerminalBackground bool + func Init() error { // *** Set paths *** @@ -370,7 +376,14 @@ func Init() error { } if viper.GetBool("a-general.color") { cview.Styles.PrimitiveBackgroundColor = GetColor("bg") - } // Otherwise it's black by default + } else { + // No colors allowed, set backgroud to black instead of default + themeMu.Lock() + theme["bg"] = tcell.ColorBlack + cview.Styles.PrimitiveBackgroundColor = tcell.ColorBlack + } + + hasDarkTerminalBackground = termenv.HasDarkBackground() // Parse HTTP command HTTPCommand = viper.GetStringSlice("a-general.http") diff --git a/config/theme.go b/config/theme.go index 2a218ea..61ab5d9 100644 --- a/config/theme.go +++ b/config/theme.go @@ -11,27 +11,43 @@ import ( // UI element tcell.Colors are mapped to a string key, such as "error" or "tab_bg" // These are the same keys used in the config file. +// Special color with no real color value +// Used for a default foreground color +// White is the terminal background is black, black if the terminal background is white +// Converted to a real color in this file before being sent out to other modules +const ColorFg = tcell.ColorSpecial | 2 + +// The same as ColorFg, but inverted +const ColorBg = tcell.ColorSpecial | 3 + var themeMu = sync.RWMutex{} var theme = map[string]tcell.Color{ + // Map these for special uses in code + "ColorBg": ColorBg, + "ColorFg": ColorFg, + // Default values below // Only the 16 Xterm system tcell.Colors are used, because those are the tcell.Colors overrided // by the user's default terminal theme // Used for cview.Styles.PrimitiveBackgroundColor // Set to tcell.ColorDefault because that allows transparent terminals to work - // The rest of this theme assumes that the background is equivalent to tcell.Color zero, or black. + // The rest of this theme assumes that the background is equivalent to black, but + // white colors switched to black later if the background is determined to be white. + // + // Also, this is set to tcell.ColorBlack in config.go if colors are disabled in the config. "bg": tcell.ColorDefault, "tab_num": tcell.ColorTeal, - "tab_divider": tcell.ColorWhite, + "tab_divider": ColorFg, "bottombar_label": tcell.ColorTeal, - "bottombar_text": tcell.ColorBlack, - "bottombar_bg": tcell.ColorWhite, - "scrollbar": tcell.ColorWhite, + "bottombar_text": ColorBg, + "bottombar_bg": ColorFg, + "scrollbar": ColorFg, // Modals - "btn_bg": tcell.ColorTeal, // All modal buttons - "btn_text": tcell.ColorWhite, + "btn_bg": tcell.ColorTeal, // All modal buttons + "btn_text": tcell.ColorWhite, // White instead of ColorFg because background is known to be Teal "dl_choice_modal_bg": tcell.ColorOlive, "dl_choice_modal_text": tcell.ColorWhite, @@ -65,10 +81,10 @@ var theme = map[string]tcell.Color{ "amfora_link": tcell.ColorBlue, "foreign_link": tcell.ColorPurple, "link_number": tcell.ColorSilver, - "regular_text": tcell.ColorWhite, - "quote_text": tcell.ColorWhite, - "preformatted_text": tcell.ColorWhite, - "list_text": tcell.ColorWhite, + "regular_text": ColorFg, + "quote_text": ColorFg, + "preformatted_text": ColorFg, + "list_text": ColorFg, } func SetColor(key string, color tcell.Color) { @@ -83,7 +99,23 @@ func SetColor(key string, color tcell.Color) { func GetColor(key string) tcell.Color { themeMu.RLock() defer themeMu.RUnlock() - return theme[key] + + color := theme[key] + + if color == ColorFg { + if hasDarkTerminalBackground { + return tcell.ColorWhite + } + return tcell.ColorBlack + } + if color == ColorBg { + if hasDarkTerminalBackground { + return tcell.ColorBlack + } + return tcell.ColorWhite + } + + return color } // colorToString converts a color to a string for use in a cview tag @@ -92,6 +124,19 @@ func colorToString(color tcell.Color) string { return "-" } + if color == ColorFg { + if hasDarkTerminalBackground { + return "white" + } + return "black" + } + if color == ColorBg { + if hasDarkTerminalBackground { + return "black" + } + return "white" + } + if color&tcell.ColorIsRGB == 0 { // tcell.Color is not RGB/TrueColor, it's a tcell.Color from the default terminal // theme as set above @@ -99,6 +144,7 @@ func colorToString(color tcell.Color) string { return ColorToColorName[color] } + // Color set by user, must be respected exactly so hex code is used return fmt.Sprintf("#%06x", color.Hex()) } diff --git a/display/display.go b/display/display.go index ab02b01..02d8136 100644 --- a/display/display.go +++ b/display/display.go @@ -96,8 +96,6 @@ func Init(version, commit, builtBy string) { layout.AddItem(bottomBar, 1, 1, false) if viper.GetBool("a-general.color") { - layout.SetBackgroundColor(config.GetColor("bg")) - bottomBar.SetBackgroundColor(config.GetColor("bottombar_bg")) bottomBar.SetLabelColor(config.GetColor("bottombar_label")) bottomBar.SetFieldBackgroundColor(config.GetColor("bottombar_bg")) @@ -106,7 +104,7 @@ func Init(version, commit, builtBy string) { browser.SetTabBackgroundColor(config.GetColor("bg")) browser.SetTabBackgroundColorFocused(config.GetColor("tab_num")) browser.SetTabTextColor(config.GetColor("tab_num")) - browser.SetTabTextColorFocused(config.GetTextColor("bg", "tab_num")) + browser.SetTabTextColorFocused(config.GetColor("ColorBg")) browser.SetTabSwitcherDivider( "", fmt.Sprintf("[%s:%s]|[-]", config.GetColorString("tab_divider"), config.GetColorString("bg")), diff --git a/go.mod b/go.mod index d24ce2d..09a7631 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.1 // indirect github.com/mmcdole/gofeed v1.1.2 + github.com/muesli/termenv v0.9.0 github.com/pelletier/go-toml v1.8.0 // indirect github.com/rkoesters/xdg v0.0.0-20181125232953-edd15b846f9b github.com/schollz/progressbar/v3 v3.8.0 diff --git a/go.sum b/go.sum index 491117a..1d76995 100644 --- a/go.sum +++ b/go.sum @@ -143,6 +143,8 @@ github.com/makeworld-the-better-one/go-gemini v0.11.0/go.mod h1:F+3x+R1xeYK90jMt github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= +github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= @@ -171,6 +173,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/muesli/termenv v0.9.0 h1:wnbOaGz+LUR3jNT0zOzinPnyDaCZUQRZj9GxK8eRVl8= +github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=