| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"sync/atomic" |
| 7 |
|
| 8 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 9 |
"github.com/go-ini/ini" |
| 10 |
) |
| 11 |
|
| 12 |
type StatuslineConfig struct { |
| 13 |
StatusColumns []*ColumnDef `ini:"status-columns" parse:"ParseColumns" default:"left<*,center>=,right>*"` |
| 14 |
ColumnSeparator string `ini:"column-separator" default:" "` |
| 15 |
Separator string `ini:"separator" default:" | "` |
| 16 |
DisplayMode string `ini:"display-mode" default:"text"` |
| 17 |
pendingKeysCol int |
| 18 |
} |
| 19 |
|
| 20 |
var statuslineConfig atomic.Pointer[StatuslineConfig] |
| 21 |
|
| 22 |
func Statusline() *StatuslineConfig { |
| 23 |
return statuslineConfig.Load() |
| 24 |
} |
| 25 |
|
| 26 |
func parseStatusline(file *ini.File) (*StatuslineConfig, error) { |
| 27 |
conf := new(StatuslineConfig) |
| 28 |
statusline := file.Section("statusline") |
| 29 |
if err := MapToStruct(statusline, conf, true); err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
|
| 33 |
log.Debugf("aerc.conf: [statusline] %#v", conf) |
| 34 |
return conf, nil |
| 35 |
} |
| 36 |
|
| 37 |
func (s *StatuslineConfig) ParseColumns(sec *ini.Section, key *ini.Key) ([]*ColumnDef, error) { |
| 38 |
if !sec.HasKey("column-left") { |
| 39 |
_, _ = sec.NewKey("column-left", "[{{.Account}}] {{.StatusInfo}}") |
| 40 |
} |
| 41 |
if !sec.HasKey("column-center") { |
| 42 |
_, _ = sec.NewKey("column-center", "{{.PendingKeys}}") |
| 43 |
} |
| 44 |
if !sec.HasKey("column-right") { |
| 45 |
_, _ = sec.NewKey("column-right", "{{.TrayInfo}} | {{cwd}}") |
| 46 |
} |
| 47 |
result, err := ParseColumnDefs(key, sec) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
s.pendingKeysCol = -1 |
| 52 |
for i, col := range result { |
| 53 |
raw, err := sec.GetKey(fmt.Sprintf("column-%s", col.Name)) |
| 54 |
if err == nil && strings.Contains(raw.String(), "PendingKeys") { |
| 55 |
s.pendingKeysCol = i |
| 56 |
break |
| 57 |
} |
| 58 |
} |
| 59 |
return result, nil |
| 60 |
} |
| 61 |
|
| 62 |
func (s *StatuslineConfig) PendingKeysColIndex() int { |
| 63 |
return s.pendingKeysCol |
| 64 |
} |