From 5f673401524410805808b0e32f5fa237968b28ab Mon Sep 17 00:00:00 2001 From: defektive Date: Thu, 11 Nov 2021 10:58:15 -0700 Subject: [PATCH] dont crash when left, center, or right bars are empty Not sure if this is the "right" fix... --- bar/bar.go | 54 +++++++++++++++++++++++++++++++++-------------------- bar/bars.go | 19 +++++++++++++------ 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/bar/bar.go b/bar/bar.go index 0b9582d..42e350c 100644 --- a/bar/bar.go +++ b/bar/bar.go @@ -209,48 +209,62 @@ func NewBar( return b, nil } +func (b *Bar) hasBarSection(section string) bool { + if b.ctx.Has(section) { + sectionMarkup := b.ctx.MustString(section) + if sectionMarkup != "\n\n" { + return true + } + } + + return false +} + func (b *Bar) createRoots() error { - if b.ctx.Has("bar_left") { + if b.hasBarSection("bar_left") { ctx := b.ctx.New(ui.Context{"bar_align": "left"}) b.LeftRoot = ui.NewRoot(ctx, func() { b.LeftRoot.Paint() b.PaintLeft(b.LeftRoot.Image()) }) + + + modules, err := b.mk.Parse(b.LeftRoot, nil, b.ctx.MustString("bar_left")) + if err != nil { + return fmt.Errorf("config: bar_left: %w", err) + } + b.LeftRoot.Inner = modules + } - if b.ctx.Has("bar_center") { + if b.hasBarSection("bar_center") { ctx := b.ctx.New(ui.Context{"bar_align": "center"}) b.CenterRoot = ui.NewRoot(ctx, func() { b.CenterRoot.Paint() b.PaintCenter(b.CenterRoot.Image()) }) + + + modules, err := b.mk.Parse(b.CenterRoot, nil, b.ctx.MustString("bar_center")) + if err != nil { + return fmt.Errorf("config: bar_center: %w", err) + } + b.CenterRoot.Inner = modules } - if b.ctx.Has("bar_right") { + if b.hasBarSection("bar_right") { ctx := b.ctx.New(ui.Context{"bar_align": "right"}) b.RightRoot = ui.NewRoot(ctx, func() { b.RightRoot.Paint() b.PaintRight(b.RightRoot.Image()) }) - } - - modules, err := b.mk.Parse(b.LeftRoot, nil, b.ctx.MustString("bar_left")) - if err != nil { - return fmt.Errorf("config: bar_left: %w", err) - } - b.LeftRoot.Inner = modules - modules, err = b.mk.Parse(b.CenterRoot, nil, b.ctx.MustString("bar_center")) - if err != nil { - return fmt.Errorf("config: bar_center: %w", err) - } - b.CenterRoot.Inner = modules - - modules, err = b.mk.Parse(b.RightRoot, nil, b.ctx.MustString("bar_right")) - if err != nil { - return fmt.Errorf("config: bar_right: %w", err) + modules, err := b.mk.Parse(b.RightRoot, nil, b.ctx.MustString("bar_right")) + if err != nil { + return fmt.Errorf("config: bar_right: %w", err) + } + b.RightRoot.Inner = modules } - b.RightRoot.Inner = modules return nil } diff --git a/bar/bars.go b/bar/bars.go index e4d828a..330c250 100644 --- a/bar/bars.go +++ b/bar/bars.go @@ -80,12 +80,19 @@ func (b *Bars) createBars() error { func (b *Bars) onClockTick() { for _, bar := range b.Bars { - bar.LeftRoot.Paint() - bar.CenterRoot.Paint() - bar.RightRoot.Paint() + if bar.LeftRoot != nil { + bar.LeftRoot.Paint() + bar.PaintLeft(bar.LeftRoot.Image()) + } + + if bar.CenterRoot != nil { + bar.CenterRoot.Paint() + bar.PaintCenter(bar.CenterRoot.Image()) + } - bar.PaintLeft(bar.LeftRoot.Image()) - bar.PaintCenter(bar.CenterRoot.Image()) - bar.PaintRight(bar.RightRoot.Image()) + if bar.RightRoot != nil { + bar.RightRoot.Paint() + bar.PaintRight(bar.RightRoot.Image()) + } } }