Skip to content
Merged
13 changes: 10 additions & 3 deletions tui/appMenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,16 @@ func (m AppMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
switch m.list.SelectedItem().(AppMenuItem).id {
case attributeMenu:
attributeList := InitAttributeList()
am, cmd := attributeList.Update(WindowMsg())
return am, cmd
item := AttributeItem{
id: "8a6755f2-efa8-4758-b893-af9a488e0bea",
namespace: "demo.com",
name: "relto",
rule: "hierarchical",
description: "The relto attribute is used to describe the relationship of the resource to the country of origin.",
values: []string{"USA", "GBR"},
}
al, cmd := InitAttributeList([]list.Item{item}, 0)
return al, cmd
}
}
}
Expand Down
63 changes: 26 additions & 37 deletions tui/attributeList.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package tui

import (
"fmt"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/reflow/wordwrap"
"github.com/opentdf/tructl/tui/constants"
)

Expand All @@ -16,7 +13,7 @@ type AttributeList struct {
}

type AttributeItem struct {
id int
id string
namespace string
name string
description string
Expand All @@ -36,24 +33,17 @@ func (m AttributeItem) Description() string {
return m.description
}

func InitAttributeList() AttributeList {
func InitAttributeList(items []list.Item, selectIdx int) (tea.Model, tea.Cmd) {
// TODO: fetch items from API

m := AttributeList{}
m.list = list.New([]list.Item{}, list.NewDefaultDelegate(), constants.WindowSize.Width, constants.WindowSize.Height)
if selectIdx > 0 {
m.list.Select(selectIdx)
}
m.list.Title = "Attributes"
m.list.SetItems([]list.Item{
AttributeItem{
id: 1,
namespace: "demo.com",
name: "relto",
rule: "hierarchical",
description: "The relto attribute is used to describe the relationship of the resource to the country of origin.",
values: []string{"USA", "GBR"},
},
})

return m
m.list.SetItems(items)
return m.Update(WindowMsg())
}

func (m AttributeList) Init() tea.Cmd {
Expand All @@ -66,7 +56,7 @@ func StyleAttr(attr string) string {
Render(attr)
}

func CreateFormat(num int) string {
func CreateViewFormat(num int) string {
var format string
for i := 0; i < num; i++ {
format += "%s %s\n"
Expand All @@ -90,26 +80,25 @@ func (m AttributeList) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
am, _ := InitAppMenu()
am.list.Select(1)
return am.Update(WindowMsg())
case "down", "j":
if m.list.Index() < len(m.list.Items())-1 {
m.list.Select(m.list.Index() + 1)
}
case "up", "k":
if m.list.Index() > 0 {
m.list.Select(m.list.Index() - 1)
}
case "c":
// show the add attribute form
// InitAttributeCreateView()
return m, nil
case "enter":
item := m.list.Items()[0].(AttributeItem)
attr_keys := []string{"Name", "Namespace", "Rule", "Description", "Values"}
content := fmt.Sprintf(
CreateFormat(len(attr_keys)),
StyleAttr(attr_keys[0]), item.name,
StyleAttr(attr_keys[1]), item.namespace,
StyleAttr(attr_keys[2]), item.rule,
StyleAttr(attr_keys[3]), item.description,
StyleAttr(attr_keys[4]), item.values,
)
wrapped := wordwrap.String(content, m.width)
am := AttributeView{}
am.title = "Attribute"
am.content = wrapped
return am.Update(WindowMsg())
return InitAttributeView(m.list.Items(), len(m.list.Items()))
case "enter", "e":
return InitAttributeView(m.list.Items(), m.list.Index())
case "ctrl+d":
m.list.RemoveItem(m.list.Index())
newIndex := m.list.Index() - 1
if newIndex < 0 {
newIndex = 0
}
m.list.Select(newIndex)
}
}
return m, nil
Expand Down
Loading