~sircmpwn/aerc

WIP: Add Style configuration v1 PROPOSED

Srivathsan Murali: 1
 WIP: Add Style configuration

 3 files changed, 69 insertions(+), 5 deletions(-)
What are your thoughts on having the style (theming) as a separate
config file (ie theme.conf or similar)? This would make it easier to
share premade themes, possibly even distributing some common ones with
aerc directly.

Alternatively, maybe adding a "theme" configuration option to aerc.conf
that would specify the name of the theme to use, and pull from files in
aerc's share folder or the user's config location.

Multiple themes could also be applied sequentially to overwrite prior
style directives, as Reto mentioned.
Thanks for starting to look into this, really excited about colorizing
aerc!



          
          
          
        
      
      
        
          







Along these lines of thought:

[style]
unread.fg=...
unread.bg=...
unread.bold=true
# ...

This is what I had been thinking of originally.
In terms of code, the implementation looks good to me.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/9680/mbox | git am -3
Learn more about email & git

[PATCH] WIP: Add Style configuration Export this patch

---
This is WIP of the style/color configuration for Aerc as I suggested
recently. 

I have setup very basic configuration for msglist just to test the
coloring out.

You can customize the styles used in tcell contexts by providing 
`[styles]` section in aerc.conf which provides the default style.

Styles for individual objects can be customized as children to the
styles sections as seen in the example below.

```
[styles]
fg=white

[styles.read]
bg=green

[styles.unread]
bg=blue

[styles.current_row]
bg=hotpink
```
As always, comments are welcome.

 config/config.go   | 30 ++++++++++++++++++++++++++++++
 config/style.go    | 33 +++++++++++++++++++++++++++++++++
 widgets/msglist.go | 11 ++++++-----
 3 files changed, 69 insertions(+), 5 deletions(-)
 create mode 100644 config/style.go

diff --git a/config/config.go b/config/config.go
index 0393e4673fc1..e72765a7bfd8 100644
--- a/config/config.go
+++ b/config/config.go
@@ -138,6 +138,7 @@ type AercConfig struct {
	ContextualUis []UIConfigContext
	General       GeneralConfig
	Templates     TemplateConfig
	Styles        map[StyleObject]tcell.Style
}

// Input: TimestampFormat
@@ -403,6 +404,27 @@ func (config *AercConfig) LoadConfig(file *ini.File) error {
			}
		}
	}
	if styles, err := file.GetSection("styles"); err == nil {
		defaultStyle := StyleConfig{}
		if err := styles.MapTo(&defaultStyle); err != nil {
			return err
		}
		config.Styles[STYLE_DEFAULT] = defaultStyle.getStyle()

		for _, child := range styles.ChildSections() {
			name := child.Name()
			index := strings.Index(name, ".")
			so, ok := StyleNames[name[index+1:]]
			if !ok {
				return errors.New("Unknown style object: " + name[index+1:])
			}
			childStyle := StyleConfig{}
			if err := child.MapTo(&childStyle); err != nil {
				return err
			}
			config.Styles[so] = childStyle.getStyle()
		}
	}
	return nil
}

@@ -486,6 +508,7 @@ func LoadConfigFromFile(root *string, sharedir string) (*AercConfig, error) {
			QuotedReply:  "quoted_reply",
			Forwards:     "forward_as_body",
		},
		Styles: map[StyleObject]tcell.Style{},
	}
	// These bindings are not configurable
	config.Bindings.AccountWizard.ExKey = KeyStroke{
@@ -629,3 +652,10 @@ func (config *AercConfig) GetUiConfig(params map[ContextType]string) UIConfig {

	return baseUi
}

func (config *AercConfig) GetStyle(so StyleObject, defaultStyle tcell.Style) tcell.Style {
	if style, ok := config.Styles[so]; ok {
		return style
	}
	return defaultStyle
}
diff --git a/config/style.go b/config/style.go
new file mode 100644
index 000000000000..998f6b620893
--- /dev/null
+++ b/config/style.go
@@ -0,0 +1,33 @@
package config

import (
	"github.com/gdamore/tcell"
)

type StyleConfig struct {
	Background string `ini:"bg"`
	Foreground string `ini:"fg"`
}

func (s *StyleConfig) getStyle() tcell.Style {
	bgColor := tcell.GetColor(s.Background)
	fgColor := tcell.GetColor(s.Foreground)
	return tcell.StyleDefault.Background(bgColor).Foreground(fgColor)
}

type StyleObject int32

const (
	STYLE_DEFAULT StyleObject = iota
	STYLE_UNREAD
	STYLE_READ
	STYLE_DELETED
	STYLE_CURRENT_ROW
)

var StyleNames = map[string]StyleObject{
	"unread":      STYLE_UNREAD,
	"read":        STYLE_READ,
	"deleted":     STYLE_DELETED,
	"current_row": STYLE_CURRENT_ROW,
}
diff --git a/widgets/msglist.go b/widgets/msglist.go
index 9aff0d4501f4..83c68fa8de57 100644
--- a/widgets/msglist.go
+++ b/widgets/msglist.go
@@ -49,7 +49,8 @@ func (ml *MessageList) Invalidate() {

func (ml *MessageList) Draw(ctx *ui.Context) {
	ml.height = ctx.Height()
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ',
		ml.conf.GetStyle(config.STYLE_DEFAULT, tcell.StyleDefault))

	store := ml.Store()
	if store == nil {
@@ -84,15 +85,15 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
			continue
		}

		style := tcell.StyleDefault
		style := ml.conf.GetStyle(config.STYLE_READ, tcell.StyleDefault)

		// current row
		if row == ml.store.SelectedIndex()-ml.scroll {
			style = style.Reverse(true)
			style = ml.conf.GetStyle(config.STYLE_CURRENT_ROW, style.Reverse(true))
		}
		// deleted message
		if _, ok := store.Deleted[msg.Uid]; ok {
			style = style.Foreground(tcell.ColorGray)
			style = ml.conf.GetStyle(config.STYLE_DELETED, style.Foreground(tcell.ColorGray))
		}
		// unread message
		seen := false
@@ -102,7 +103,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
			}
		}
		if !seen {
			style = style.Bold(true)
			style = ml.conf.GetStyle(config.STYLE_UNREAD, style.Bold(true))
		}

		ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
-- 
2.25.0
Here is a screenshot of aerc with the example styles config.
https://imgur.com/a/TA5swdG
View this thread in the archives