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

Commit

Permalink
fix tree item render width bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 14, 2022
1 parent 87ffd85 commit 4240601
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/.idea
/debug.log
/gohost.exe
/output
/build
26 changes: 9 additions & 17 deletions gohost/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (s *Service) EnableHost() {
}

func (s *Service) UnfoldNode(treeNode *TreeNode) {
if !s.foldableNode(treeNode) {
if !s.isFoldableNode(treeNode) {
return
}
treeNode.SetFolded(false)
Expand All @@ -187,26 +187,15 @@ func (s *Service) UnfoldNode(treeNode *TreeNode) {
}

func (s *Service) FoldNode(treeNode *TreeNode) {
if !s.foldableNode(treeNode) {
if !s.isFoldableNode(treeNode) {
return
}
treeNode.SetFolded(true)
s.RemoveNodesByParent(treeNode)
s.UpdateGroupNode(treeNode)
}

func (s *Service) FlipFoldNode(treeNode *TreeNode) {
if !s.foldableNode(treeNode) {
return
}
if treeNode.IsFolded() {
s.UnfoldNode(treeNode)
} else {
s.FoldNode(treeNode)
}
}

func (s *Service) foldableNode(treeNode *TreeNode) bool {
func (s *Service) isFoldableNode(treeNode *TreeNode) bool {
if treeNode == nil || treeNode == s.SysHostNode {
return false
}
Expand All @@ -218,8 +207,11 @@ func (s *Service) EnableNode(node *TreeNode) {
if node == nil || node == s.SysHostNode {
return
}
node.SetEnabled(!node.IsEnabled())
node.SetEnabled(true)
s.UpdateNode(node)
// TODO Load all enable nodes

if s.isFoldableNode(node) {
for _, child := range node.Children() {
s.EnableNode(child)
}
}
}
9 changes: 9 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.PHONY: linux windows macos

linux:
GOOS=linux GOARCH=amd64 go build -o build/gohost

windows:
GOOS=windows GOARCH=amd64 go build -o build/gohost.exe

macos:
31 changes: 21 additions & 10 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,26 @@ func (d *nodeItemDelegate) Render(w io.Writer, m list.Model, index int, item lis
}

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

if len(str) > d.width {
strLen := lipgloss.Width(str)
if strLen > d.width {
if d.width <= 3 {
str = strings.Repeat(" ", d.width)
str = strings.Repeat(".", d.width)
} else {
str = str[:d.width-3] + "..."
}
} else if len(str) < d.width {
str = str + strings.Repeat(" ", d.width-len(str))
} else {
str = str + strings.Repeat(" ", d.width-strLen)
}

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

_, _ = fmt.Fprint(w, str)
Expand Down Expand Up @@ -149,7 +156,11 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return v, nil
case key.Matches(m, keys.Enter):
cmd = func() tea.Msg {
svc.FlipFoldNode(selectedNode)
if selectedNode.IsFolded() {
svc.UnfoldNode(selectedNode)
} else {
svc.FoldNode(selectedNode)
}
// Switch to editor view if selected host node
host, ok := selectedNode.Node.(gohost.Host)
if !ok {
Expand All @@ -163,7 +174,7 @@ func (v *TreeView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(m, keys.Create):
cmd = func() tea.Msg {
v.model.switchState(nodeViewState)
svc.FlipFoldNode(selectedNode)
svc.UnfoldNode(selectedNode)
return RefreshTreeViewItems{}
}
case key.Matches(m, keys.Delete):
Expand Down Expand Up @@ -192,9 +203,9 @@ func (v *TreeView) View() string {
}

func (v *TreeView) SetWidth(width int) {
v.nodeList.SetWidth(width)
v.nodeItemDelegate.SetWidth(width)
v.width = width
v.nodeList.SetWidth(v.width)
v.nodeItemDelegate.SetWidth(v.width)
}

func (v *TreeView) SetHeight(height int) {
Expand Down

0 comments on commit 4240601

Please sign in to comment.