add scrollable functionality for fstree
This commit is contained in:
+32
-1
@@ -52,6 +52,8 @@ type FsTree struct {
|
|||||||
selectedNode *FsNode
|
selectedNode *FsNode
|
||||||
hoveredNode *FsNode
|
hoveredNode *FsNode
|
||||||
errMsg string
|
errMsg string
|
||||||
|
height int
|
||||||
|
width int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== Bubble Tea Interface Implementation ====================
|
// ==================== Bubble Tea Interface Implementation ====================
|
||||||
@@ -61,6 +63,9 @@ func (t *FsTree) Init() tea.Cmd {
|
|||||||
|
|
||||||
func (t *FsTree) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (t *FsTree) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch m := msg.(type) {
|
switch m := msg.(type) {
|
||||||
|
case tea.WindowSizeMsg:
|
||||||
|
t.width = m.Width
|
||||||
|
t.height = m.Height
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
t.errMsg = ""
|
t.errMsg = ""
|
||||||
switch m.String() {
|
switch m.String() {
|
||||||
@@ -117,7 +122,33 @@ func (t *FsTree) View() string {
|
|||||||
|
|
||||||
builder := &strings.Builder{}
|
builder := &strings.Builder{}
|
||||||
t.renderNode(t.root, 0, builder)
|
t.renderNode(t.root, 0, builder)
|
||||||
return builder.String()
|
rendered := builder.String()
|
||||||
|
|
||||||
|
// clamp to height around selected node
|
||||||
|
if t.selectedNode != nil {
|
||||||
|
lines := strings.Split(rendered, "\n")
|
||||||
|
totalLines := len(lines)
|
||||||
|
selectedLine := t.selectedNode.line
|
||||||
|
|
||||||
|
halfHeight := t.height / 2
|
||||||
|
startLine := selectedLine - halfHeight
|
||||||
|
if startLine < 0 {
|
||||||
|
startLine = 0
|
||||||
|
}
|
||||||
|
endLine := startLine + t.height
|
||||||
|
if endLine > totalLines {
|
||||||
|
endLine = totalLines
|
||||||
|
startLine = endLine - t.height
|
||||||
|
if startLine < 0 {
|
||||||
|
startLine = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clampedLines := lines[startLine:endLine]
|
||||||
|
rendered = strings.Join(clampedLines, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return rendered
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==================== FsTree helper methods ====================
|
// ==================== FsTree helper methods ====================
|
||||||
|
|||||||
@@ -98,12 +98,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case tea.WindowSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
m.terminalWidth = msg.Width
|
m.terminalWidth = msg.Width
|
||||||
m.terminalHeight = msg.Height
|
m.terminalHeight = msg.Height
|
||||||
m.viewport.Width = msg.Width - m.width - 1
|
if m.tree != nil {
|
||||||
m.viewport.Height = msg.Height
|
m.tree.Update(tea.WindowSizeMsg{
|
||||||
|
Height: m.terminalHeight,
|
||||||
|
Width: m.terminalWidth,
|
||||||
|
})
|
||||||
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
case treeLoadedMsg:
|
case treeLoadedMsg:
|
||||||
m.tree = msg.tree
|
m.tree = msg.tree
|
||||||
m.loading = false
|
m.loading = false
|
||||||
|
m.tree.Update(tea.WindowSizeMsg{
|
||||||
|
Height: m.terminalHeight,
|
||||||
|
Width: m.terminalWidth,
|
||||||
|
})
|
||||||
return m, nil
|
return m, nil
|
||||||
case spinner.TickMsg:
|
case spinner.TickMsg:
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
|
|||||||
Reference in New Issue
Block a user