Fix: parent dir

This commit is contained in:
Grail Finder
2025-11-20 12:03:15 +03:00
parent 8351a9e084
commit b4f9c5e26a

106
tables.go
View File

@@ -540,22 +540,40 @@ func makeImportChatTable(filenames []string) *tview.Table {
func makeFilePicker() *tview.Flex { func makeFilePicker() *tview.Flex {
// Initialize with directory from config or current directory // Initialize with directory from config or current directory
currentDir := cfg.FilePickerDir startDir := cfg.FilePickerDir
if currentDir == "" { if startDir == "" {
currentDir = "." startDir = "."
}
// If startDir is ".", resolve it to the actual current working directory
if startDir == "." {
wd, err := os.Getwd()
if err == nil {
startDir = wd
}
} }
// Track navigation history // Track navigation history
dirStack := []string{currentDir} dirStack := []string{startDir}
currentStackPos := 0 currentStackPos := 0
// Track selected file // Track selected file
var selectedFile string var selectedFile string
// Track currently displayed directory (changes as user navigates)
var currentDisplayDir string = startDir
// Create UI elements // Create UI elements
listView := tview.NewList() listView := tview.NewList()
listView.SetBorder(true).SetTitle("Files & Directories").SetTitleAlign(tview.AlignLeft) listView.SetBorder(true).SetTitle("Files & Directories").SetTitleAlign(tview.AlignLeft)
// Path input field
pathInput := tview.NewInputField().
SetLabel("Path: ").
SetText(startDir).
SetFieldWidth(50)
pathInput.SetBorder(true).SetTitle("Enter Path").SetTitleAlign(tview.AlignLeft)
statusView := tview.NewTextView() statusView := tview.NewTextView()
statusView.SetBorder(true).SetTitle("Selected File").SetTitleAlign(tview.AlignLeft) statusView.SetBorder(true).SetTitle("Selected File").SetTitleAlign(tview.AlignLeft)
statusView.SetTextColor(tcell.ColorYellow) statusView.SetTextColor(tcell.ColorYellow)
@@ -578,14 +596,20 @@ func makeFilePicker() *tview.Flex {
pages.RemovePage(filePickerPage) pages.RemovePage(filePickerPage)
}) })
// Path input button - will be updated after refreshList is defined
goButton := tview.NewButton("Go")
buttonBar.AddItem(tview.NewBox().SetBackgroundColor(tcell.ColorDefault), 0, 1, false) buttonBar.AddItem(tview.NewBox().SetBackgroundColor(tcell.ColorDefault), 0, 1, false)
buttonBar.AddItem(goButton, 5, 1, true)
buttonBar.AddItem(tview.NewBox(), 1, 1, false)
buttonBar.AddItem(loadButton, 8, 1, true) buttonBar.AddItem(loadButton, 8, 1, true)
buttonBar.AddItem(tview.NewBox(), 1, 1, false) buttonBar.AddItem(tview.NewBox(), 1, 1, false)
buttonBar.AddItem(cancelButton, 8, 1, true) buttonBar.AddItem(cancelButton, 8, 1, true)
buttonBar.AddItem(tview.NewBox().SetBackgroundColor(tcell.ColorDefault), 0, 1, false) buttonBar.AddItem(tview.NewBox().SetBackgroundColor(tcell.ColorDefault), 0, 1, false)
// Layout // Layout - add path input field at the top
flex := tview.NewFlex().SetDirection(tview.FlexRow) flex := tview.NewFlex().SetDirection(tview.FlexRow)
flex.AddItem(pathInput, 3, 0, false)
flex.AddItem(listView, 0, 3, true) flex.AddItem(listView, 0, 3, true)
flex.AddItem(statusView, 3, 0, false) flex.AddItem(statusView, 3, 0, false)
flex.AddItem(buttonBar, 3, 0, false) flex.AddItem(buttonBar, 3, 0, false)
@@ -595,6 +619,10 @@ func makeFilePicker() *tview.Flex {
refreshList = func(dir string) { refreshList = func(dir string) {
listView.Clear() listView.Clear()
// Update the path input field and current display directory
pathInput.SetText(dir)
currentDisplayDir = dir // Update the current display directory
// Add parent directory (..) if not at root // Add parent directory (..) if not at root
if dir != "/" { if dir != "/" {
parentDir := path.Dir(dir) parentDir := path.Dir(dir)
@@ -622,16 +650,20 @@ func makeFilePicker() *tview.Flex {
for _, file := range files { for _, file := range files {
name := file.Name() name := file.Name()
if file.IsDir() { if file.IsDir() {
listView.AddItem(name+"/", "(Directory)", 0, func() { // Capture the directory name for the closure to avoid loop variable issues
newDir := path.Join(dir, name) dirName := name
listView.AddItem(dirName+"/", "(Directory)", 0, func() {
newDir := path.Join(dir, dirName)
refreshList(newDir) refreshList(newDir)
dirStack = append(dirStack, newDir) dirStack = append(dirStack, newDir)
currentStackPos = len(dirStack) - 1 currentStackPos = len(dirStack) - 1
statusView.SetText("Current: " + newDir) statusView.SetText("Current: " + newDir)
}) })
} else { } else {
listView.AddItem(name, "(File)", 0, func() { // Capture the file name for the closure to avoid loop variable issues
selectedFile = path.Join(dir, name) fileName := name
listView.AddItem(fileName, "(File)", 0, func() {
selectedFile = path.Join(dir, fileName)
statusView.SetText("Selected: " + selectedFile) statusView.SetText("Selected: " + selectedFile)
}) })
} }
@@ -641,7 +673,42 @@ func makeFilePicker() *tview.Flex {
} }
// Initialize the file list // Initialize the file list
refreshList(currentDir) refreshList(startDir)
// Set up keyboard navigation for the path input
pathInput.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
// Trigger the Go functionality when Enter is pressed in the path input
newPath := pathInput.GetText()
if newPath != "" {
// Check if path exists and is a directory
if info, err := os.Stat(newPath); err == nil && info.IsDir() {
refreshList(newPath)
dirStack = append(dirStack, newPath)
currentStackPos = len(dirStack) - 1
statusView.SetText("Current: " + newPath)
} else {
statusView.SetText("Invalid directory: " + newPath)
}
}
}
})
// Now that refreshList is defined, set the Go button's functionality
goButton.SetSelectedFunc(func() {
newPath := pathInput.GetText()
if newPath != "" {
// Check if path exists and is a directory
if info, err := os.Stat(newPath); err == nil && info.IsDir() {
refreshList(newPath)
dirStack = append(dirStack, newPath)
currentStackPos = len(dirStack) - 1
statusView.SetText("Current: " + newPath)
} else {
statusView.SetText("Invalid directory: " + newPath)
}
}
})
// Set up keyboard navigation // Set up keyboard navigation
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
@@ -667,6 +734,7 @@ func makeFilePicker() *tview.Flex {
// Let's improve the approach by tracking the currently selected item // Let's improve the approach by tracking the currently selected item
itemText, _ := listView.GetItemText(itemIndex) itemText, _ := listView.GetItemText(itemIndex)
logger.Info("choosing dir", "itemText", itemText)
// Check if it's a directory (typically ends with /) // Check if it's a directory (typically ends with /)
if strings.HasSuffix(itemText, "/") { if strings.HasSuffix(itemText, "/") {
// This is a directory, we need to get the full path // This is a directory, we need to get the full path
@@ -674,28 +742,34 @@ func makeFilePicker() *tview.Flex {
var targetDir string var targetDir string
if strings.HasPrefix(itemText, "../") { if strings.HasPrefix(itemText, "../") {
// Parent directory - need to go up from current directory // Parent directory - need to go up from current directory
targetDir = path.Dir(currentDir) targetDir = path.Dir(currentDisplayDir)
// Avoid going above root - if parent is same as current and it's system root // Avoid going above root - if parent is same as current and it's system root
if targetDir == currentDir && currentDir == "/" { if targetDir == currentDisplayDir && currentDisplayDir == "/" {
// We're at root, don't navigate // We're at root, don't navigate
logger.Warn("went to root", "dir", targetDir)
return nil return nil
} }
} else { } else {
// Regular subdirectory // Regular subdirectory
dirName := strings.TrimSuffix(itemText, "/") dirName := strings.TrimSuffix(itemText, "/")
targetDir = path.Join(currentDir, dirName) targetDir = path.Join(currentDisplayDir, dirName)
} }
// Navigate to the selected directory // Navigate to the selected directory
logger.Info("going to the dir", "dir", targetDir)
refreshList(targetDir) refreshList(targetDir)
dirStack = append(dirStack, targetDir) dirStack = append(dirStack, targetDir)
currentStackPos = len(dirStack) - 1 currentStackPos = len(dirStack) - 1
statusView.SetText("Current: " + targetDir) statusView.SetText("Current: " + targetDir)
return nil return nil
} else { } else {
// It's a file, load it if one was selected // It's a file - construct the full path from current directory and the item name
if selectedFile != "" { // We can't rely only on the selectedFile variable since Enter key might be pressed
textArea.SetText(selectedFile, true) // without having clicked the file first
filePath := path.Join(currentDisplayDir, itemText)
// Verify it's actually a file (not just lacking a directory suffix)
if info, err := os.Stat(filePath); err == nil && !info.IsDir() {
textArea.SetText(filePath, true)
app.SetFocus(textArea) app.SetFocus(textArea)
pages.RemovePage(filePickerPage) pages.RemovePage(filePickerPage)
} }