From 9fb4a5b5d375acecf4b17a9701db6e5290a7c388 Mon Sep 17 00:00:00 2001 From: Brandon Fulljames Date: Mon, 29 Jan 2024 16:14:51 +0900 Subject: [PATCH] Add usable text module in yaml --- pkg/modules/text/config.go | 5 +++++ pkg/modules/text/text.go | 22 ++++++++++++++++++++-- pkg/modules/text/text_test.go | 16 ++++++++++++++-- pkg/yakdash/build.go | 6 +++--- pkg/yakdash/modules.go | 5 ++++- 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 pkg/modules/text/config.go diff --git a/pkg/modules/text/config.go b/pkg/modules/text/config.go new file mode 100644 index 0000000..6128088 --- /dev/null +++ b/pkg/modules/text/config.go @@ -0,0 +1,5 @@ +package text + +type config struct { + Text string `mapstructure:"text"` +} diff --git a/pkg/modules/text/text.go b/pkg/modules/text/text.go index 661d423..0a8dca5 100644 --- a/pkg/modules/text/text.go +++ b/pkg/modules/text/text.go @@ -1,12 +1,30 @@ package text -import tea "github.com/charmbracelet/bubbletea" +import ( + "fmt" + + tea "github.com/charmbracelet/bubbletea" + "github.com/mitchellh/mapstructure" +) type model struct { text string } -func New(text string) model { +func New(cfg map[string]interface{}) (model, error) { + var config config + err := mapstructure.Decode(cfg, &config) + + if err != nil { + return model{}, fmt.Errorf("failed to parse config: %w", err) + } + + return model{ + text: config.Text, + }, nil +} + +func NewPlainText(text string) model { return model{ text: text, } diff --git a/pkg/modules/text/text_test.go b/pkg/modules/text/text_test.go index 367f418..c184b89 100644 --- a/pkg/modules/text/text_test.go +++ b/pkg/modules/text/text_test.go @@ -9,14 +9,14 @@ import ( ) func TestTextHasNoInit(t *testing.T) { - m := text.New("hello world") + m := text.NewPlainText("hello world") cmd := m.Init() assert.Nil(t, cmd) } func TestTextDisplaysText(t *testing.T) { - m := text.New("hello world") + m := text.NewPlainText("hello world") assert.Equal(t, "hello world", m.View(), "Text should be initialized to the given text") @@ -24,3 +24,15 @@ func TestTextDisplaysText(t *testing.T) { m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("a")}) assert.Equal(t, "hello world", m.View(), "Text should not change on update") } + +func TestTextParsesConfig(t *testing.T) { + cfg := map[string]interface{}{ + "text": "hello", + } + + m, err := text.New(cfg) + + assert.NoError(t, err) + + assert.Equal(t, "hello", m.View()) +} diff --git a/pkg/yakdash/build.go b/pkg/yakdash/build.go index cfcd8ac..0002562 100644 --- a/pkg/yakdash/build.go +++ b/pkg/yakdash/build.go @@ -30,16 +30,16 @@ func New(l layout.Root) model { module, err := loadModule(node) if err != nil { - module = text.New(fmt.Sprintf("Error loading module %q: %s", node.Module, err.Error())) + module = text.NewPlainText(fmt.Sprintf("Error loading module %q: %s", node.Module, err.Error())) } alignV, err := panes.ToAlignmentVertical(node.Style.AlignVertical) if err != nil { - module = text.New(fmt.Sprintf("Bad alignment %q: %s", node.Module, err.Error())) + module = text.NewPlainText(fmt.Sprintf("Bad alignment %q: %s", node.Module, err.Error())) } alignH, err := panes.ToAlignmentHorizontal(node.Style.AlignHorizontal) if err != nil { - module = text.New(fmt.Sprintf("Bad alignment %q: %s", node.Module, err.Error())) + module = text.NewPlainText(fmt.Sprintf("Bad alignment %q: %s", node.Module, err.Error())) } return panes.NewLeaf(module).WithName(node.Name).WithAlignment(alignV, alignH) diff --git a/pkg/yakdash/modules.go b/pkg/yakdash/modules.go index 04baeb5..3df2500 100644 --- a/pkg/yakdash/modules.go +++ b/pkg/yakdash/modules.go @@ -17,7 +17,10 @@ func loadModule(l layout.Node) (tea.Model, error) { case "command": return command.New(l.Config) + case "text": + return text.New(l.Config) + default: - return text.New("Unknown module: " + l.Module), nil + return text.NewPlainText("Unknown module: " + l.Module), nil } }