| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"regexp" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"text/template" |
| 10 |
|
| 11 |
"git.sr.ht/~rjarry/aerc/lib/templates" |
| 12 |
"github.com/go-ini/ini" |
| 13 |
) |
| 14 |
|
| 15 |
type ColumnFlags uint32 |
| 16 |
|
| 17 |
func (f ColumnFlags) Has(o ColumnFlags) bool { return f&o == o } |
| 18 |
|
| 19 |
const ( |
| 20 |
ALIGN_LEFT ColumnFlags = 1 << iota |
| 21 |
ALIGN_CENTER |
| 22 |
ALIGN_RIGHT |
| 23 |
WIDTH_AUTO // whatever is left |
| 24 |
WIDTH_FRACTION // ratio of total width |
| 25 |
WIDTH_EXACT // exact number of characters |
| 26 |
WIDTH_FIT // fit to column content width |
| 27 |
) |
| 28 |
|
| 29 |
type ColumnDef struct { |
| 30 |
Name string |
| 31 |
Flags ColumnFlags |
| 32 |
Width float64 |
| 33 |
Template *template.Template |
| 34 |
} |
| 35 |
|
| 36 |
var columnRe = regexp.MustCompile(`^([\w-]+)(?:([<:>])(=|\*|\d+%?)?)?$`) |
| 37 |
|
| 38 |
func parseColumnDef(col string, section *ini.Section) (*ColumnDef, error) { |
| 39 |
col = strings.TrimSpace(col) |
| 40 |
match := columnRe.FindStringSubmatch(col) |
| 41 |
if match == nil { |
| 42 |
return nil, fmt.Errorf("invalid column def: %v", col) |
| 43 |
} |
| 44 |
name := match[1] |
| 45 |
keyName := fmt.Sprintf("column-%s", name) |
| 46 |
|
| 47 |
var flags ColumnFlags |
| 48 |
switch match[2] { |
| 49 |
case "<", "": |
| 50 |
flags |= ALIGN_LEFT |
| 51 |
case ":": |
| 52 |
flags |= ALIGN_CENTER |
| 53 |
case ">": |
| 54 |
flags |= ALIGN_RIGHT |
| 55 |
} |
| 56 |
|
| 57 |
var width float64 = 0 |
| 58 |
switch match[3] { |
| 59 |
case "=": |
| 60 |
flags |= WIDTH_FIT |
| 61 |
case "*", "": |
| 62 |
flags |= WIDTH_AUTO |
| 63 |
default: |
| 64 |
s := match[3] |
| 65 |
var divider float64 = 1 |
| 66 |
if strings.HasSuffix(s, "%") { |
| 67 |
divider = 100 |
| 68 |
s = strings.TrimSuffix(s, "%") |
| 69 |
flags |= WIDTH_FRACTION |
| 70 |
} else { |
| 71 |
flags |= WIDTH_EXACT |
| 72 |
} |
| 73 |
w, err := strconv.ParseFloat(s, 64) |
| 74 |
if err != nil { |
| 75 |
return nil, fmt.Errorf("%s: %w", keyName, err) |
| 76 |
} |
| 77 |
if divider == 100 && w > 100 { |
| 78 |
return nil, fmt.Errorf("%s: invalid width %.0f%%", keyName, w) |
| 79 |
} |
| 80 |
width = w / divider |
| 81 |
} |
| 82 |
key, err := section.GetKey(keyName) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
|
| 87 |
t, err := templates.ParseTemplate(keyName, key.String()) |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
|
| 92 |
err = templates.Render(t, &bytes.Buffer{}, &dummyData{}) |
| 93 |
if err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
|
| 97 |
return &ColumnDef{ |
| 98 |
Name: name, |
| 99 |
Flags: flags, |
| 100 |
Width: width, |
| 101 |
Template: t, |
| 102 |
}, nil |
| 103 |
} |
| 104 |
|
| 105 |
func ParseColumnDefs(key *ini.Key, section *ini.Section) ([]*ColumnDef, error) { |
| 106 |
var columns []*ColumnDef |
| 107 |
for _, col := range key.Strings(",") { |
| 108 |
c, err := parseColumnDef(col, section) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
columns = append(columns, c) |
| 113 |
} |
| 114 |
if len(columns) == 0 { |
| 115 |
return nil, nil |
| 116 |
} |
| 117 |
return columns, nil |
| 118 |
} |