Skip to content

Commit

Permalink
Add usable text module in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertras committed Jan 29, 2024
1 parent 9a6f82b commit 9fb4a5b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pkg/modules/text/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package text

type config struct {
Text string `mapstructure:"text"`
}
22 changes: 20 additions & 2 deletions pkg/modules/text/text.go
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/modules/text/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ 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")

// Send some random message
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())
}
6 changes: 3 additions & 3 deletions pkg/yakdash/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pkg/yakdash/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 9fb4a5b

Please sign in to comment.