Skip to content

Commit

Permalink
Save per-terminal-mode theme settings
Browse files Browse the repository at this point in the history
At this point, termshark doesn't insist that a theme has to support
every typical terminal color mode - 16-colors, 256-colors,
truecolor. Most of the current themes are for 256-colors, and will also
work in truecolor mode. If the user has theme dracula set in 256-colors
mode, then runs termshark in a cut-down terminal on the same
machine (e.g. an xterm with 16 colors), then the theme won't
load. Rather than having that silently fail, I'm now saving a theme
setting per terminal mode, under toml keys theme-16, theme-256 and
theme-truecolor. When the theme is set, a message is displayed to make
it clear the theme is set for this color mode only. It's still not
completely obvious though - if the user set a theme in one terminal
emulator, the user might still wonder why it doesn't load up in the
cut-down terminal emulator. I'm not displaying a message - I don't want
to make the user click it away. Maybe it would help to have a more
obvious message buffer for short messages of interest to the user that
will disappear on their own...
  • Loading branch information
gcla committed Oct 23, 2020
1 parent 7934226 commit 81fd807
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion assets/statik/statik.go

Large diffs are not rendered by default.

24 changes: 15 additions & 9 deletions cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,15 +1022,21 @@ Loop:
// Need to do that here because the app won't know how many colors the screen
// has (and therefore which variant of the theme to load) until the screen is
// activated.
themeName := termshark.ConfString("main.theme", "default")
err = theme.Load(themeName, app)
if err != nil {
log.Warnf("Theme %s could not be loaded: %v", themeName, err)
if themeName != "default" {
err = theme.Load("default", app)
if err != nil {
log.Warnf("Theme %s could not be loaded: %v", themeName, err)
}
mode := theme.Mode(app.GetColorMode()).String() // more concise
themeName := termshark.ConfString(fmt.Sprintf("main.theme-%s", mode), "default")
loaded := false
if themeName != "" {
err = theme.Load(themeName, app)
if err != nil {
log.Warnf("Theme %s could not be loaded: %v", themeName, err)
} else {
loaded = true
}
}
if !loaded && themeName != "default" {
err = theme.Load("default", app)
if err != nil {
log.Warnf("Theme %s could not be loaded: %v", themeName, err)
}
}

Expand Down
35 changes: 25 additions & 10 deletions theme/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ func MakeColorSafe(s string, l Layer) (gowid.Color, error) {
return gowid.MakeColorSafe(s)
}

type Mode gowid.ColorMode

func (m Mode) String() string {
switch gowid.ColorMode(m) {
case gowid.Mode256Colors:
return "256"
case gowid.Mode88Colors:
return "88"
case gowid.Mode16Colors:
return "16"
case gowid.Mode8Colors:
return "8"
case gowid.ModeMonochrome:
return "mono"
case gowid.Mode24BitColors:
return "truecolor"
default:
return "unknown"
}
}

// Load will set the package-level theme object to a viper object representing the
// toml file either (a) read from disk, or failing that (b) built-in to termshark.
// Disk themes are prefered and take precedence.
Expand All @@ -81,21 +102,15 @@ func Load(name string, app gowid.IApp) error {
stdConf := configdir.New("", "termshark")
dirs := stdConf.QueryFolders(configdir.Global)

var mode string
switch app.GetColorMode() {
case gowid.Mode24BitColors:
mode = "truecolor"
case gowid.Mode256Colors:
mode = "256"
default:
mode = "16"
}
mode := Mode(app.GetColorMode()).String()

log.Infof("Loading theme %s in terminal mode %v", name, app.GetColorMode())

// If there's not a truecolor theme, we assume the user wants the best alternative to be loaded,
// and if a terminal has truecolor support, it'll surely have 256-color support.
modes := []string{mode}
if mode == "truecolor" {
modes = append(modes, "256")
modes = append(modes, Mode(gowid.Mode256Colors).String())
}

for _, m := range modes {
Expand Down
4 changes: 3 additions & 1 deletion ui/lastline.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,11 @@ func (d themeCommand) Run(app gowid.IApp, args ...string) error {
if len(args) != 2 {
err = invalidThemeCommandErr
} else {
termshark.SetConf("main.theme", args[1])
mode := theme.Mode(app.GetColorMode()).String() // more concise
termshark.SetConf(fmt.Sprintf("main.theme-%s", mode), args[1])
theme.Load(args[1], app)
SetupColors()
OpenMessage(fmt.Sprintf("Set %s theme for terminal mode %v.", args[1], app.GetColorMode()), appView, app)
}

if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,12 @@ func lastLineMode(app gowid.IApp) {
return nil
}))

MiniBuffer.Register("no-theme", minibufferFn(func(gowid.IApp, ...string) error {
termshark.DeleteConf("main.theme")
MiniBuffer.Register("no-theme", minibufferFn(func(app gowid.IApp, s ...string) error {
mode := theme.Mode(app.GetColorMode()).String() // more concise
termshark.DeleteConf(fmt.Sprintf("main.theme-%s", mode))
theme.Load("default", app)
SetupColors()
OpenMessage(fmt.Sprintf("Cleared theme for terminal mode %v.", app.GetColorMode()), appView, app)
return nil
}))

Expand Down

0 comments on commit 81fd807

Please sign in to comment.