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

Commit

Permalink
use default list
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 7, 2022
1 parent 163d024 commit 53472df
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 59 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
-
name: Run GoReleaser
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
Expand Down
8 changes: 4 additions & 4 deletions gohost/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func (s *Service) loadGroups() []Group {
return groups
}

func (s *Service) loadGroupNodes() []*TreeNode[Node] {
func (s *Service) loadGroupNodes() []*TreeNode {
groups := s.loadGroups()
groupNodes := make([]*TreeNode[Node], 0, len(groups))
groupNodes := make([]*TreeNode, 0, len(groups))
for _, group := range groups {
groupNodes = append(groupNodes, NewTreeNode[Node](group, 0))
groupNodes = append(groupNodes, NewTreeNode(group, 0))
}
return groupNodes
}
Expand All @@ -60,6 +60,6 @@ func (s *Service) SaveGroup(group Group) error {
return err
}
// FIXME set correct depth
s.nodes[group.ID] = NewTreeNode[Node](&group, 0)
s.nodes[group.ID] = NewTreeNode(&group, 0)
return nil
}
8 changes: 3 additions & 5 deletions gohost/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import (
type Host interface {
Node
GetID() string
GetName() string
GetContent() []byte
SetContent([]byte)
GetDesc() string
IsEnabled() bool
IsEditable() bool
}
Expand All @@ -34,16 +32,16 @@ func (s *Service) LoadHosts(groupID string) []Host {
return s.loadLocalHosts(groupID)
}

func (s *Service) LoadHostNodes(groupID string) []*TreeNode[Node] {
func (s *Service) LoadHostNodes(groupID string) []*TreeNode {
groupNode := s.nodes[groupID]
if groupNode == nil {
return nil
}
hostNodeDepth := groupNode.Depth + 1
hosts := s.LoadHosts(groupID)
hostNodes := make([]*TreeNode[Node], 0, len(hosts))
hostNodes := make([]*TreeNode, 0, len(hosts))
for _, host := range hosts {
node := NewTreeNode[Node](host, hostNodeDepth)
node := NewTreeNode(host, hostNodeDepth)
hostNodes = append(hostNodes, node)
}
return hostNodes
Expand Down
20 changes: 8 additions & 12 deletions gohost/local_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ type LocalHost struct {
Enabled bool
}

func (h *LocalHost) Title() string {
return h.Name
}

func (h *LocalHost) Description() string {
return h.Desc
}

func (h *LocalHost) IsEditable() bool {
return true
}
Expand All @@ -19,10 +27,6 @@ func (h *LocalHost) GetID() string {
return h.ID
}

func (h *LocalHost) GetName() string {
return h.Name
}

func (h *LocalHost) GetContent() []byte {
return h.Content
}
Expand All @@ -31,14 +35,6 @@ func (h *LocalHost) SetContent(content []byte) {
h.Content = content
}

func (h *LocalHost) GetDesc() string {
return h.Desc
}

func (h *LocalHost) GetGroupID() string {
return h.GroupID
}

func (h *LocalHost) IsEnabled() bool {
return h.Enabled
}
Expand Down
20 changes: 10 additions & 10 deletions gohost/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ package gohost
import "github.com/charmbracelet/bubbles/list"

type Node interface {
list.Item
list.DefaultItem
GetID() string
GetParentID() string
}

type TreeNode[T Node] struct {
Node T
Children []*TreeNode[T]
type TreeNode struct {
Node
Children []*TreeNode
Depth int
IsFolded bool
}

func NewTreeNode[T Node](data T, depth int) *TreeNode[T] {
return &TreeNode[T]{
func NewTreeNode(data Node, depth int) *TreeNode {
return &TreeNode{
Node: data,
Children: make([]*TreeNode[T], 0),
Children: make([]*TreeNode, 0),
Depth: depth,
IsFolded: true,
}
}

func (n *TreeNode[T]) FilterValue() string {
func (n *TreeNode) FilterValue() string {
return n.Node.FilterValue()
}

func (n *TreeNode[T]) GetID() string {
func (n *TreeNode) GetID() string {
return n.Node.GetID()
}

func (n *TreeNode[T]) GetParentID() string {
func (n *TreeNode) GetParentID() string {
return n.Node.GetParentID()
}
22 changes: 11 additions & 11 deletions gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ func GetService() *Service {
func NewService() *Service {
return &Service{
store: db.Instance(),
nodes: make(map[string]*TreeNode[Node], 0),
tree: make([]*TreeNode[Node], 0),
SysHostNode: NewTreeNode[Node](SysHost(), 0),
nodes: make(map[string]*TreeNode, 0),
tree: make([]*TreeNode, 0),
SysHostNode: NewTreeNode(SysHost(), 0),
}
}

type Service struct {
store *db.Store
nodes map[string]*TreeNode[Node]
tree []*TreeNode[Node]
SysHostNode *TreeNode[Node]
nodes map[string]*TreeNode
tree []*TreeNode
SysHostNode *TreeNode
}

// Tree the system host tree node is always first
func (s *Service) Tree() []*TreeNode[Node] {
func (s *Service) Tree() []*TreeNode {
return s.tree
}

func (s *Service) cacheNodes(nodes []*TreeNode[Node]) {
func (s *Service) cacheNodes(nodes []*TreeNode) {
for _, node := range nodes {
s.nodes[node.GetID()] = node
}
}

func (s *Service) buildTree(nodes []*TreeNode[Node]) {
func (s *Service) buildTree(nodes []*TreeNode) {
// Build tree
for _, node := range nodes {
p, exist := s.nodes[node.Node.GetParentID()]
Expand All @@ -71,13 +71,13 @@ func (s *Service) buildTree(nodes []*TreeNode[Node]) {
}

func (s *Service) Load() {
nodes := []*TreeNode[Node]{s.SysHostNode}
nodes := []*TreeNode{s.SysHostNode}
nodes = append(nodes, s.loadGroupNodes()...)
s.cacheNodes(nodes)
s.buildTree(nodes)
}

func (s *Service) ChildNodes(nodeID string) []*TreeNode[Node] {
func (s *Service) ChildNodes(nodeID string) []*TreeNode {
return s.nodes[nodeID].Children
}

Expand Down
8 changes: 8 additions & 0 deletions gohost/sys_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ type sysHost struct {
desc string
}

func (s *sysHost) Title() string {
return s.name
}

func (s *sysHost) Description() string {
return s.desc
}

func (s *sysHost) IsEditable() bool {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (v *EditorView) SetHost(host gohost.Host) {
}

func (v *EditorView) RefreshStatusLine() {
v.statusLine = fmt.Sprintf("file: %s, saved: %t\n", v.host.GetName(), v.IsSaved())
v.statusLine = fmt.Sprintf("file: %s, saved: %t\n", v.host.Title(), v.IsSaved())
}

func (v *EditorView) IsSaved() bool {
Expand Down
15 changes: 8 additions & 7 deletions tui/group_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type groupItemDelegate struct {
}

func (d groupItemDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
node, ok := item.(*gohost.TreeNode[gohost.Node])
node, ok := item.(*gohost.TreeNode)
if !ok {
return
}
Expand All @@ -31,7 +31,7 @@ func (d groupItemDelegate) Render(w io.Writer, m list.Model, index int, item lis
case gohost.Group:
str += fmt.Sprintf("%s[G] %d. %s", spaces, index, node.Name)
case gohost.Host:
str += fmt.Sprintf("%s[L] %d. %s", spaces, index, node.GetName())
str += fmt.Sprintf("%s[L] %d. %s", spaces, index, node.Title())
}
_, _ = fmt.Fprint(w, str)
}
Expand All @@ -52,7 +52,7 @@ func (d groupItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
type GroupView struct {
model *Model
groupList list.Model
selectedNode *gohost.TreeNode[gohost.Node]
selectedNode *gohost.TreeNode
selectedIndex int
selectedGroup gohost.Group
selectedHost gohost.Host
Expand All @@ -68,7 +68,8 @@ func NewGroupView(model *Model) *GroupView {
groups := util.WrapSlice[list.Item](treeNodes)

// Create nodes list view
groupList := list.New(groups, groupItemDelegate{}, 0, 0)
//groupList := list.New(groups, groupItemDelegate{}, 0, 0)
groupList := list.New(groups, list.NewDefaultDelegate(), 0, 0)
// TODO add remaining help key
groupList.Title = "Groups"
groupList.SetShowHelp(false)
Expand Down Expand Up @@ -99,7 +100,7 @@ func (v *GroupView) Update(msg tea.Msg) []tea.Cmd {
case key.Matches(m, keys.Enter):
selectedItem := v.groupList.SelectedItem()
if selectedItem != nil {
v.selectedNode = selectedItem.(*gohost.TreeNode[gohost.Node])
v.selectedNode = selectedItem.(*gohost.TreeNode)
v.selectedIndex = v.groupList.Index()
switch v.selectedNode.Node.(type) {
case gohost.Group:
Expand Down Expand Up @@ -153,7 +154,7 @@ func (v *GroupView) foldSelectedGroup() {
if items[next] == nil {
break
}
node := items[next].(*gohost.TreeNode[gohost.Node])
node := items[next].(*gohost.TreeNode)
if node.Depth > v.selectedNode.Depth {
node.IsFolded = true
v.groupList.RemoveItem(next)
Expand All @@ -165,7 +166,7 @@ func (v *GroupView) foldSelectedGroup() {

func (v *GroupView) onHostNodeSelected(cmds *[]tea.Cmd) {
v.selectedHost = v.selectedNode.Node.(gohost.Host)
v.model.Log("select host: " + v.selectedHost.GetName())
v.model.Log("select host: " + v.selectedHost.Title())
v.model.SwitchState(editorViewState)
v.model.editorView.SetHost(v.selectedHost)
}
10 changes: 8 additions & 2 deletions tui/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type keyMaps struct {
Enter key.Binding
Switch key.Binding
Save key.Binding
New key.Binding
}

func newKeys() keyMaps {
Expand All @@ -37,7 +38,7 @@ func newKeys() keyMaps {
key.WithHelp("?", "toggle help"),
),
Quit: key.NewBinding(
key.WithKeys( "ctrl+c"),
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "quit"),
),
Enter: key.NewBinding(
Expand All @@ -52,6 +53,10 @@ func newKeys() keyMaps {
key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "save"),
),
New: key.NewBinding(
key.WithKeys("ctrl+n"),
key.WithHelp("ctrl+n", "new"),
),
}
}

Expand All @@ -63,7 +68,8 @@ func (k keyMaps) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Up, k.Down}, // column
{k.Left, k.Right},
{k.Switch},
{k.Switch, k.New},
{k.Save},
{k.Help, k.Quit},
}
}
2 changes: 1 addition & 1 deletion tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type sessionState int
const (
groupViewState = iota
editorViewState
sysHostViewState
nodeViewState
lastState
)

Expand Down
4 changes: 4 additions & 0 deletions tui/node_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tui

type NodeView struct {
}

0 comments on commit 53472df

Please sign in to comment.