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

Commit

Permalink
add node types
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 8, 2022
1 parent e035731 commit 62b657e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tui/node_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tui

var (
GroupNode = &NodeType{"Group", "Group contains some hosts"}
LocalHost = &NodeType{"Local Host", "Host stored in local database"}
RemoteHost = &NodeType{"Remote Host", "Host from internet"}
)

type NodeType struct {
Name string
Desc string
}

func (n *NodeType) FilterValue() string {
return n.Name
}

func (n *NodeType) Title() string {
return n.Name
}

func (n *NodeType) Description() string {
return n.Desc
}
14 changes: 14 additions & 0 deletions tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"gohost/tui/styles"
Expand All @@ -13,9 +14,11 @@ type NodeView struct {
preFocusIdx int
focusIdx int
inputs []textinput.Model
nodeTypes list.Model
}

func NewNodeView(model *Model) *NodeView {
// Text inputs
nodeNameTextInput := textinput.New()
nodeNameTextInput.Prompt = "Name: "
nodeNameTextInput.Focus()
Expand All @@ -25,11 +28,15 @@ func NewNodeView(model *Model) *NodeView {
descTextInput := textinput.New()
descTextInput.Prompt = "Description: "

// Node type choices
nodeTypes := list.New([]list.Item{GroupNode, LocalHost, RemoteHost}, list.NewDefaultDelegate(), 20, 20)

view := &NodeView{
model: model,
preFocusIdx: 0,
focusIdx: 0,
inputs: []textinput.Model{nodeNameTextInput, descTextInput},
nodeTypes: nodeTypes,
}

return view
Expand All @@ -43,6 +50,9 @@ func (v *NodeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
v.nodeTypes.SetHeight(m.Height - v.model.reservedHeight)
v.nodeTypes.SetWidth(m.Width / 3 * 2)
case tea.KeyMsg:
if v.model.state == nodeViewState {
switch {
Expand All @@ -61,6 +71,8 @@ func (v *NodeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
v.inputs[i], cmd = v.inputs[i].Update(msg)
cmds = append(cmds, cmd)
}
v.nodeTypes, cmd = v.nodeTypes.Update(msg)
cmds = append(cmds, cmd)
return v, tea.Batch(cmds...)
}

Expand All @@ -72,6 +84,8 @@ func (v *NodeView) View() string {
b.WriteString(cfg.LineBreak)
}
}
b.WriteString("\n")
b.WriteString(v.nodeTypes.View())
return b.String()
}

Expand Down

0 comments on commit 62b657e

Please sign in to comment.