From 21d0f3381e642764e8778bc909cb1fdb4cb5e7ce Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Thu, 2 Mar 2023 23:50:50 +0100 Subject: templates: avoid error when using .Style with unknown style name When using {{.Style "foo" bar}} and "foo" is not defined in the styleset under the [user] section, there is an obscure error: template: column-right:1:18: executing "column-right" at <.Style>: error calling Style: runtime error: invalid memory address or nil pointer dereference Which is most of the time truncated and can only be read in logs. Referencing an undefined style is not a serious problem. Return the default tcell style in that case and avoid the error. Signed-off-by: Robin Jarry Acked-by: Tim Culverhouse --- config/style.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'config') diff --git a/config/style.go b/config/style.go index 317c77d0..7b13c3a2 100644 --- a/config/style.go +++ b/config/style.go @@ -288,7 +288,10 @@ func (ss StyleSet) Selected(so StyleObject) tcell.Style { } func (ss StyleSet) UserStyle(name string) tcell.Style { - return ss.user[name].Get() + if style, found := ss.user[name]; found { + return style.Get() + } + return tcell.StyleDefault } func (ss StyleSet) Compose(so StyleObject, sos []StyleObject) tcell.Style { -- cgit