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