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

Commit

Permalink
add enable func
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 13, 2022
1 parent a0eb4fb commit a849360
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
1 change: 0 additions & 1 deletion gohost/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Node interface {
const (
MaskFold = 1 << iota
MaskEnable
MaskEditable
)

type TreeNode struct {
Expand Down
6 changes: 6 additions & 0 deletions gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ func (s *Service) DeleteNode(node *TreeNode) {
}
}

func (s *Service) UpdateNode(node *TreeNode) {
if err := s.store.Update(node.GetID(), node.Node); err != nil {
panic(err)
}
}

func (s *Service) ApplyHost(hosts []byte) {
// Truncate system host file
sysHostFile, err := os.Create(cfg.SysHostFile)
Expand Down
1 change: 1 addition & 0 deletions gohost/sys_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func SysHostInstance() Host {
sysHostInstance = &SysHost{
Name: "System Host",
Desc: cfg.SysHostFile,
Flag: MaskEnable,
}
})
return sysHostInstance
Expand Down
2 changes: 1 addition & 1 deletion tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(m, keys.Save):
if v.host.IsEditable() {
v.host.SetContent([]byte(v.hostEditor.Value()))
err := gohost.GetService().UpdateHost(v.host)
err := svc.UpdateHost(v.host)
if err != nil {
log.Debug(err.Error())
} else {
Expand Down
28 changes: 19 additions & 9 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,41 @@ func newNodeItemDelegate() *nodeItemDelegate {
}

func (d *nodeItemDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {

node, ok := item.(*gohost.TreeNode)
if !ok {
return
}

var str string
if node.IsEnabled() {
str = "+" + str
} else {
str = "-" + str
}

switch node.Node.(type) {
case *gohost.Group:
var icon string
if node.IsFolded() {
icon = "/ "
} else {
icon = "| "
} else {
icon = "/ "
}
str = strings.Repeat(" ", node.Depth()) + icon + node.Title()
str = strings.Repeat(" ", node.Depth()) + str + icon + node.Title()
case *gohost.SysHost:
str = strings.Repeat(" ", node.Depth()) + "* " + node.Title()
str = strings.Repeat(" ", node.Depth()) + str + "* " + node.Title()
case *gohost.LocalHost:
str = strings.Repeat(" ", node.Depth()) + "# " + node.Title()
str = strings.Repeat(" ", node.Depth()) + str + "# " + node.Title()
case *gohost.RemoteHost:
str = strings.Repeat(" ", node.Depth()) + "@" + node.Title()
str = strings.Repeat(" ", node.Depth()) + str + "@" + node.Title()
}

if m.Index() == index {
str = d.selectedStyle.Render("> " + str)
} else {
str = d.normalStyle.Render(" " + str)
}

if len(str) > d.width {
if d.width <= 3 {
str = strings.Repeat(" ", d.width)
Expand Down Expand Up @@ -135,11 +143,11 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if v.model.state != treeViewState {
return v, nil
}
selectedNode := v.SelectedNode()
switch {
case key.Matches(m, keys.Esc):
return v, nil
case key.Matches(m, keys.Enter):
selectedNode := v.SelectedNode()
switch node := selectedNode.Node.(type) {
case *gohost.Group:
cmd = func() tea.Msg {
Expand Down Expand Up @@ -168,10 +176,12 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case key.Matches(m, keys.Delete):
cmd = func() tea.Msg {
selectedNode := v.SelectedNode()
svc.DeleteNode(selectedNode)
return RefreshTreeViewItems{}
}
case key.Matches(m, keys.Apply):
selectedNode.SetEnabled(!selectedNode.IsEnabled())
svc.UpdateNode(selectedNode)
}
}

Expand Down

0 comments on commit a849360

Please sign in to comment.