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

Commit

Permalink
add node view
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 12, 2022
1 parent a809e8b commit 305dd38
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
30 changes: 14 additions & 16 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,24 @@ func NewNodeView(model *Model) *NodeView {
confirmButton.OnClick = func() tea.Cmd {
log.Debug(fmt.Sprintf("name %s, desc %s, url %s, choice %s",
nameTextInput.Value(), descTextInput.Value(), urlTextInput.Value(), nodeTypeChoices.SelectedItem()))
var cmd tea.Cmd
selectedNode := model.treeView.selectedNode
selectedNodeType := model.treeView.selectedNodeType
// Check inputs
if nodeTypeChoices.SelectedItem() == nil {
log.Debug(fmt.Sprintf("no node type was selected"))
return nil
}

// Get parent node
selectedNode := model.treeView.SelectedNode()
selectedNode.SetFolded(false)
var parent *gohost.TreeNode
switch selectedNodeType {
case NodeSysHost, NodeLocalHost, NodeRemoteHost:
parent = selectedNode.Parent()
case NodeGroup:
switch selectedNode.Node.(type) {
case *gohost.Group:
parent = selectedNode
case gohost.Host:
parent = selectedNode.Parent()
}

if nodeTypeChoices.SelectedItem() == nil {
log.Debug(fmt.Sprintf("no node type was selected"))
return nil
}

var cmd tea.Cmd
switch nodeTypeChoices.SelectedItem() {
case NodeGroup:
node := &gohost.Group{
Expand All @@ -70,10 +70,8 @@ func NewNodeView(model *Model) *NodeView {
groupNode := gohost.NewTreeNode(node)
groupNode.SetParent(parent)
groupNode.SetDepth(parent.Depth() + 1)
err := svc.SaveGroupNode(groupNode)
if err != nil {
// TODO display error in tui
log.Error(err)
if err := svc.SaveGroupNode(groupNode);err != nil {
panic(err)
}
cmd = model.treeView.RefreshTreeNodes()
case NodeLocalHost:
Expand Down
57 changes: 24 additions & 33 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ func (d *nodeItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {

// TreeView is tui helpView for nodes tree
type TreeView struct {
model *Model
nodeList list.Model
selectedNode *gohost.TreeNode
selectedNodeType *NodeType
selectedIndex int
width, height int
model *Model
nodeList list.Model
width, height int
}

func NewTreeView(model *Model) *TreeView {
Expand All @@ -86,12 +83,11 @@ func NewTreeView(model *Model) *TreeView {
nodeList.Title = "gohost"
nodeList.SetShowStatusBar(false)
nodeList.SetShowHelp(false)
nodeList.Select(0)

return &TreeView{
model: model,
nodeList: nodeList,
selectedNode: svc.SysHostNode,
selectedNodeType: NodeSysHost,
model: model,
nodeList: nodeList,
}
}

Expand All @@ -113,33 +109,25 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if v.model.state == treeViewState {
switch {
case key.Matches(m, keys.Enter):
selectedItem := v.nodeList.SelectedItem()
if selectedItem != nil {
v.selectedNode = selectedItem.(*gohost.TreeNode)
v.selectedIndex = v.nodeList.Index()
switch v.selectedNode.Node.(type) {
case *gohost.Group:
v.selectedNodeType = NodeGroup
v.selectedNode.FlipFolded()
cmd = v.RefreshTreeNodes()
case *gohost.SysHost:
v.selectedNodeType = NodeSysHost
v.onHostNodeSelected(&cmds)
case *gohost.LocalHost:
v.selectedNodeType = NodeLocalHost
v.onHostNodeSelected(&cmds)
case *gohost.RemoteHost:
v.selectedNodeType = NodeRemoteHost
v.onHostNodeSelected(&cmds)
}
selectedNode := v.SelectedNode()
switch node := selectedNode.Node.(type) {
case *gohost.Group:
selectedNode.FlipFolded()
cmd = v.RefreshTreeNodes()
case gohost.Host:
v.onHostNodeSelected(node, &cmds)
}
case key.Matches(m, keys.New):
v.model.switchState(nodeViewState)
}
} else {
// Disable key
msg = nil
}
}
v.nodeList, cmd = v.nodeList.Update(msg)
log.Debug(fmt.Sprintf("cursor at %d, selected item %v",
v.nodeList.Cursor(), v.nodeList.SelectedItem().FilterValue()))
cmds = append(cmds, cmd)
return v, tea.Batch(cmds...)
}
Expand Down Expand Up @@ -170,9 +158,12 @@ func (v *TreeView) RefreshTreeNodes() tea.Cmd {
return v.nodeList.SetItems(svc.TreeNodeItem())
}

func (v *TreeView) onHostNodeSelected(cmds *[]tea.Cmd) {
selectedHost := v.selectedNode.Node.(gohost.Host)
log.Debug("select host: " + selectedHost.Title())
func (v *TreeView) SelectedNode() *gohost.TreeNode {
return v.nodeList.SelectedItem().(*gohost.TreeNode)
}

func (v *TreeView) onHostNodeSelected(host gohost.Host, cmds *[]tea.Cmd) {
log.Debug("select host: " + host.Title())
v.model.switchState(editorViewState)
v.model.editorView.SetHost(selectedHost)
v.model.editorView.SetHost(host)
}

0 comments on commit 305dd38

Please sign in to comment.