diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2023-02-27 09:18:16 -0600 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-02 23:04:35 +0100 |
commit | d9a8edd8e9269aa1189d55c8d13caa05084435f5 (patch) | |
tree | 65ee1373e485902a82155f6b52ebee59403b91e7 /config | |
parent | 8f8e22dbdd4f9e2ff1604e47e0fc78ff5912d633 (diff) | |
download | aerc-d9a8edd8e9269aa1189d55c8d13caa05084435f5.tar.gz |
templates: allow inline user styles
Allow custom user-defined styles in a styleset. The styles can take any
name, and must be under the [user] ini section. All attributes apply to
user defined styles.
Example:
[user]
red.fg=red
red.bold=true
Add a .Style function which accepts the name of a user-defined style and
applies it to the string.
{{.Style "red" "foo"}}
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'config')
-rw-r--r-- | config/style.go | 32 | ||||
-rw-r--r-- | config/templates.go | 1 | ||||
-rw-r--r-- | config/ui.go | 4 |
3 files changed, 37 insertions, 0 deletions
diff --git a/config/style.go b/config/style.go index 65c5a3b1..317c77d0 100644 --- a/config/style.go +++ b/config/style.go @@ -254,6 +254,7 @@ func (s Style) composeWith(styles []*Style) Style { type StyleSet struct { objects map[StyleObject]*Style selected map[StyleObject]*Style + user map[string]*Style path string } @@ -261,6 +262,7 @@ func NewStyleSet() StyleSet { ss := StyleSet{ objects: make(map[StyleObject]*Style), selected: make(map[StyleObject]*Style), + user: make(map[string]*Style), } for _, so := range StyleNames { ss.objects[so] = new(Style) @@ -285,6 +287,10 @@ func (ss StyleSet) Selected(so StyleObject) tcell.Style { return ss.selected[so].Get() } +func (ss StyleSet) UserStyle(name string) tcell.Style { + return ss.user[name].Get() +} + func (ss StyleSet) Compose(so StyleObject, sos []StyleObject) tcell.Style { base := *ss.objects[so] styles := make([]*Style, len(sos)) @@ -437,6 +443,32 @@ func (ss *StyleSet) ParseStyleSet(file *ini.File) error { } } + user, err := file.GetSection("user") + if err != nil { + // This errors if the section doesn't exist, which is ok + return nil + } + for _, key := range user.KeyStrings() { + tokens := strings.Split(key, ".") + var styleName, attr string + switch len(tokens) { + case 2: + styleName, attr = tokens[0], tokens[1] + default: + return errors.New("Style parsing error: " + key) + } + val := user.KeysHash()[key] + s, ok := ss.user[styleName] + if !ok { + // Haven't seen this name before, add it to the map + s = &Style{} + ss.user[styleName] = s + } + if err := s.Set(attr, val); err != nil { + return err + } + } + return nil } diff --git a/config/templates.go b/config/templates.go index bc05be66..f618d365 100644 --- a/config/templates.go +++ b/config/templates.go @@ -111,3 +111,4 @@ func (d *dummyData) ContentInfo() string { return "" } func (d *dummyData) StatusInfo() string { return "" } func (d *dummyData) TrayInfo() string { return "" } func (d *dummyData) PendingKeys() string { return "" } +func (d *dummyData) Style(string, string) string { return "" } diff --git a/config/ui.go b/config/ui.go index b1330ffd..64469643 100644 --- a/config/ui.go +++ b/config/ui.go @@ -629,6 +629,10 @@ func (base *UIConfig) mergeContextual( return base } +func (uiConfig *UIConfig) GetUserStyle(name string) tcell.Style { + return uiConfig.style.UserStyle(name) +} + func (uiConfig *UIConfig) GetStyle(so StyleObject) tcell.Style { return uiConfig.style.Get(so) } |