config: Strongly type context type

The go compiler can't help much with untyped int constants.
Even though the only valid constants are 0-3 it will happily accept 4 as input.

Let's let the go compiler worry about correctness here. This also allows people
not very familiar with the code to use it properly via auto completion.
This commit is contained in:
Reto Brunner 2020-01-24 18:18:49 +01:00 committed by Drew DeVault
parent df5d9a3ec7
commit e78b7b85e4
3 changed files with 10 additions and 7 deletions

View file

@ -46,14 +46,16 @@ type UIConfig struct {
CompletionPopovers bool `ini:"completion-popovers"` CompletionPopovers bool `ini:"completion-popovers"`
} }
type ContextType int
const ( const (
UI_CONTEXT_FOLDER = iota UI_CONTEXT_FOLDER ContextType = iota
UI_CONTEXT_ACCOUNT UI_CONTEXT_ACCOUNT
UI_CONTEXT_SUBJECT UI_CONTEXT_SUBJECT
) )
type UIConfigContext struct { type UIConfigContext struct {
ContextType int ContextType ContextType
Regex *regexp.Regexp Regex *regexp.Regexp
UiConfig UIConfig UiConfig UIConfig
} }
@ -602,7 +604,8 @@ func parseLayout(layout string) [][]string {
return l return l
} }
func (config *AercConfig) mergeContextualUi(baseUi *UIConfig, contextType int, s string) { func (config *AercConfig) mergeContextualUi(baseUi *UIConfig,
contextType ContextType, s string) {
for _, contextualUi := range config.ContextualUis { for _, contextualUi := range config.ContextualUis {
if contextualUi.ContextType != contextType { if contextualUi.ContextType != contextType {
continue continue
@ -617,7 +620,7 @@ func (config *AercConfig) mergeContextualUi(baseUi *UIConfig, contextType int, s
} }
} }
func (config *AercConfig) GetUiConfig(params map[int]string) UIConfig { func (config *AercConfig) GetUiConfig(params map[ContextType]string) UIConfig {
baseUi := config.Ui baseUi := config.Ui
for k, v := range params { for k, v := range params {

View file

@ -32,7 +32,7 @@ type AccountView struct {
} }
func (acct *AccountView) UiConfig() config.UIConfig { func (acct *AccountView) UiConfig() config.UIConfig {
return acct.conf.GetUiConfig(map[int]string{ return acct.conf.GetUiConfig(map[config.ContextType]string{
config.UI_CONTEXT_ACCOUNT: acct.AccountConfig().Name, config.UI_CONTEXT_ACCOUNT: acct.AccountConfig().Name,
config.UI_CONTEXT_FOLDER: acct.Directories().Selected(), config.UI_CONTEXT_FOLDER: acct.Directories().Selected(),
}) })
@ -41,7 +41,7 @@ func (acct *AccountView) UiConfig() config.UIConfig {
func NewAccountView(aerc *Aerc, conf *config.AercConfig, acct *config.AccountConfig, func NewAccountView(aerc *Aerc, conf *config.AercConfig, acct *config.AccountConfig,
logger *log.Logger, host TabHost) *AccountView { logger *log.Logger, host TabHost) *AccountView {
acctUiConf := conf.GetUiConfig(map[int]string{ acctUiConf := conf.GetUiConfig(map[config.ContextType]string{
config.UI_CONTEXT_ACCOUNT: acct.Name, config.UI_CONTEXT_ACCOUNT: acct.Name,
}) })

View file

@ -106,7 +106,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
} }
ctx.Fill(0, row, ctx.Width(), 1, ' ', style) ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
uiConfig := ml.conf.GetUiConfig(map[int]string{ uiConfig := ml.conf.GetUiConfig(map[config.ContextType]string{
config.UI_CONTEXT_ACCOUNT: ml.aerc.SelectedAccount().AccountConfig().Name, config.UI_CONTEXT_ACCOUNT: ml.aerc.SelectedAccount().AccountConfig().Name,
config.UI_CONTEXT_FOLDER: ml.aerc.SelectedAccount().Directories().Selected(), config.UI_CONTEXT_FOLDER: ml.aerc.SelectedAccount().Directories().Selected(),
config.UI_CONTEXT_SUBJECT: msg.Envelope.Subject, config.UI_CONTEXT_SUBJECT: msg.Envelope.Subject,