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

Commit

Permalink
fix list item render
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 13, 2022
1 parent aa97d8e commit 8738664
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 37 deletions.
40 changes: 27 additions & 13 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/gohost"
"gohost/log"
"gohost/tui/keys"
)

type EditorView struct {
model *Model
hostEditor textarea.Model
host gohost.Host
statusLine string
saved bool
prevLen int
model *Model
hostEditor textarea.Model
host gohost.Host
statusLine string
statusMsg string
saved bool
prevLen int
width, height int
}

func NewTextView(model *Model) *EditorView {
Expand All @@ -28,8 +31,11 @@ func NewTextView(model *Model) *EditorView {
hostEditor: hostEditor,
host: nil,
statusLine: "",
statusMsg: "",
saved: true,
prevLen: 0,
width: 0,
height: 0,
}
}

Expand Down Expand Up @@ -59,7 +65,8 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch m := msg.(type) {
case tea.WindowSizeMsg:
v.hostEditor.SetHeight(m.Height)
v.width, v.height = m.Width, m.Height
v.hostEditor.SetHeight(m.Height - 2)
v.hostEditor.SetWidth(m.Width)
log.Debug(fmt.Sprintf("editor view w %d h %d", m.Width, m.Height))
case tea.KeyMsg:
Expand All @@ -79,10 +86,9 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
v.SetSaved()
}
} else {
log.Debug("Can not edit this")
v.statusMsg = "Can not edit this"
}
}
v.statusLine = "hit key: " + m.String()
}
v.RefreshStatusLine()
v.hostEditor, cmd = v.hostEditor.Update(msg)
Expand All @@ -91,9 +97,13 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (v *EditorView) View() string {
// TODO add status line for editor view
//return lipgloss.JoinVertical(lipgloss.Top, v.hostEditor.View(), v.statusLine)
return v.hostEditor.View()
var statusLine string
if len(v.statusLine) > v.width {
statusLine = v.statusLine[:v.width-3] + "..."
} else {
statusLine = v.statusLine
}
return lipgloss.JoinVertical(lipgloss.Right, v.hostEditor.View(), "", statusLine)
}

func (v *EditorView) Focus() {
Expand All @@ -112,7 +122,11 @@ func (v *EditorView) SetHost(host gohost.Host) {
}

func (v *EditorView) RefreshStatusLine() {
//v.statusLine = fmt.Sprintf("file: %s, saved: %t\n", v.host.Title(), v.IsSaved())
if v.statusMsg == "" {
v.statusLine = fmt.Sprintf("[file: %s] [saved: %t]", v.host.Title(), v.IsSaved())
} else {
v.statusLine = fmt.Sprintf("[file: %s] [saved: %t] [info: %s]", v.host.Title(), v.IsSaved(), v.statusMsg)
}
}

func (v *EditorView) IsSaved() bool {
Expand Down
1 change: 1 addition & 0 deletions tui/main_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (m *Model) View() string {
case treeViewState:
v = lipgloss.JoinVertical(lipgloss.Left,
lipgloss.JoinHorizontal(lipgloss.Top,
//styles.FocusedView.Width(m.leftViewWidth).Render(m.treeView.View()),
styles.FocusedView.Render(m.treeView.View()),
styles.DefaultView.Render(m.editorView.View()),
),
Expand Down
2 changes: 2 additions & 0 deletions tui/styles/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ var (
BorderForeground(lipgloss.Color("73"))
UnfocusedFormItem = lipgloss.NewStyle()
FocusedFormItem = lipgloss.NewStyle().Foreground(lipgloss.Color("73"))

StatusLine = lipgloss.NewStyle()
)
69 changes: 45 additions & 24 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,55 @@ import (
type nodeItemDelegate struct {
selectedStyle lipgloss.Style
normalStyle lipgloss.Style
width int
}

func newNodeItemDelegate() *nodeItemDelegate {
return &nodeItemDelegate{
selectedStyle: styles.FocusedFormItem,
normalStyle: styles.UnfocusedFormItem,
width: 0,
}
}

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
switch node.Node.(type) {
case *gohost.Group:
var icon string
if node.IsFolded() {
icon = "📁"
if d.width <= 3 {
str = strings.Repeat(".", d.width)
} else {
switch node.Node.(type) {
case *gohost.Group:
var icon string
if node.IsFolded() {
icon = "📁"
} else {
icon = "📂"
}
str = strings.Repeat(" ", node.Depth()) + icon + node.Title()
case *gohost.SysHost:
str = strings.Repeat(" ", node.Depth()) + "🐝" + node.Title()
case *gohost.LocalHost:
str = strings.Repeat(" ", node.Depth()) + "📑" + node.Title()
case *gohost.RemoteHost:
str = strings.Repeat(" ", node.Depth()) + "🌐" + node.Title()
}
if m.Index() == index {
str = d.selectedStyle.Render("> " + str)
} else {
icon = "📂"
str = d.normalStyle.Render(" " + str)
}
if len(str) > d.width {
str = str[:d.width-3] + "..."
} else if len(str) < d.width {
str = str + strings.Repeat(" ", d.width-len(str))
}
str = strings.Repeat(" ", node.Depth()) + icon + node.Title()
case *gohost.SysHost:
str = strings.Repeat(" ", node.Depth()) + "🐝" + node.Title()
case *gohost.LocalHost:
str = strings.Repeat(" ", node.Depth()) + "📑" + node.Title()
case *gohost.RemoteHost:
str = strings.Repeat(" ", node.Depth()) + "🌐" + node.Title()
}
if m.Index() == index {
str = d.selectedStyle.Render("> " + str)
} else {
str = d.normalStyle.Render(" " + str)
}

_, _ = fmt.Fprint(w, str)
}

Expand All @@ -69,17 +82,23 @@ func (d *nodeItemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
return nil
}

func (d *nodeItemDelegate) SetWidth(width int) {
d.width = width
}

// TreeView is tui helpView for nodes tree
type TreeView struct {
model *Model
nodeList list.Model
width, height int
model *Model
nodeList list.Model
nodeItemDelegate *nodeItemDelegate
width, height int
}

func NewTreeView(model *Model) *TreeView {
// Create nodes list helpView
nodes := svc.TreeNodesAsItem()
nodeList := list.New(nodes, newNodeItemDelegate(), 0, 0)
delegate := newNodeItemDelegate()
nodeList := list.New(nodes, delegate, 0, 0)
nodeList.Title = "gohost"
nodeList.SetShowStatusBar(false)
nodeList.SetShowHelp(false)
Expand All @@ -90,8 +109,9 @@ func NewTreeView(model *Model) *TreeView {
nodeList.Select(0)

return &TreeView{
model: model,
nodeList: nodeList,
model: model,
nodeList: nodeList,
nodeItemDelegate: delegate,
}
}

Expand Down Expand Up @@ -168,6 +188,7 @@ func (v *TreeView) View() string {

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

Expand Down

0 comments on commit 8738664

Please sign in to comment.