Private
Public Access
1
0

filepicker: Track expanded paths

This commit is contained in:
2024-12-27 06:18:49 +00:00
parent 6b629ed1d6
commit 3cac7e8e11

View File

@@ -26,6 +26,7 @@ type Model struct {
rootPath string rootPath string
SelectedPaths []string SelectedPaths []string
expandedPaths map[string]bool // Track expanded paths
} }
func New(path string) Model { func New(path string) Model {
@@ -41,6 +42,7 @@ func New(path string) Model {
rootPath: path, rootPath: path,
SelectedPaths: make([]string, 0), SelectedPaths: make([]string, 0),
showHidden: false, showHidden: false,
expandedPaths: make(map[string]bool),
} }
m.loadDirectory(root) m.loadDirectory(root)
@@ -49,6 +51,13 @@ func New(path string) Model {
} }
func (m *Model) loadDirectory(node *Node) error { func (m *Model) loadDirectory(node *Node) error {
// Save current expanded state of children
for _, child := range node.children {
if child.expanded {
m.expandedPaths[child.path] = true
}
}
entries, err := os.ReadDir(node.path) entries, err := os.ReadDir(node.path)
if err != nil { if err != nil {
return err return err
@@ -60,11 +69,12 @@ func (m *Model) loadDirectory(node *Node) error {
continue continue
} }
childPath := filepath.Join(node.path, entry.Name())
child := &Node{ child := &Node{
path: filepath.Join(node.path, entry.Name()), path: childPath,
name: entry.Name(), name: entry.Name(),
isDir: entry.IsDir(), isDir: entry.IsDir(),
expanded: false, expanded: m.expandedPaths[childPath], // Restore expanded state
} }
node.children = append(node.children, child) node.children = append(node.children, child)
} }
@@ -101,6 +111,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
current := m.displayLines[m.cursor] current := m.displayLines[m.cursor]
if current.isDir { if current.isDir {
current.expanded = true current.expanded = true
m.expandedPaths[current.path] = true
m.loadDirectory(current) m.loadDirectory(current)
m.updateDisplayLines() m.updateDisplayLines()
} }
@@ -108,12 +119,14 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
current := m.displayLines[m.cursor] current := m.displayLines[m.cursor]
if current.isDir && current.expanded { if current.isDir && current.expanded {
current.expanded = false current.expanded = false
delete(m.expandedPaths, current.path)
m.updateDisplayLines() m.updateDisplayLines()
} else { } else {
// Find and collapse parent // Find and collapse parent
parent := m.findParent(current) parent := m.findParent(current)
if parent != nil && parent.path != m.rootPath { if parent != nil && parent.path != m.rootPath {
parent.expanded = false parent.expanded = false
delete(m.expandedPaths, parent.path)
m.updateDisplayLines() m.updateDisplayLines()
// Move cursor to parent // Move cursor to parent
for i, node := range m.displayLines { for i, node := range m.displayLines {
@@ -229,11 +242,23 @@ func (m Model) View() string {
} }
func (m *Model) reloadTree() { func (m *Model) reloadTree() {
// Save expanded state before reloading
m.saveExpandedState(m.root)
m.root.children = nil m.root.children = nil
m.loadDirectory(m.root) m.loadDirectory(m.root)
m.updateDisplayLines() m.updateDisplayLines()
} }
func (m *Model) saveExpandedState(node *Node) {
if node.isDir && node.expanded {
m.expandedPaths[node.path] = true
for _, child := range node.children {
m.saveExpandedState(child)
}
}
}
func (m *Model) findParent(node *Node) *Node { func (m *Model) findParent(node *Node) *Node {
var findParentRec func(*Node) *Node var findParentRec func(*Node) *Node