Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
wrap tui list item
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 5, 2022
1 parent 1f33c1f commit b1e4bed
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
8 changes: 4 additions & 4 deletions group/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestGroupService_Save(t *testing.T) {
a.NoError(err)
}
}
savedGroups, err := gs.LoadGroups()
savedGroups, err := gs.loadGroups()
a.NoError(err)
for i := range savedGroups {
fmt.Println("wtf", savedGroups[i])
Expand All @@ -52,13 +52,13 @@ func TestGroupService_Save(t *testing.T) {
func TestGroupService_BuildTree(t *testing.T) {
a := assert.New(t)
gs := NewService()
groups, err := gs.LoadGroups()
groups, err := gs.loadGroups()
a.NoError(err)
for i := range groups {
fmt.Println(groups[i])
}
gs.BuildTree(groups)
for _, node := range gs.Tree {
gs.buildTree(groups)
for _, node := range gs.tree {
fmt.Println(node)
}
}
45 changes: 29 additions & 16 deletions group/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ func Service() *service {
return instance
}

type service struct {
Groups map[uint]*Node
Tree []*Node
}

func NewService() *service {
return &service{
Groups: make(map[uint]*Node, 0),
Tree: make([]*Node, 0),
groups: make(map[uint]*Node, 0),
tree: make([]*Node, 0),
}
}

func (gs *service) LoadGroups() ([]Group, error) {
type service struct {
groups map[uint]*Node
tree []*Node
}

func (gs *service) Tree() []*Node {
return gs.tree
}

func (gs *service) loadGroups() ([]Group, error) {
var groups []Group
if err := store.Store().Find(&groups, &bolthold.Query{}); err != nil {
if errors.Is(bolthold.ErrNotFound, err) {
Expand All @@ -43,31 +47,40 @@ func (gs *service) LoadGroups() ([]Group, error) {
return groups, nil
}

func (gs *service) BuildTree(groups []Group) {
if len(gs.Groups) == 0 {
func (gs *service) buildTree(groups []Group) {
if len(groups) == 0 {
return
}
for _, group := range groups {
gs.Groups[group.ID] = NewGroupNode(group)
gs.groups[group.ID] = NewGroupNode(group)
}
for _, node := range gs.Groups {
p, exist := gs.Groups[node.Parent]
for _, node := range gs.groups {
p, exist := gs.groups[node.Parent]
if !exist {
gs.Tree = append(gs.Tree, node)
gs.tree = append(gs.tree, node)
continue
}
p.Children = append(p.Children, node)
}
}

func (gs *service) Load() error {
groups, err := gs.loadGroups()
if err != nil {
return err
}
gs.buildTree(groups)
return nil
}

func (gs *service) Save(group Group) error {
if _, exist := gs.Groups[group.ID]; exist {
if _, exist := gs.groups[group.ID]; exist {
return ErrGroupExist
}
err := store.Store().Insert(group.ID, group)
if err != nil {
return err
}
gs.Groups[group.ID] = NewGroupNode(group)
gs.groups[group.ID] = NewGroupNode(group)
return nil
}
22 changes: 22 additions & 0 deletions host/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package host

import "sync"

var (
instance *service
once sync.Once
)

func Service() *service {
once.Do(func() {
instance = newService()
})
return instance
}

type service struct {
}

func newService() *service {
return &service{}
}
19 changes: 19 additions & 0 deletions tui/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tui

import (
"github.com/charmbracelet/bubbles/list"
"reflect"
)

func wrapListItems(slice any) []list.Item {
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
return nil
}
items := make([]list.Item, v.Len())
for i := 0; i < v.Len(); i++ {
item := v.Index(i).Interface()
items[i] = item.(list.Item)
}
return items
}
13 changes: 4 additions & 9 deletions tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

var (
docStyle = lipgloss.NewStyle().Margin(1, 2)
docStyle = lipgloss.NewStyle().Margin(1, 1)
modelStyle = lipgloss.NewStyle().Padding(1, 2).BorderStyle(lipgloss.HiddenBorder())
focusedModelStyle = lipgloss.NewStyle().Padding(1, 2).BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("69"))
)
Expand All @@ -34,20 +34,15 @@ type Model struct {

func NewModel() (*Model, error) {
groupService := group.Service()
groups, err := groupService.LoadGroups()
if err != nil {
if err := groupService.Load(); err != nil {
return nil, err
}
groupItems := make([]list.Item, len(groups))
for i := range groups {
groupItems[i] = groups[i]
}
groupService.BuildTree(groups)
groups := wrapListItems(groupService.Tree())

keys := newKeys()
m := &Model{
keys: keys,
groupList: list.New(groupItems, list.NewDefaultDelegate(), 0, 0),
groupList: list.New(groups, list.NewDefaultDelegate(), 0, 0),
help: help.New(),
}

Expand Down

0 comments on commit b1e4bed

Please sign in to comment.