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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
package config
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"text/template"
"git.sr.ht/~rjarry/aerc/lib/templates"
"github.com/go-ini/ini"
)
type ColumnFlags uint32
func (f ColumnFlags) Has(o ColumnFlags) bool { return f&o == o }
const (
ALIGN_LEFT ColumnFlags = 1 << iota
ALIGN_CENTER
ALIGN_RIGHT
WIDTH_AUTO // whatever is left
WIDTH_FRACTION // ratio of total width
WIDTH_EXACT // exact number of characters
WIDTH_FIT // fit to column content width
)
type ColumnDef struct {
Name string
Flags ColumnFlags
Width float64
Template *template.Template
}
var columnRe = regexp.MustCompile(`^([\w-]+)(?:([<:>])(=|\*|\d+%?)?)?$`)
func parseColumnDef(col string, section *ini.Section) (*ColumnDef, error) {
col = strings.TrimSpace(col)
match := columnRe.FindStringSubmatch(col)
if match == nil {
return nil, fmt.Errorf("invalid column def: %v", col)
}
name := match[1]
keyName := fmt.Sprintf("column-%s", name)
var flags ColumnFlags
switch match[2] {
case "<", "":
flags |= ALIGN_LEFT
case ":":
flags |= ALIGN_CENTER
case ">":
flags |= ALIGN_RIGHT
}
var width float64 = 0
switch match[3] {
case "=":
flags |= WIDTH_FIT
case "*", "":
flags |= WIDTH_AUTO
default:
s := match[3]
var divider float64 = 1
if strings.HasSuffix(s, "%") {
divider = 100
s = strings.TrimSuffix(s, "%")
flags |= WIDTH_FRACTION
} else {
flags |= WIDTH_EXACT
}
w, err := strconv.ParseFloat(s, 64)
if err != nil {
return nil, fmt.Errorf("%s: %w", keyName, err)
}
if divider == 100 && w > 100 {
return nil, fmt.Errorf("%s: invalid width %.0f%%", keyName, w)
}
width = w / divider
}
key, err := section.GetKey(keyName)
if err != nil {
return nil, err
}
t, err := templates.ParseTemplate(keyName, key.String())
if err != nil {
return nil, err
}
data := templates.DummyData()
var buf bytes.Buffer
err = t.Execute(&buf, data)
if err != nil {
return nil, err
}
return &ColumnDef{
Name: name,
Flags: flags,
Width: width,
Template: t,
}, nil
}
func ParseColumnDefs(key *ini.Key, section *ini.Section) ([]*ColumnDef, error) {
var columns []*ColumnDef
for _, col := range key.Strings(",") {
c, err := parseColumnDef(col, section)
if err != nil {
return nil, err
}
columns = append(columns, c)
}
if len(columns) == 0 {
return nil, fmt.Errorf("%s cannot be empty", key.Name())
}
return columns, nil
}
func ColumnDefsToIni(defs []*ColumnDef, keyName string) string {
var s strings.Builder
var cols []string
templates := make(map[string]string)
for _, def := range defs {
col := def.Name
switch {
case def.Flags.Has(ALIGN_LEFT):
col += "<"
case def.Flags.Has(ALIGN_CENTER):
col += ":"
case def.Flags.Has(ALIGN_RIGHT):
col += ">"
}
switch {
case def.Flags.Has(WIDTH_FIT):
col += "="
case def.Flags.Has(WIDTH_AUTO):
col += "*"
case def.Flags.Has(WIDTH_FRACTION):
col += fmt.Sprintf("%.0f%%", def.Width*100)
default:
col += fmt.Sprintf("%.0f", def.Width)
}
cols = append(cols, col)
tree := reflect.ValueOf(def.Template.Tree)
text := tree.Elem().FieldByName("text").String()
templates[fmt.Sprintf("column-%s", def.Name)] = text
}
s.WriteString(fmt.Sprintf("%s = %s\n", keyName, strings.Join(cols, ",")))
for name, text := range templates {
s.WriteString(fmt.Sprintf("%s = %s\n", name, text))
}
return s.String()
}
|