Skip to content

Commit

Permalink
Add theme and no-theme minibuffer commands
Browse files Browse the repository at this point in the history
You can run

:theme

and then select from the options - populated internally from termshark's
built-in theme repository, and from
~/.config/termshark/themes/*.toml. The choice is saved under the key
main.theme in termshark.toml.

:no-theme

to cause termshark to use its default colors (and save that preference).
  • Loading branch information
gcla committed Sep 11, 2020
1 parent 2edf53d commit 2e8bb74
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
97 changes: 97 additions & 0 deletions ui/lastline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ui

import (
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
Expand All @@ -15,10 +16,15 @@ import (
"github.com/gcla/gowid/gwutil"
"github.com/gcla/gowid/vim"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/theme"
"github.com/gcla/termshark/v2/widgets/mapkeys"
"github.com/gcla/termshark/v2/widgets/minibuffer"
"github.com/gdamore/tcell/terminfo"
"github.com/gdamore/tcell/terminfo/dynamic"
"github.com/rakyll/statik/fs"
"github.com/shibukawa/configdir"

_ "github.com/gcla/termshark/v2/assets/statik"
)

//======================================================================
Expand All @@ -29,6 +35,7 @@ var invalidReadCommandErr = fmt.Errorf("Invalid read command")
var invalidRecentsCommandErr = fmt.Errorf("Invalid recents command")
var invalidMapCommandErr = fmt.Errorf("Invalid map command")
var invalidFilterCommandErr = fmt.Errorf("Invalid filter command")
var invalidThemeCommandErr = fmt.Errorf("Invalid theme command")

type minibufferFn func(gowid.IApp, ...string) error

Expand Down Expand Up @@ -220,6 +227,58 @@ func (s filterArg) Completions() []string {

//======================================================================

type themeArg struct {
substr string
}

var _ minibuffer.IArg = themeArg{}

func (s themeArg) OfferCompletion() bool {
return true
}

func (s themeArg) Completions() []string {
matches := make([]string, 0)

// First gather built-in themes
statikFS, err := fs.New()
if err == nil {
dir, err := statikFS.Open("/themes")
if err == nil {
info, err := dir.Readdir(-1)
if err == nil {
for _, finfo := range info {
m := strings.TrimSuffix(finfo.Name(), ".toml")
if strings.Contains(m, s.substr) {
matches = append(matches, m)
}
}
}
}
}

// Then from filesystem
stdConf := configdir.New("", "termshark")
conf := stdConf.QueryFolderContainsFile("themes")
if conf != nil {
files, err := ioutil.ReadDir(filepath.Join(conf.Path, "themes"))
if err == nil {
for _, file := range files {
m := strings.TrimSuffix(file.Name(), ".toml")
if !termshark.StringInSlice(m, matches) {
if strings.Contains(m, s.substr) {
matches = append(matches, m)
}
}
}
}
}

return matches
}

//======================================================================

func stringIn(s string, a []string) bool {
for _, s2 := range a {
if s == s2 {
Expand Down Expand Up @@ -460,6 +519,44 @@ func (d filterCommand) Arguments(toks []string) []minibuffer.IArg {

//======================================================================

type themeCommand struct{}

var _ minibuffer.IAction = themeCommand{}

func (d themeCommand) Run(app gowid.IApp, args ...string) error {
var err error

if len(args) != 2 {
err = invalidThemeCommandErr
} else {
termshark.SetConf("main.theme", args[1])
theme.Load(args[1])
SetupColors()
}

if err != nil {
OpenMessage(fmt.Sprintf("Error: %s", err), appView, app)
}

return err
}

func (d themeCommand) OfferCompletion() bool {
return true
}

func (d themeCommand) Arguments(toks []string) []minibuffer.IArg {
res := make([]minibuffer.IArg, 0)
pref := ""
if len(toks) > 0 {
pref = toks[0]
}
res = append(res, themeArg{substr: pref})
return res
}

//======================================================================

type mapCommand struct {
w *mapkeys.Widget
}
Expand Down
9 changes: 9 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/gcla/termshark/v2/pdmltree"
"github.com/gcla/termshark/v2/psmlmodel"
"github.com/gcla/termshark/v2/system"
"github.com/gcla/termshark/v2/theme"
"github.com/gcla/termshark/v2/ui/menuutil"
"github.com/gcla/termshark/v2/ui/tableutil"
"github.com/gcla/termshark/v2/widgets"
Expand Down Expand Up @@ -1017,6 +1018,13 @@ func lastLineMode(app gowid.IApp) {
return nil
}))

MiniBuffer.Register("no-theme", minibufferFn(func(gowid.IApp, ...string) error {
theme.Clear()
termshark.DeleteConf("main.theme")
SetupColors()
return nil
}))

MiniBuffer.Register("convs", minibufferFn(func(gowid.IApp, ...string) error {
openConvsUi(app)
return nil
Expand Down Expand Up @@ -1057,6 +1065,7 @@ func lastLineMode(app gowid.IApp) {
MiniBuffer.Register("load", readCommand{complete: true})
MiniBuffer.Register("recents", recentsCommand{})
MiniBuffer.Register("filter", filterCommand{})
MiniBuffer.Register("theme", themeCommand{})
MiniBuffer.Register("map", mapCommand{w: keyMapper})
MiniBuffer.Register("unmap", unmapCommand{w: keyMapper})
MiniBuffer.Register("help", helpCommand{})
Expand Down

0 comments on commit 2e8bb74

Please sign in to comment.