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

Commit

Permalink
use node flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 13, 2022
1 parent 8738664 commit a674dc2
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 55 deletions.
31 changes: 23 additions & 8 deletions gohost/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ type Group struct {
ParentID db.ID
Name string
Desc string
Flag int
}

func (g *Group) SetFlag(flag int) {
g.Flag = flag
}

func (g *Group) GetFlag() int {
return g.Flag
}

func (g *Group) Title() string {
Expand Down Expand Up @@ -63,20 +72,26 @@ func (s *Service) loadGroupNodesByParent(parent *TreeNode) []*TreeNode {
}

func (s *Service) SaveGroup(group *Group) error {
err := s.store.Insert(s.extractID(group), group)
if err != nil {
return err
}
return nil
return s.store.Insert(s.extractID(group), group)
}

func (s *Service) SaveGroupNode(groupNode *TreeNode) error {
func (s *Service) SaveGroupNode(groupNode *TreeNode) {
group := groupNode.Node.(*Group)
if err := s.SaveGroup(group); err != nil {
return err
panic(err)
}
s.nodes[groupNode.GetID()] = groupNode
return nil
}

func (s *Service) UpdateGroup(group *Group) error {
return s.store.Update(group.GetID(), group)
}

func (s *Service) UpdateGroupNode(groupNode *TreeNode) {
group := groupNode.Node.(*Group)
if err := s.UpdateGroup(group); err != nil {
panic(err)
}
}

func (s *Service) DeleteGroup(id db.ID) error {
Expand Down
1 change: 0 additions & 1 deletion gohost/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ type Host interface {
Node
GetContent() []byte
SetContent([]byte)
IsEnabled() bool
IsEditable() bool
}

Expand Down
14 changes: 9 additions & 5 deletions gohost/local_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ type LocalHost struct {
Name string
Content []byte
Desc string
Enabled bool
Flag int
}

func (h *LocalHost) SetFlag(flag int) {
h.Flag = flag
}

func (h *LocalHost) GetFlag() int {
return h.Flag
}

func (h *LocalHost) Title() string {
Expand All @@ -41,10 +49,6 @@ func (h *LocalHost) SetContent(content []byte) {
h.Content = content
}

func (h *LocalHost) IsEnabled() bool {
return h.Enabled
}

func (h *LocalHost) FilterValue() string {
return h.Name
}
Expand Down
34 changes: 29 additions & 5 deletions gohost/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ type Node interface {
list.DefaultItem
GetID() db.ID
GetParentID() db.ID
GetFlag() int
SetFlag(flag int)
}

const (
MaskFold = 1 << iota
MaskEnable
MaskEditable
)

type TreeNode struct {
Node
parent *TreeNode
children []*TreeNode
depth int
isFolded bool
}

func NewTreeNode(node Node) *TreeNode {
Expand All @@ -25,7 +32,6 @@ func NewTreeNode(node Node) *TreeNode {
parent: nil,
children: make([]*TreeNode, 0),
depth: 0,
isFolded: true,
}
}

Expand Down Expand Up @@ -72,9 +78,27 @@ func (n *TreeNode) SetDepth(depth int) {
}

func (n *TreeNode) IsFolded() bool {
return n.isFolded
return n.Node.GetFlag()&MaskFold == MaskFold
}

func (n *TreeNode) SetFolded(folded bool) {
flag := n.Node.GetFlag()
if folded {
n.Node.SetFlag(flag | MaskFold)
} else {
n.Node.SetFlag(flag & (^MaskFold))
}
}

func (n *TreeNode) IsEnabled() bool {
return n.Node.GetFlag()&MaskEnable == MaskEnable
}

func (n *TreeNode) SetFolded(isFolded bool) {
n.isFolded = isFolded
func (n *TreeNode) SetEnabled(enabled bool) {
flag := n.Node.GetFlag()
if enabled {
n.Node.SetFlag(flag | MaskEnable)
} else {
n.Node.SetFlag(flag & (^MaskEnable))
}
}
20 changes: 20 additions & 0 deletions gohost/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gohost

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMasks(t *testing.T) {
a := assert.New(t)
node := NewTreeNode(&Group{})
a.False(node.IsEnabled())
node.SetEnabled(true)
a.True(node.IsEnabled())

a.False(node.IsFolded())
node.SetFolded(true)
a.True(node.IsFolded())
node.SetFolded(false)
a.False(node.IsFolded())
}
27 changes: 27 additions & 0 deletions gohost/remote_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,36 @@ import (
"gohost/db"
)

var _ Host = (*RemoteHost)(nil)

type RemoteHost struct {
}

func (r *RemoteHost) SetFlag(flag int) {
//TODO implement me
panic("implement me")
}

func (r *RemoteHost) GetFlag() int {
//TODO implement me
panic("implement me")
}

func (r *RemoteHost) GetContent() []byte {
//TODO implement me
panic("implement me")
}

func (r *RemoteHost) SetContent(bytes []byte) {
//TODO implement me
panic("implement me")
}

func (r *RemoteHost) IsEditable() bool {
//TODO implement me
panic("implement me")
}

func (r *RemoteHost) FilterValue() string {
//TODO implement me
panic("implement me")
Expand Down
64 changes: 32 additions & 32 deletions gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
func GetService() *Service {
serviceOnce.Do(func() {
service = NewService()
service.loadRootNodes()
service.LoadNodesByParent(service.tree)
})
return service
}
Expand All @@ -28,10 +28,10 @@ func NewService() *Service {
nodes: make(map[db.ID]*TreeNode, 0),
}
s.tree = &TreeNode{
Node: &Group{ID: 0},
parent: nil,
children: make([]*TreeNode, 0),
depth: -1,
isFolded: false,
}
sysHostNode := NewTreeNode(SysHostInstance())
sysHostNode.SetParent(s.tree)
Expand Down Expand Up @@ -98,19 +98,21 @@ func (s *Service) treeNodesAsItem(nodes []*TreeNode, res *[]list.Item) {
}
}

func (s *Service) loadRootNodes() []*TreeNode {
return s.LoadNodesByParent(s.tree)
}

func (s *Service) LoadNodesByParent(parent *TreeNode) []*TreeNode {
var nodes []*TreeNode
var children []*TreeNode
if parent == s.tree {
nodes = append(nodes, s.SysHostNode)
children = append(children, s.SysHostNode)
}
children = append(children, s.loadGroupNodesByParent(parent)...)
children = append(children, s.loadLocalHostNodesByParent(parent)...)
parent.SetChildren(children)
s.cacheNodes(children)
nodes := append([]*TreeNode{}, children...)
for _, node := range children {
if !node.IsFolded() {
nodes = append(nodes, s.LoadNodesByParent(node)...)
}
}
nodes = append(nodes, s.loadGroupNodesByParent(parent)...)
nodes = append(nodes, s.loadLocalHostNodesByParent(parent)...)
parent.SetChildren(nodes)
s.cacheNodes(nodes)
return nodes
}

Expand All @@ -122,26 +124,6 @@ func (s *Service) RemoveNodesByParentID(parentID db.ID) {
node.SetChildren(nil)
}

// ApplyHost TODO apply host to system
func (s *Service) ApplyHost(hosts []byte) {
// open system host file
sysHostFile, err := os.Create(cfg.SysHostFile)
if err != nil {
panic(err)
}
defer sysHostFile.Close()

// write hosts to system host file
if _, err = sysHostFile.Write(hosts); err != nil {
panic(err)
}
}

func (s *Service) CombineHost(hosts ...[]byte) []byte {
// TODO combine host
return nil
}

func (s *Service) extractID(node Node) db.ID {
if node.GetID() == 0 {
return s.store.NextID()
Expand Down Expand Up @@ -174,3 +156,21 @@ func (s *Service) DeleteNode(node *TreeNode) {

}
}

func (s *Service) ApplyHost(hosts []byte) {
// Truncate system host file
sysHostFile, err := os.Create(cfg.SysHostFile)
if err != nil {
panic(err)
}
defer sysHostFile.Close()
// Write hosts to system host file
if _, err = sysHostFile.Write(hosts); err != nil {
panic(err)
}
}

func (s *Service) EnableHost() {
// TODO enable as group node
// TODO enable as localhost node
}
9 changes: 9 additions & 0 deletions gohost/sys_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ type SysHost struct {
ID db.ID `boltholdKey:"ID"`
Name string
Desc string
Flag int
}

func (s *SysHost) SetFlag(flag int) {
s.Flag = flag
}

func (s *SysHost) GetFlag() int {
return s.Flag
}

func (s *SysHost) Title() string {
Expand Down
5 changes: 1 addition & 4 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,13 @@ func NewNodeView(model *Model) *NodeView {
}
groupNode := gohost.NewTreeNode(group)
groupNode.SetParent(parent)
if err := svc.SaveGroupNode(groupNode); err != nil {
panic(err)
}
svc.SaveGroupNode(groupNode)
case NodeLocalHost:
localHost := &gohost.LocalHost{
GroupID: parent.GetID(),
Name: nameTextInput.Value(),
Content: nil,
Desc: descTextInput.Value(),
Enabled: false,
}
localHostNode := gohost.NewTreeNode(localHost)
localHostNode.SetParent(parent)
Expand Down
1 change: 1 addition & 0 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
selectedNode.SetFolded(true)
svc.RemoveNodesByParentID(selectedNode.GetID())
}
svc.UpdateGroupNode(selectedNode)
return RefreshTreeViewItems{}
}
case gohost.Host:
Expand Down

0 comments on commit a674dc2

Please sign in to comment.