aboutsummaryrefslogtreecommitdiffstats
path: root/config/statusline.go
blob: 1c2c6240482ccc304ab41e9dd5f41bbdc6ce0184 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package config

import (
	"regexp"
	"strings"

	"git.sr.ht/~rjarry/aerc/lib/templates"
	"git.sr.ht/~rjarry/aerc/log"
	"github.com/go-ini/ini"
)

type StatuslineConfig struct {
	StatusColumns   []*ColumnDef `ini:"status-columns" parse:"ParseColumns" default:"left<*,center>=,right>*"`
	ColumnSeparator string       `ini:"column-separator" default:" "`
	Separator       string       `ini:"separator" default:" | "`
	DisplayMode     string       `ini:"display-mode" default:"text"`
}

var Statusline = new(StatuslineConfig)

func parseStatusline(file *ini.File) error {
	statusline := file.Section("statusline")
	if err := MapToStruct(statusline, Statusline, true); err != nil {
		return err
	}

	if key, err := statusline.GetKey("render-format"); err == nil {
		columns, err := convertRenderFormat(key.String())
		if err != nil {
			return err
		}
		Statusline.StatusColumns = columns
		log.Warnf("%s %s",
			"The [statusline] render-format setting has been replaced by status-columns.",
			"render-format will be removed in aerc 0.17.")
		Warnings = append(Warnings, Warning{
			Title: "DEPRECATION WARNING: [statusline].render-format",
			Body: `
The render-format setting is deprecated. It has been replaced by status-columns.

Your configuration in this instance was automatically converted to:

[statusline]
` + ColumnDefsToIni(columns, "status-columns") + `
Your configuration file was not changed. To make this change permanent and to
dismiss this deprecation warning on launch, copy the above lines into aerc.conf
and remove render-format from it. See aerc-config(5) for more details.

render-format will be removed in aerc 0.17.
`,
		})
	}

	log.Debugf("aerc.conf: [statusline] %#v", Statusline)
	return nil
}

func (s *StatuslineConfig) ParseColumns(sec *ini.Section, key *ini.Key) ([]*ColumnDef, error) {
	if !sec.HasKey("column-left") {
		_, _ = sec.NewKey("column-left", "[{{.Account}}] {{.StatusInfo}}")
	}
	if !sec.HasKey("column-center") {
		_, _ = sec.NewKey("column-center", "{{.PendingKeys}}")
	}
	if !sec.HasKey("column-right") {
		_, _ = sec.NewKey("column-right", "{{.TrayInfo}}")
	}
	return ParseColumnDefs(key, sec)
}

var (
	renderFmtRe    = regexp.MustCompile(`%(-?\d+)?(\.\d+)?[acdmSTp]`)
	statuslineMute = false
)

func convertRenderFormat(renderFormat string) ([]*ColumnDef, error) {
	var columns []*ColumnDef

	tokens := strings.Split(renderFormat, "%>")

	left := renderFmtRe.ReplaceAllStringFunc(
		tokens[0], renderVerbToTemplate)
	left = strings.TrimSpace(left)
	t, err := templates.ParseTemplate("column-left", left)
	if err != nil {
		return nil, err
	}
	columns = append(columns, &ColumnDef{
		Name:     "left",
		Template: t,
		Flags:    ALIGN_LEFT | WIDTH_AUTO,
	})

	if len(tokens) == 2 {
		right := renderFmtRe.ReplaceAllStringFunc(
			tokens[1], renderVerbToTemplate)
		right = strings.TrimSpace(right)
		t, err := templates.ParseTemplate("column-right", right)
		if err != nil {
			return nil, err
		}
		columns = append(columns, &ColumnDef{
			Name:     "right",
			Template: t,
			Flags:    ALIGN_RIGHT | WIDTH_AUTO,
		})
	}

	if statuslineMute {
		columns = nil
	}

	return columns, nil
}

func renderVerbToTemplate(verb string) (template string) {
	switch verb[len(verb)-1] {
	case 'a':
		template = `{{.Account}}`
	case 'c':
		template = `{{.ConnectionInfo}}`
	case 'd':
		template = `{{.Folder}}`
	case 'S':
		template = `{{.StatusInfo}}`
	case 'T':
		template = `{{.TrayInfo}}`
	case 'p':
		template = `{{cwd}}`
	case 'm':
		statuslineMute = true
	}
	return template
}