cointop-sh / cointop

A fast and lightweight interactive terminal based UI application for tracking cryptocurrencies 🚀
https://cointop.sh
Apache License 2.0
3.98k stars 311 forks source link

Add support for defining reusable values in colorschemes #261

Closed lyricnz closed 2 years ago

lyricnz commented 2 years ago

It would be nice if you could define a logical name, then reuse it throughout the file - this would make it easier to update many values at once.

lyricnz commented 2 years ago

Something like this?

// NewColorscheme ...
func NewColorscheme(colors ColorschemeColors) *Colorscheme {
    // Build lookup table for defined values, then replace references to these
    const prefix = "define_"
    const reference = "&"
    defines := ColorschemeColors{}
    for k, v := range colors {
        if strings.HasPrefix(k, prefix) {
            defines[k[len(prefix):]] = v
        }
    }
    for k, v := range colors {
        if vs, ok := v.(string); ok {
            if strings.HasPrefix(vs, reference) {
                colors[k] = defines[vs[len(reference):]]
            }
        }
    }

    return &Colorscheme{
        colors:     colors,
        cache:      make(ColorCache),
        cacheMutex: sync.RWMutex{},
    }
}
lyricnz commented 2 years ago

Included as part of https://github.com/cointop-sh/cointop/pull/232