| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"maps" |
| 7 |
"os" |
| 8 |
"regexp" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 13 |
"github.com/emersion/go-message/mail" |
| 14 |
"github.com/go-ini/ini" |
| 15 |
"go.rockorager.dev/vaxis" |
| 16 |
) |
| 17 |
|
| 18 |
type StyleObject int32 |
| 19 |
|
| 20 |
const ( |
| 21 |
STYLE_DEFAULT StyleObject = iota |
| 22 |
STYLE_ERROR |
| 23 |
STYLE_WARNING |
| 24 |
STYLE_SUCCESS |
| 25 |
|
| 26 |
STYLE_TITLE |
| 27 |
STYLE_HEADER |
| 28 |
STYLE_REALNAME |
| 29 |
STYLE_EMAIL |
| 30 |
STYLE_SUBJECT |
| 31 |
STYLE_DATE |
| 32 |
|
| 33 |
STYLE_STATUSLINE_DEFAULT |
| 34 |
STYLE_STATUSLINE_ERROR |
| 35 |
STYLE_STATUSLINE_WARNING |
| 36 |
STYLE_STATUSLINE_SUCCESS |
| 37 |
|
| 38 |
STYLE_MSGLIST_DEFAULT |
| 39 |
STYLE_MSGLIST_UNREAD |
| 40 |
STYLE_MSGLIST_READ |
| 41 |
STYLE_MSGLIST_FLAGGED |
| 42 |
STYLE_MSGLIST_DELETED |
| 43 |
STYLE_MSGLIST_MARKED |
| 44 |
STYLE_MSGLIST_RESULT |
| 45 |
STYLE_MSGLIST_ANSWERED |
| 46 |
STYLE_MSGLIST_FORWARDED |
| 47 |
STYLE_MSGLIST_THREAD_FOLDED |
| 48 |
STYLE_MSGLIST_GUTTER |
| 49 |
STYLE_MSGLIST_PILL |
| 50 |
STYLE_MSGLIST_THREAD_CONTEXT |
| 51 |
STYLE_MSGLIST_THREAD_ORPHAN |
| 52 |
|
| 53 |
STYLE_DIRLIST_DEFAULT |
| 54 |
STYLE_DIRLIST_UNREAD |
| 55 |
STYLE_DIRLIST_RECENT |
| 56 |
|
| 57 |
STYLE_PART_SWITCHER |
| 58 |
STYLE_PART_FILENAME |
| 59 |
STYLE_PART_MIMETYPE |
| 60 |
|
| 61 |
STYLE_COMPLETION_DEFAULT |
| 62 |
STYLE_COMPLETION_DESCRIPTION |
| 63 |
STYLE_COMPLETION_GUTTER |
| 64 |
STYLE_COMPLETION_PILL |
| 65 |
|
| 66 |
STYLE_TAB |
| 67 |
STYLE_STACK |
| 68 |
STYLE_SPINNER |
| 69 |
STYLE_BORDER |
| 70 |
|
| 71 |
STYLE_SELECTOR_DEFAULT |
| 72 |
STYLE_SELECTOR_FOCUSED |
| 73 |
STYLE_SELECTOR_CHOOSER |
| 74 |
|
| 75 |
STYLE_KEYHINT_DEFAULT |
| 76 |
STYLE_KEYHINT_KEY |
| 77 |
STYLE_KEYHINT_DESC |
| 78 |
) |
| 79 |
|
| 80 |
var StyleNames = map[string]StyleObject{ |
| 81 |
"default": STYLE_DEFAULT, |
| 82 |
"error": STYLE_ERROR, |
| 83 |
"warning": STYLE_WARNING, |
| 84 |
"success": STYLE_SUCCESS, |
| 85 |
|
| 86 |
"title": STYLE_TITLE, |
| 87 |
"header": STYLE_HEADER, |
| 88 |
"realname": STYLE_REALNAME, |
| 89 |
"email": STYLE_EMAIL, |
| 90 |
"subject": STYLE_SUBJECT, |
| 91 |
"date": STYLE_DATE, |
| 92 |
|
| 93 |
"statusline_default": STYLE_STATUSLINE_DEFAULT, |
| 94 |
"statusline_error": STYLE_STATUSLINE_ERROR, |
| 95 |
"statusline_warning": STYLE_STATUSLINE_WARNING, |
| 96 |
"statusline_success": STYLE_STATUSLINE_SUCCESS, |
| 97 |
|
| 98 |
"msglist_default": STYLE_MSGLIST_DEFAULT, |
| 99 |
"msglist_unread": STYLE_MSGLIST_UNREAD, |
| 100 |
"msglist_read": STYLE_MSGLIST_READ, |
| 101 |
"msglist_flagged": STYLE_MSGLIST_FLAGGED, |
| 102 |
"msglist_deleted": STYLE_MSGLIST_DELETED, |
| 103 |
"msglist_marked": STYLE_MSGLIST_MARKED, |
| 104 |
"msglist_result": STYLE_MSGLIST_RESULT, |
| 105 |
"msglist_answered": STYLE_MSGLIST_ANSWERED, |
| 106 |
"msglist_forwarded": STYLE_MSGLIST_FORWARDED, |
| 107 |
"msglist_gutter": STYLE_MSGLIST_GUTTER, |
| 108 |
"msglist_pill": STYLE_MSGLIST_PILL, |
| 109 |
|
| 110 |
"msglist_thread_folded": STYLE_MSGLIST_THREAD_FOLDED, |
| 111 |
"msglist_thread_context": STYLE_MSGLIST_THREAD_CONTEXT, |
| 112 |
"msglist_thread_orphan": STYLE_MSGLIST_THREAD_ORPHAN, |
| 113 |
|
| 114 |
"dirlist_default": STYLE_DIRLIST_DEFAULT, |
| 115 |
"dirlist_unread": STYLE_DIRLIST_UNREAD, |
| 116 |
"dirlist_recent": STYLE_DIRLIST_RECENT, |
| 117 |
|
| 118 |
"part_switcher": STYLE_PART_SWITCHER, |
| 119 |
"part_filename": STYLE_PART_FILENAME, |
| 120 |
"part_mimetype": STYLE_PART_MIMETYPE, |
| 121 |
|
| 122 |
"completion_default": STYLE_COMPLETION_DEFAULT, |
| 123 |
"completion_description": STYLE_COMPLETION_DESCRIPTION, |
| 124 |
"completion_gutter": STYLE_COMPLETION_GUTTER, |
| 125 |
"completion_pill": STYLE_COMPLETION_PILL, |
| 126 |
|
| 127 |
"tab": STYLE_TAB, |
| 128 |
"stack": STYLE_STACK, |
| 129 |
"spinner": STYLE_SPINNER, |
| 130 |
"border": STYLE_BORDER, |
| 131 |
|
| 132 |
"selector_default": STYLE_SELECTOR_DEFAULT, |
| 133 |
"selector_focused": STYLE_SELECTOR_FOCUSED, |
| 134 |
"selector_chooser": STYLE_SELECTOR_CHOOSER, |
| 135 |
|
| 136 |
"keyhint_default": STYLE_KEYHINT_DEFAULT, |
| 137 |
"keyhint_key": STYLE_KEYHINT_KEY, |
| 138 |
"keyhint_desc": STYLE_KEYHINT_DESC, |
| 139 |
} |
| 140 |
|
| 141 |
type StyleHeaderPattern struct { |
| 142 |
RawPattern string |
| 143 |
Re *regexp.Regexp |
| 144 |
} |
| 145 |
|
| 146 |
type Style struct { |
| 147 |
Fg vaxis.Color |
| 148 |
Bg vaxis.Color |
| 149 |
Bold bool |
| 150 |
Blink bool |
| 151 |
Underline bool |
| 152 |
Reverse bool |
| 153 |
Italic bool |
| 154 |
Dim bool |
| 155 |
// Only for msglist, maps header -> pattern/regexp |
| 156 |
// All regexps must match in order for the style to be applied |
| 157 |
headerPatterns map[string]*StyleHeaderPattern |
| 158 |
} |
| 159 |
|
| 160 |
func (s Style) Get() vaxis.Style { |
| 161 |
vx := vaxis.Style{ |
| 162 |
Foreground: s.Fg, |
| 163 |
Background: s.Bg, |
| 164 |
} |
| 165 |
if s.Bold { |
| 166 |
vx.Attribute |= vaxis.AttrBold |
| 167 |
} |
| 168 |
if s.Blink { |
| 169 |
vx.Attribute |= vaxis.AttrBlink |
| 170 |
} |
| 171 |
if s.Underline { |
| 172 |
vx.UnderlineStyle |= vaxis.UnderlineSingle |
| 173 |
} |
| 174 |
if s.Reverse { |
| 175 |
vx.Attribute |= vaxis.AttrReverse |
| 176 |
} |
| 177 |
if s.Italic { |
| 178 |
vx.Attribute |= vaxis.AttrItalic |
| 179 |
} |
| 180 |
if s.Dim { |
| 181 |
vx.Attribute |= vaxis.AttrDim |
| 182 |
} |
| 183 |
return vx |
| 184 |
} |
| 185 |
|
| 186 |
func (s *Style) Normal() { |
| 187 |
s.Bold = false |
| 188 |
s.Blink = false |
| 189 |
s.Underline = false |
| 190 |
s.Reverse = false |
| 191 |
s.Italic = false |
| 192 |
s.Dim = false |
| 193 |
} |
| 194 |
|
| 195 |
func (s *Style) Default() *Style { |
| 196 |
s.Fg = 0 |
| 197 |
s.Bg = 0 |
| 198 |
return s |
| 199 |
} |
| 200 |
|
| 201 |
func (s *Style) Reset() *Style { |
| 202 |
s.Default() |
| 203 |
s.Normal() |
| 204 |
return s |
| 205 |
} |
| 206 |
|
| 207 |
func (s *Style) hasSameHeaderPatterns(other map[string]*StyleHeaderPattern) bool { |
| 208 |
return maps.EqualFunc(s.headerPatterns, other, func(a, b *StyleHeaderPattern) bool { |
| 209 |
return a.RawPattern == b.RawPattern |
| 210 |
}) |
| 211 |
} |
| 212 |
|
| 213 |
func boolSwitch(val string, cur_val bool) (bool, error) { |
| 214 |
switch val { |
| 215 |
case "true": |
| 216 |
return true, nil |
| 217 |
case "false": |
| 218 |
return false, nil |
| 219 |
case "toggle": |
| 220 |
return !cur_val, nil |
| 221 |
default: |
| 222 |
return cur_val, errors.New( |
| 223 |
"Bool Switch attribute must be true, false, or toggle") |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func extractColor(val string) vaxis.Color { |
| 228 |
// Check if the string can be interpreted as a number, indicating a |
| 229 |
// reference to the color number. Otherwise retrieve the number based |
| 230 |
// on the name. |
| 231 |
if i, err := strconv.ParseUint(val, 10, 8); err == nil { |
| 232 |
return vaxis.IndexColor(uint8(i)) |
| 233 |
} |
| 234 |
if cut, found := strings.CutPrefix(val, "#"); found { |
| 235 |
val = cut |
| 236 |
hex, err := strconv.ParseUint(val, 16, 32) |
| 237 |
if err != nil { |
| 238 |
return 0 |
| 239 |
} |
| 240 |
return vaxis.HexColor(uint32(hex)) |
| 241 |
} |
| 242 |
return colorNames[val] |
| 243 |
} |
| 244 |
|
| 245 |
func (s *Style) Set(attr, val string) error { |
| 246 |
switch attr { |
| 247 |
case "fg": |
| 248 |
s.Fg = extractColor(val) |
| 249 |
case "bg": |
| 250 |
s.Bg = extractColor(val) |
| 251 |
case "bold": |
| 252 |
if state, err := boolSwitch(val, s.Bold); err != nil { |
| 253 |
return err |
| 254 |
} else { |
| 255 |
s.Bold = state |
| 256 |
} |
| 257 |
case "blink": |
| 258 |
if state, err := boolSwitch(val, s.Blink); err != nil { |
| 259 |
return err |
| 260 |
} else { |
| 261 |
s.Blink = state |
| 262 |
} |
| 263 |
case "underline": |
| 264 |
if state, err := boolSwitch(val, s.Underline); err != nil { |
| 265 |
return err |
| 266 |
} else { |
| 267 |
s.Underline = state |
| 268 |
} |
| 269 |
case "reverse": |
| 270 |
if state, err := boolSwitch(val, s.Reverse); err != nil { |
| 271 |
return err |
| 272 |
} else { |
| 273 |
s.Reverse = state |
| 274 |
} |
| 275 |
case "italic": |
| 276 |
if state, err := boolSwitch(val, s.Italic); err != nil { |
| 277 |
return err |
| 278 |
} else { |
| 279 |
s.Italic = state |
| 280 |
} |
| 281 |
case "dim": |
| 282 |
if state, err := boolSwitch(val, s.Dim); err != nil { |
| 283 |
return err |
| 284 |
} else { |
| 285 |
s.Dim = state |
| 286 |
} |
| 287 |
case "default": |
| 288 |
s.Default() |
| 289 |
case "normal": |
| 290 |
s.Normal() |
| 291 |
default: |
| 292 |
return errors.New("Unknown style attribute: " + attr) |
| 293 |
} |
| 294 |
|
| 295 |
return nil |
| 296 |
} |
| 297 |
|
| 298 |
func (s Style) composeWith(styles []*Style) Style { |
| 299 |
newStyle := s |
| 300 |
for _, st := range styles { |
| 301 |
if st.Fg != s.Fg && st.Fg != 0 { |
| 302 |
newStyle.Fg = st.Fg |
| 303 |
} |
| 304 |
if st.Bg != s.Bg && st.Bg != 0 { |
| 305 |
newStyle.Bg = st.Bg |
| 306 |
} |
| 307 |
if st.Bold != s.Bold { |
| 308 |
newStyle.Bold = st.Bold |
| 309 |
} |
| 310 |
if st.Blink != s.Blink { |
| 311 |
newStyle.Blink = st.Blink |
| 312 |
} |
| 313 |
if st.Underline != s.Underline { |
| 314 |
newStyle.Underline = st.Underline |
| 315 |
} |
| 316 |
if st.Reverse != s.Reverse { |
| 317 |
newStyle.Reverse = st.Reverse |
| 318 |
} |
| 319 |
if st.Italic != s.Italic { |
| 320 |
newStyle.Italic = st.Italic |
| 321 |
} |
| 322 |
if st.Dim != s.Dim { |
| 323 |
newStyle.Dim = st.Dim |
| 324 |
} |
| 325 |
} |
| 326 |
return newStyle |
| 327 |
} |
| 328 |
|
| 329 |
type StyleConf struct { |
| 330 |
base Style |
| 331 |
dynamic []Style |
| 332 |
} |
| 333 |
|
| 334 |
type StyleSet struct { |
| 335 |
objects map[StyleObject]*StyleConf |
| 336 |
selected map[StyleObject]*StyleConf |
| 337 |
user map[string]*Style |
| 338 |
paths []string |
| 339 |
} |
| 340 |
|
| 341 |
const defaultStyleset string = ` |
| 342 |
*.selected.bg = 12 |
| 343 |
*.selected.fg = 15 |
| 344 |
*.selected.bold = true |
| 345 |
statusline_*.dim = true |
| 346 |
*warning.dim = false |
| 347 |
*warning.bold = true |
| 348 |
*warning.fg = 11 |
| 349 |
*success.dim = false |
| 350 |
*success.bold = true |
| 351 |
*success.fg = 10 |
| 352 |
*error.dim = false |
| 353 |
*error.bold = true |
| 354 |
*error.fg = 9 |
| 355 |
border.fg = 12 |
| 356 |
border.bold = true |
| 357 |
title.bg = 12 |
| 358 |
title.fg = 15 |
| 359 |
title.bold = true |
| 360 |
header.fg = 4 |
| 361 |
header.bold = true |
| 362 |
msglist_unread.bold = true |
| 363 |
msglist_deleted.dim = true |
| 364 |
msglist_marked.bg = 6 |
| 365 |
msglist_marked.fg = 15 |
| 366 |
msglist_pill.bg = 12 |
| 367 |
msglist_pill.fg = 15 |
| 368 |
part_mimetype.fg = 12 |
| 369 |
selector_chooser.bold = true |
| 370 |
selector_focused.bold = true |
| 371 |
selector_focused.bg = 12 |
| 372 |
selector_focused.fg = 15 |
| 373 |
completion_*.bg = 8 |
| 374 |
completion_pill.bg = 12 |
| 375 |
completion_default.fg = 15 |
| 376 |
completion_description.fg = 15 |
| 377 |
completion_description.dim = true |
| 378 |
` |
| 379 |
|
| 380 |
func NewStyleSet() *StyleSet { |
| 381 |
ss := &StyleSet{ |
| 382 |
objects: make(map[StyleObject]*StyleConf), |
| 383 |
selected: make(map[StyleObject]*StyleConf), |
| 384 |
user: make(map[string]*Style), |
| 385 |
} |
| 386 |
for _, so := range StyleNames { |
| 387 |
ss.objects[so] = new(StyleConf) |
| 388 |
ss.selected[so] = new(StyleConf) |
| 389 |
} |
| 390 |
f, err := ini.Load([]byte(defaultStyleset)) |
| 391 |
if err == nil { |
| 392 |
err = ss.ParseStyleSet(f) |
| 393 |
} |
| 394 |
if err != nil { |
| 395 |
panic(err) |
| 396 |
} |
| 397 |
return ss |
| 398 |
} |
| 399 |
|
| 400 |
func (c *StyleConf) getStyle(h *mail.Header) *Style { |
| 401 |
if h == nil { |
| 402 |
return &c.base |
| 403 |
} |
| 404 |
style := &c.base |
| 405 |
|
| 406 |
// All dynamic styles must be iterated through, as later ones might be a |
| 407 |
// narrower match based due to multiple header patterns. |
| 408 |
for _, s := range c.dynamic { |
| 409 |
allMatch := true |
| 410 |
for header, pattern := range s.headerPatterns { |
| 411 |
val, _ := h.Text(header) |
| 412 |
allMatch = allMatch && pattern.Re.MatchString(val) |
| 413 |
} |
| 414 |
|
| 415 |
if allMatch { |
| 416 |
s := c.base.composeWith([]*Style{&s}) |
| 417 |
style = &s |
| 418 |
} |
| 419 |
} |
| 420 |
return style |
| 421 |
} |
| 422 |
|
| 423 |
func (ss StyleSet) Get(so StyleObject, h *mail.Header) vaxis.Style { |
| 424 |
return ss.objects[so].getStyle(h).Get() |
| 425 |
} |
| 426 |
|
| 427 |
func (ss StyleSet) Selected(so StyleObject, h *mail.Header) vaxis.Style { |
| 428 |
return ss.selected[so].getStyle(h).Get() |
| 429 |
} |
| 430 |
|
| 431 |
func (ss StyleSet) UserStyle(name string) vaxis.Style { |
| 432 |
if style, found := ss.user[name]; found { |
| 433 |
return style.Get() |
| 434 |
} |
| 435 |
return vaxis.Style{} |
| 436 |
} |
| 437 |
|
| 438 |
func (ss StyleSet) Compose( |
| 439 |
so StyleObject, sos []StyleObject, h *mail.Header, |
| 440 |
) vaxis.Style { |
| 441 |
base := *ss.objects[so].getStyle(h) |
| 442 |
styles := make([]*Style, len(sos)) |
| 443 |
for i, so := range sos { |
| 444 |
styles[i] = ss.objects[so].getStyle(h) |
| 445 |
} |
| 446 |
|
| 447 |
return base.composeWith(styles).Get() |
| 448 |
} |
| 449 |
|
| 450 |
func (ss StyleSet) ComposeSelected( |
| 451 |
so StyleObject, sos []StyleObject, h *mail.Header, |
| 452 |
) vaxis.Style { |
| 453 |
base := *ss.selected[so].getStyle(h) |
| 454 |
styles := make([]*Style, len(sos)) |
| 455 |
for i, so := range sos { |
| 456 |
styles[i] = ss.selected[so].getStyle(h) |
| 457 |
} |
| 458 |
|
| 459 |
return base.composeWith(styles).Get() |
| 460 |
} |
| 461 |
|
| 462 |
func findStyleSet(stylesetName string, stylesetsDir []string) (string, error) { |
| 463 |
for _, dir := range stylesetsDir { |
| 464 |
stylesetPath := xdg.ExpandHome(dir, stylesetName) |
| 465 |
if _, err := os.Stat(stylesetPath); os.IsNotExist(err) { |
| 466 |
continue |
| 467 |
} |
| 468 |
|
| 469 |
return stylesetPath, nil |
| 470 |
} |
| 471 |
|
| 472 |
return "", fmt.Errorf( |
| 473 |
"Can't find styleset %q in any of %v", stylesetName, stylesetsDir) |
| 474 |
} |
| 475 |
|
| 476 |
func (ss *StyleSet) ParseStyleSet(file *ini.File) error { |
| 477 |
defaultSection, err := file.GetSection(ini.DefaultSection) |
| 478 |
if err != nil { |
| 479 |
return err |
| 480 |
} |
| 481 |
|
| 482 |
// parse non-selected items first |
| 483 |
for _, key := range defaultSection.Keys() { |
| 484 |
err = ss.parseKey(key, false) |
| 485 |
if err != nil { |
| 486 |
return err |
| 487 |
} |
| 488 |
} |
| 489 |
// override with selected items afterwards |
| 490 |
for _, key := range defaultSection.Keys() { |
| 491 |
err = ss.parseKey(key, true) |
| 492 |
if err != nil { |
| 493 |
return err |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
user, err := file.GetSection("user") |
| 498 |
if err != nil { |
| 499 |
// This errors if the section doesn't exist, which is ok |
| 500 |
return nil |
| 501 |
} |
| 502 |
for _, key := range user.KeyStrings() { |
| 503 |
tokens := strings.Split(key, ".") |
| 504 |
var styleName, attr string |
| 505 |
switch len(tokens) { |
| 506 |
case 2: |
| 507 |
styleName, attr = tokens[0], tokens[1] |
| 508 |
default: |
| 509 |
return errors.New("Style parsing error: " + key) |
| 510 |
} |
| 511 |
val := user.KeysHash()[key] |
| 512 |
s, ok := ss.user[styleName] |
| 513 |
if !ok { |
| 514 |
// Haven't seen this name before, add it to the map |
| 515 |
s = &Style{} |
| 516 |
ss.user[styleName] = s |
| 517 |
} |
| 518 |
if err := s.Set(attr, val); err != nil { |
| 519 |
return fmt.Errorf("[user].%s=%s: %w", key, val, err) |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
return nil |
| 524 |
} |
| 525 |
|
| 526 |
var ( |
| 527 |
styleObjRe = regexp.MustCompile(`^([\w\*\?]+)(\.(?:[\w-]+,.+?)+?)?(\.selected)?\.(\w+)$`) |
| 528 |
styleHeaderPatternsRe = regexp.MustCompile(`([\w-]+),(~/(?:.+?)/|(?:.+?))\.`) |
| 529 |
) |
| 530 |
|
| 531 |
func (ss *StyleSet) parseKey(key *ini.Key, selected bool) error { |
| 532 |
groups := styleObjRe.FindStringSubmatch(key.Name()) |
| 533 |
if groups == nil { |
| 534 |
return errors.New("invalid style syntax: " + key.Name()) |
| 535 |
} |
| 536 |
if (groups[3] == ".selected") != selected { |
| 537 |
return nil |
| 538 |
} |
| 539 |
obj, attr := groups[1], groups[4] |
| 540 |
|
| 541 |
// As there can be multiple header patterns, match them separately, one |
| 542 |
// by one |
| 543 |
headerMatches := styleHeaderPatternsRe.FindAllStringSubmatch(groups[2]+".", -1) |
| 544 |
headerPatterns := make(map[string]*StyleHeaderPattern) |
| 545 |
for _, match := range headerMatches { |
| 546 |
headerPatterns[match[1]] = &StyleHeaderPattern{ |
| 547 |
RawPattern: match[2], |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
objRe, err := fnmatchToRegex(obj) |
| 552 |
if err != nil { |
| 553 |
return err |
| 554 |
} |
| 555 |
num := 0 |
| 556 |
for sn, so := range StyleNames { |
| 557 |
if !objRe.MatchString(sn) { |
| 558 |
continue |
| 559 |
} |
| 560 |
if !selected { |
| 561 |
err = ss.objects[so].update(headerPatterns, attr, key.Value()) |
| 562 |
if err != nil { |
| 563 |
return fmt.Errorf("%s=%s: %w", key.Name(), key.Value(), err) |
| 564 |
} |
| 565 |
} |
| 566 |
err = ss.selected[so].update(headerPatterns, attr, key.Value()) |
| 567 |
if err != nil { |
| 568 |
return fmt.Errorf("%s=%s: %w", key.Name(), key.Value(), err) |
| 569 |
} |
| 570 |
num++ |
| 571 |
} |
| 572 |
if num == 0 { |
| 573 |
return errors.New("unknown style object: " + obj) |
| 574 |
} |
| 575 |
return nil |
| 576 |
} |
| 577 |
|
| 578 |
func (c *StyleConf) update(headerPatterns map[string]*StyleHeaderPattern, attr, val string) error { |
| 579 |
if len(headerPatterns) == 0 { |
| 580 |
return (&c.base).Set(attr, val) |
| 581 |
} |
| 582 |
|
| 583 |
// Check existing entries and overwrite ones with same header/pattern |
| 584 |
for i := range c.dynamic { |
| 585 |
s := &c.dynamic[i] |
| 586 |
if s.hasSameHeaderPatterns(headerPatterns) { |
| 587 |
return s.Set(attr, val) |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
s := Style{} |
| 592 |
err := (&s).Set(attr, val) |
| 593 |
if err != nil { |
| 594 |
return err |
| 595 |
} |
| 596 |
|
| 597 |
for _, p := range headerPatterns { |
| 598 |
var pattern string |
| 599 |
switch { |
| 600 |
case strings.HasPrefix(p.RawPattern, "~/"): |
| 601 |
pattern = p.RawPattern[2 : len(p.RawPattern)-1] |
| 602 |
case strings.HasPrefix(p.RawPattern, "~"): |
| 603 |
pattern = p.RawPattern[1:] |
| 604 |
default: |
| 605 |
pattern = "^" + regexp.QuoteMeta(p.RawPattern) + "$" |
| 606 |
} |
| 607 |
|
| 608 |
re, err := regexp.Compile(pattern) |
| 609 |
if err != nil { |
| 610 |
return err |
| 611 |
} |
| 612 |
p.Re = re |
| 613 |
} |
| 614 |
|
| 615 |
s.headerPatterns = headerPatterns |
| 616 |
c.dynamic = append(c.dynamic, s) |
| 617 |
return nil |
| 618 |
} |
| 619 |
|
| 620 |
func (ss *StyleSet) LoadStyleSet(stylesetName string, stylesetDirs []string) error { |
| 621 |
parts := strings.Split(stylesetName, ",") |
| 622 |
filepaths := []string{} |
| 623 |
for _, name := range parts { |
| 624 |
filepath, err := findStyleSet(strings.TrimSpace(name), stylesetDirs) |
| 625 |
if err != nil { |
| 626 |
return err |
| 627 |
} |
| 628 |
filepaths = append(filepaths, filepath) |
| 629 |
} |
| 630 |
|
| 631 |
var options ini.LoadOptions |
| 632 |
options.SpaceBeforeInlineComment = true |
| 633 |
|
| 634 |
ifilepaths := make([]any, len(filepaths)) |
| 635 |
for i, v := range filepaths { |
| 636 |
ifilepaths[i] = v |
| 637 |
} |
| 638 |
file, err := ini.LoadSources(options, ifilepaths[0], ifilepaths[1:]...) |
| 639 |
if err != nil { |
| 640 |
return err |
| 641 |
} |
| 642 |
|
| 643 |
ss.paths = filepaths |
| 644 |
|
| 645 |
return ss.ParseStyleSet(file) |
| 646 |
} |
| 647 |
|
| 648 |
func fnmatchToRegex(pattern string) (*regexp.Regexp, error) { |
| 649 |
p := regexp.QuoteMeta(pattern) |
| 650 |
p = strings.ReplaceAll(p, `\*`, `.*`) |
| 651 |
return regexp.Compile(strings.ReplaceAll(p, `\?`, `.`)) |
| 652 |
} |
| 653 |
|
| 654 |
var colorNames = map[string]vaxis.Color{ |
| 655 |
"black": vaxis.IndexColor(0), |
| 656 |
"maroon": vaxis.IndexColor(1), |
| 657 |
"green": vaxis.IndexColor(2), |
| 658 |
"olive": vaxis.IndexColor(3), |
| 659 |
"navy": vaxis.IndexColor(4), |
| 660 |
"purple": vaxis.IndexColor(5), |
| 661 |
"teal": vaxis.IndexColor(6), |
| 662 |
"silver": vaxis.IndexColor(7), |
| 663 |
"gray": vaxis.IndexColor(8), |
| 664 |
"red": vaxis.IndexColor(9), |
| 665 |
"lime": vaxis.IndexColor(10), |
| 666 |
"yellow": vaxis.IndexColor(11), |
| 667 |
"blue": vaxis.IndexColor(12), |
| 668 |
"fuchsia": vaxis.IndexColor(13), |
| 669 |
"aqua": vaxis.IndexColor(14), |
| 670 |
"white": vaxis.IndexColor(15), |
| 671 |
"aliceblue": vaxis.HexColor(0xF0F8FF), |
| 672 |
"antiquewhite": vaxis.HexColor(0xFAEBD7), |
| 673 |
"aquamarine": vaxis.HexColor(0x7FFFD4), |
| 674 |
"azure": vaxis.HexColor(0xF0FFFF), |
| 675 |
"beige": vaxis.HexColor(0xF5F5DC), |
| 676 |
"bisque": vaxis.HexColor(0xFFE4C4), |
| 677 |
"blanchedalmond": vaxis.HexColor(0xFFEBCD), |
| 678 |
"blueviolet": vaxis.HexColor(0x8A2BE2), |
| 679 |
"brown": vaxis.HexColor(0xA52A2A), |
| 680 |
"burlywood": vaxis.HexColor(0xDEB887), |
| 681 |
"cadetblue": vaxis.HexColor(0x5F9EA0), |
| 682 |
"chartreuse": vaxis.HexColor(0x7FFF00), |
| 683 |
"chocolate": vaxis.HexColor(0xD2691E), |
| 684 |
"coral": vaxis.HexColor(0xFF7F50), |
| 685 |
"cornflowerblue": vaxis.HexColor(0x6495ED), |
| 686 |
"cornsilk": vaxis.HexColor(0xFFF8DC), |
| 687 |
"crimson": vaxis.HexColor(0xDC143C), |
| 688 |
"darkblue": vaxis.HexColor(0x00008B), |
| 689 |
"darkcyan": vaxis.HexColor(0x008B8B), |
| 690 |
"darkgoldenrod": vaxis.HexColor(0xB8860B), |
| 691 |
"darkgray": vaxis.HexColor(0xA9A9A9), |
| 692 |
"darkgreen": vaxis.HexColor(0x006400), |
| 693 |
"darkkhaki": vaxis.HexColor(0xBDB76B), |
| 694 |
"darkmagenta": vaxis.HexColor(0x8B008B), |
| 695 |
"darkolivegreen": vaxis.HexColor(0x556B2F), |
| 696 |
"darkorange": vaxis.HexColor(0xFF8C00), |
| 697 |
"darkorchid": vaxis.HexColor(0x9932CC), |
| 698 |
"darkred": vaxis.HexColor(0x8B0000), |
| 699 |
"darksalmon": vaxis.HexColor(0xE9967A), |
| 700 |
"darkseagreen": vaxis.HexColor(0x8FBC8F), |
| 701 |
"darkslateblue": vaxis.HexColor(0x483D8B), |
| 702 |
"darkslategray": vaxis.HexColor(0x2F4F4F), |
| 703 |
"darkturquoise": vaxis.HexColor(0x00CED1), |
| 704 |
"darkviolet": vaxis.HexColor(0x9400D3), |
| 705 |
"deeppink": vaxis.HexColor(0xFF1493), |
| 706 |
"deepskyblue": vaxis.HexColor(0x00BFFF), |
| 707 |
"dimgray": vaxis.HexColor(0x696969), |
| 708 |
"dodgerblue": vaxis.HexColor(0x1E90FF), |
| 709 |
"firebrick": vaxis.HexColor(0xB22222), |
| 710 |
"floralwhite": vaxis.HexColor(0xFFFAF0), |
| 711 |
"forestgreen": vaxis.HexColor(0x228B22), |
| 712 |
"gainsboro": vaxis.HexColor(0xDCDCDC), |
| 713 |
"ghostwhite": vaxis.HexColor(0xF8F8FF), |
| 714 |
"gold": vaxis.HexColor(0xFFD700), |
| 715 |
"goldenrod": vaxis.HexColor(0xDAA520), |
| 716 |
"greenyellow": vaxis.HexColor(0xADFF2F), |
| 717 |
"honeydew": vaxis.HexColor(0xF0FFF0), |
| 718 |
"hotpink": vaxis.HexColor(0xFF69B4), |
| 719 |
"indianred": vaxis.HexColor(0xCD5C5C), |
| 720 |
"indigo": vaxis.HexColor(0x4B0082), |
| 721 |
"ivory": vaxis.HexColor(0xFFFFF0), |
| 722 |
"khaki": vaxis.HexColor(0xF0E68C), |
| 723 |
"lavender": vaxis.HexColor(0xE6E6FA), |
| 724 |
"lavenderblush": vaxis.HexColor(0xFFF0F5), |
| 725 |
"lawngreen": vaxis.HexColor(0x7CFC00), |
| 726 |
"lemonchiffon": vaxis.HexColor(0xFFFACD), |
| 727 |
"lightblue": vaxis.HexColor(0xADD8E6), |
| 728 |
"lightcoral": vaxis.HexColor(0xF08080), |
| 729 |
"lightcyan": vaxis.HexColor(0xE0FFFF), |
| 730 |
"lightgoldenrodyellow": vaxis.HexColor(0xFAFAD2), |
| 731 |
"lightgray": vaxis.HexColor(0xD3D3D3), |
| 732 |
"lightgreen": vaxis.HexColor(0x90EE90), |
| 733 |
"lightpink": vaxis.HexColor(0xFFB6C1), |
| 734 |
"lightsalmon": vaxis.HexColor(0xFFA07A), |
| 735 |
"lightseagreen": vaxis.HexColor(0x20B2AA), |
| 736 |
"lightskyblue": vaxis.HexColor(0x87CEFA), |
| 737 |
"lightslategray": vaxis.HexColor(0x778899), |
| 738 |
"lightsteelblue": vaxis.HexColor(0xB0C4DE), |
| 739 |
"lightyellow": vaxis.HexColor(0xFFFFE0), |
| 740 |
"limegreen": vaxis.HexColor(0x32CD32), |
| 741 |
"linen": vaxis.HexColor(0xFAF0E6), |
| 742 |
"mediumaquamarine": vaxis.HexColor(0x66CDAA), |
| 743 |
"mediumblue": vaxis.HexColor(0x0000CD), |
| 744 |
"mediumorchid": vaxis.HexColor(0xBA55D3), |
| 745 |
"mediumpurple": vaxis.HexColor(0x9370DB), |
| 746 |
"mediumseagreen": vaxis.HexColor(0x3CB371), |
| 747 |
"mediumslateblue": vaxis.HexColor(0x7B68EE), |
| 748 |
"mediumspringgreen": vaxis.HexColor(0x00FA9A), |
| 749 |
"mediumturquoise": vaxis.HexColor(0x48D1CC), |
| 750 |
"mediumvioletred": vaxis.HexColor(0xC71585), |
| 751 |
"midnightblue": vaxis.HexColor(0x191970), |
| 752 |
"mintcream": vaxis.HexColor(0xF5FFFA), |
| 753 |
"mistyrose": vaxis.HexColor(0xFFE4E1), |
| 754 |
"moccasin": vaxis.HexColor(0xFFE4B5), |
| 755 |
"navajowhite": vaxis.HexColor(0xFFDEAD), |
| 756 |
"oldlace": vaxis.HexColor(0xFDF5E6), |
| 757 |
"olivedrab": vaxis.HexColor(0x6B8E23), |
| 758 |
"orange": vaxis.HexColor(0xFFA500), |
| 759 |
"orangered": vaxis.HexColor(0xFF4500), |
| 760 |
"orchid": vaxis.HexColor(0xDA70D6), |
| 761 |
"palegoldenrod": vaxis.HexColor(0xEEE8AA), |
| 762 |
"palegreen": vaxis.HexColor(0x98FB98), |
| 763 |
"paleturquoise": vaxis.HexColor(0xAFEEEE), |
| 764 |
"palevioletred": vaxis.HexColor(0xDB7093), |
| 765 |
"papayawhip": vaxis.HexColor(0xFFEFD5), |
| 766 |
"peachpuff": vaxis.HexColor(0xFFDAB9), |
| 767 |
"peru": vaxis.HexColor(0xCD853F), |
| 768 |
"pink": vaxis.HexColor(0xFFC0CB), |
| 769 |
"plum": vaxis.HexColor(0xDDA0DD), |
| 770 |
"powderblue": vaxis.HexColor(0xB0E0E6), |
| 771 |
"rebeccapurple": vaxis.HexColor(0x663399), |
| 772 |
"rosybrown": vaxis.HexColor(0xBC8F8F), |
| 773 |
"royalblue": vaxis.HexColor(0x4169E1), |
| 774 |
"saddlebrown": vaxis.HexColor(0x8B4513), |
| 775 |
"salmon": vaxis.HexColor(0xFA8072), |
| 776 |
"sandybrown": vaxis.HexColor(0xF4A460), |
| 777 |
"seagreen": vaxis.HexColor(0x2E8B57), |
| 778 |
"seashell": vaxis.HexColor(0xFFF5EE), |
| 779 |
"sienna": vaxis.HexColor(0xA0522D), |
| 780 |
"skyblue": vaxis.HexColor(0x87CEEB), |
| 781 |
"slateblue": vaxis.HexColor(0x6A5ACD), |
| 782 |
"slategray": vaxis.HexColor(0x708090), |
| 783 |
"snow": vaxis.HexColor(0xFFFAFA), |
| 784 |
"springgreen": vaxis.HexColor(0x00FF7F), |
| 785 |
"steelblue": vaxis.HexColor(0x4682B4), |
| 786 |
"tan": vaxis.HexColor(0xD2B48C), |
| 787 |
"thistle": vaxis.HexColor(0xD8BFD8), |
| 788 |
"tomato": vaxis.HexColor(0xFF6347), |
| 789 |
"turquoise": vaxis.HexColor(0x40E0D0), |
| 790 |
"violet": vaxis.HexColor(0xEE82EE), |
| 791 |
"wheat": vaxis.HexColor(0xF5DEB3), |
| 792 |
"whitesmoke": vaxis.HexColor(0xF5F5F5), |
| 793 |
"yellowgreen": vaxis.HexColor(0x9ACD32), |
| 794 |
} |