From 8ba753e08cb3e689a590f3fb4958f5d9442de0f3 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 26 Jan 2023 19:45:31 +0100 Subject: [PATCH] fix: remove segment from cache on empty template --- src/engine/segment.go | 23 ++++++++++++++++++----- src/platform/shell.go | 6 ++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/engine/segment.go b/src/engine/segment.go index b33e8a04..a44b0646 100644 --- a/src/engine/segment.go +++ b/src/engine/segment.go @@ -47,6 +47,7 @@ type Segment struct { writer SegmentWriter text string styleCache SegmentStyle + name string } // SegmentTiming holds the timing context for a segment @@ -427,6 +428,18 @@ func (segment *Segment) string() string { return text } +func (segment *Segment) Name() string { + if len(segment.name) != 0 { + return segment.name + } + name := segment.Alias + if len(name) == 0 { + name = c.Title(language.English).String(string(segment.Type)) + } + segment.name = name + return name +} + func (segment *Segment) SetEnabled(env platform.Environment) { defer func() { err := recover() @@ -456,11 +469,7 @@ func (segment *Segment) SetEnabled(env platform.Environment) { } if segment.writer.Enabled() { segment.Enabled = true - name := segment.Alias - if len(name) == 0 { - name = c.Title(language.English).String(string(segment.Type)) - } - env.TemplateCache().AddSegmentData(name, segment.writer) + env.TemplateCache().AddSegmentData(segment.Name(), segment.writer) } } @@ -470,6 +479,10 @@ func (segment *Segment) SetText() { } segment.text = segment.string() segment.Enabled = len(strings.ReplaceAll(segment.text, " ", "")) > 0 + if !segment.Enabled { + segment.env.TemplateCache().RemoveSegmentData(segment.Name()) + } + if segment.Interactive { return } diff --git a/src/platform/shell.go b/src/platform/shell.go index 15d66199..b7f2fff9 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -163,6 +163,12 @@ func (t *TemplateCache) AddSegmentData(key string, value interface{}) { t.Unlock() } +func (t *TemplateCache) RemoveSegmentData(key string) { + t.Lock() + delete(t.Segments, key) + t.Unlock() +} + type Environment interface { Getenv(key string) string Pwd() string