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

Commit

Permalink
rm unused sort
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 7, 2022
1 parent f7c0304 commit 163d024
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module gohost
go 1.18

require (
github.com/charmbracelet/bubbles v0.13.0
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.22.1
github.com/charmbracelet/lipgloss v0.5.0
github.com/spf13/cobra v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/bubbles v0.13.0 h1:zP/ROH3wJEBqZWKIsD50ZKKlx3ydLInq3LdD/Nrlb8w=
github.com/charmbracelet/bubbles v0.13.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWoiNibae+1yCMtcc=
github.com/charmbracelet/bubbles v0.14.0 h1:DJfCwnARfWjZLvMglhSQzo76UZ2gucuHPy9jLWX45Og=
github.com/charmbracelet/bubbles v0.14.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWoiNibae+1yCMtcc=
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/bubbletea v0.22.1 h1:z66q0LWdJNOWEH9zadiAIXp2GN1AWrwNXU8obVY9X24=
github.com/charmbracelet/bubbletea v0.22.1/go.mod h1:8/7hVvbPN6ZZPkczLiB8YpLkLJ0n7DMho5Wvfd2X1C0=
Expand Down
1 change: 0 additions & 1 deletion gohost/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (s *Service) loadGroups() []Group {

func (s *Service) loadGroupNodes() []*TreeNode[Node] {
groups := s.loadGroups()

groupNodes := make([]*TreeNode[Node], 0, len(groups))
for _, group := range groups {
groupNodes = append(groupNodes, NewTreeNode[Node](group, 0))
Expand Down
26 changes: 10 additions & 16 deletions gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"gohost/config"
"gohost/db"
"os"
"sort"
"sync"
)

Expand Down Expand Up @@ -36,6 +35,7 @@ type Service struct {
SysHostNode *TreeNode[Node]
}

// Tree the system host tree node is always first
func (s *Service) Tree() []*TreeNode[Node] {
return s.tree
}
Expand All @@ -46,9 +46,9 @@ func (s *Service) cacheNodes(nodes []*TreeNode[Node]) {
}
}

func (s *Service) buildTree() {
func (s *Service) buildTree(nodes []*TreeNode[Node]) {
// Build tree
for _, node := range s.nodes {
for _, node := range nodes {
p, exist := s.nodes[node.Node.GetParentID()]
if !exist {
s.tree = append(s.tree, node)
Expand All @@ -58,19 +58,13 @@ func (s *Service) buildTree() {
p.Children = append(p.Children, node)
}
// Bfs to set depth
sort.Slice(s.tree, func(i, j int) bool {
return s.tree[i].Node.GetID() < s.tree[j].GetID()
})
nodes := s.tree
queue := s.tree
depth := 0
for len(nodes) > 0 {
for _, node := range nodes {
node.Depth = depth
sort.Slice(node.Children, func(i, j int) bool {
return node.Children[i].GetID() < node.Children[j].GetID()
})
nodes = append(nodes, node.Children...)
nodes = nodes[1:]
for len(queue) > 0 {
for _, treeNode := range queue {
treeNode.Depth = depth
queue = append(queue, treeNode.Children...)
queue = queue[1:]
}
depth++
}
Expand All @@ -80,7 +74,7 @@ func (s *Service) Load() {
nodes := []*TreeNode[Node]{s.SysHostNode}
nodes = append(nodes, s.loadGroupNodes()...)
s.cacheNodes(nodes)
s.buildTree()
s.buildTree(nodes)
}

func (s *Service) ChildNodes(nodeID string) []*TreeNode[Node] {
Expand Down
3 changes: 3 additions & 0 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type EditorView struct {
func NewTextView(model *Model) *EditorView {
hostEditor := textarea.New()
hostEditor.ShowLineNumbers = true
hostEditor.CharLimit = 0
return &EditorView{
model: model,
hostEditor: hostEditor,
Expand Down Expand Up @@ -66,6 +67,7 @@ func (v *EditorView) Update(msg tea.Msg) []tea.Cmd {
// Disable key
msg = nil
}
v.statusLine = "hit key: " + m.String()
}
//v.RefreshStatusLine()
v.hostEditor, cmd = v.hostEditor.Update(msg)
Expand All @@ -86,6 +88,7 @@ func (v *EditorView) Blur() {

func (v *EditorView) SetHost(host gohost.Host) {
v.host = host
v.hostEditor.Reset()
v.hostEditor.SetValue(string(host.GetContent()))
v.prevLen = v.hostEditor.Length()
}
Expand Down
5 changes: 3 additions & 2 deletions tui/group_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func NewGroupView(model *Model) *GroupView {
// Get nodes service
service := gohost.GetService()
service.Load()
groups := util.WrapSlice[list.Item](service.Tree())
treeNodes := service.Tree()
groups := util.WrapSlice[list.Item](treeNodes)

// Create nodes list view
groupList := list.New(groups, groupItemDelegate{}, 0, 0)
Expand All @@ -75,7 +76,7 @@ func NewGroupView(model *Model) *GroupView {
return &GroupView{
model: model,
groupList: groupList,
selectedNode: service.SysHostNode,
selectedNode: treeNodes[0],
service: service,
}
}
Expand Down

0 comments on commit 163d024

Please sign in to comment.