Fix directory navigation and path initialization
- Initialize browser path to the actual startup directory instead of '/' - Improve '..' navigation to be relative to the filesystem root - Fix path duplication issues by avoiding redundant absolute path joining
This commit is contained in:
parent
80c2dfd743
commit
beae5fc026
|
|
@ -4,6 +4,7 @@ import (
|
|||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"gioui.org/app"
|
||||
|
|
@ -40,10 +41,18 @@ func run(w *app.Window) error {
|
|||
rootDir := flag.String("root", startpath, "root directory for the filesystem")
|
||||
flag.Parse()
|
||||
|
||||
fs := real.NewRealFileSystem(*rootDir)
|
||||
log.Printf("using filesystem at %s", fs.WorkingDir)
|
||||
// Initialize the real filesystem rooted at the system root "/"
|
||||
// so that the browser can navigate the entire system.
|
||||
fs := real.NewRealFileSystem("/")
|
||||
|
||||
logic := editor.NewLogic(fs)
|
||||
// Get the absolute path of the startup directory
|
||||
startAbs, err := filepath.Abs(*rootDir)
|
||||
if err != nil {
|
||||
startAbs = *rootDir
|
||||
}
|
||||
log.Printf("using filesystem at / (startup directory: %s)", startAbs)
|
||||
|
||||
logic := editor.NewLogic(fs, startAbs)
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
|
||||
var mu sync.Mutex
|
||||
var elems []ui.Element
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@ package browser
|
|||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
|
|
@ -81,11 +79,24 @@ func HandleBrowserTap(bm *BrowserManager, s *BrowserState, index int) {
|
|||
// Navigate into directory or up
|
||||
var newPath string
|
||||
if entry.Name == ".." {
|
||||
newPath = filepath.Dir(s.CurrentPath)
|
||||
// If we are at the root (".") stay there
|
||||
if s.CurrentPath == "." || s.CurrentPath == "" {
|
||||
newPath = "."
|
||||
} else {
|
||||
// Get parent directory
|
||||
parent := filepath.Dir(s.CurrentPath)
|
||||
if parent == "." || parent == "/" {
|
||||
newPath = "."
|
||||
} else {
|
||||
newPath = parent
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Join child entry name
|
||||
if s.CurrentPath == "." {
|
||||
newPath = entry.Name
|
||||
} else {
|
||||
newPath = filepath.Join(s.CurrentPath, entry.Name)
|
||||
if !strings.HasPrefix(newPath, "/") {
|
||||
newPath = "/" + newPath
|
||||
}
|
||||
}
|
||||
bm.NavigateTo(newPath)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,8 @@ func NewBrowserManager(state *BrowserState, wp *pool.WorkerPool, fs pool.FileSys
|
|||
}
|
||||
|
||||
func (bm *BrowserManager) NavigateTo(dirPath string) {
|
||||
fmt.Printf("NavigateTo: %s\n", dirPath)
|
||||
fmt.Printf("NavigateTo: dirPath=%s, state.CurrentPath=%s\n", dirPath, bm.state.CurrentPath)
|
||||
|
||||
bm.state.CurrentPath = dirPath
|
||||
navigateToDirectory(bm.state, dirPath)
|
||||
bm.state.Loading = true
|
||||
|
|
@ -86,7 +87,6 @@ func (bm *BrowserManager) HandleResult(result pool.Result) {
|
|||
}
|
||||
|
||||
func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) {
|
||||
fmt.Printf("handleBuildIndexSuccess: TotalEntries=%d\n", len(result.Data.([]types.DirEntry)))
|
||||
bm.state.Loading = false
|
||||
|
||||
// In the mock implementation, BuildIndexTask returns []types.DirEntry.
|
||||
|
|
@ -95,7 +95,9 @@ func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) {
|
|||
|
||||
var browserEntries []Entry
|
||||
// Add ".." entry if not at root
|
||||
if bm.state.CurrentPath != "/" && bm.state.CurrentPath != "." {
|
||||
// We check against "/" specifically. "." is just a relative path for current dir,
|
||||
// which still has a parent.
|
||||
if bm.state.CurrentPath != "/" && bm.state.CurrentPath != "" {
|
||||
browserEntries = append(browserEntries, Entry{
|
||||
Path: filepath.Dir(bm.state.CurrentPath),
|
||||
Name: "..",
|
||||
|
|
@ -107,6 +109,7 @@ func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) {
|
|||
|
||||
for _, e := range entries {
|
||||
info, _ := e.Info()
|
||||
fmt.Printf("Adding entry: %s\n", e.Name())
|
||||
browserEntries = append(browserEntries, Entry{
|
||||
Path: filepath.Join(bm.state.CurrentPath, e.Name()),
|
||||
Name: e.Name(),
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ type Logic struct {
|
|||
}
|
||||
|
||||
// NewLogic creates a new Logic instance, accepting an optional mockFS.
|
||||
func NewLogic(mfs pool.FileSystem) *Logic {
|
||||
func NewLogic(mfs pool.FileSystem, startPath ...string) *Logic {
|
||||
state := NewState()
|
||||
TheState = state
|
||||
|
||||
|
|
@ -81,8 +81,12 @@ func NewLogic(mfs pool.FileSystem) *Logic {
|
|||
wp := pool.NewWorkerPool(4)
|
||||
wp.Start()
|
||||
|
||||
// Set browser initial path to mock root
|
||||
state.Browser.CurrentPath = "/"
|
||||
// Set browser initial path
|
||||
path := "/"
|
||||
if len(startPath) > 0 {
|
||||
path = startPath[0]
|
||||
}
|
||||
state.Browser.CurrentPath = path
|
||||
bm, _ := browser.NewBrowserManager(&state.Browser, wp, mockFS)
|
||||
|
||||
TheLogic = &Logic{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user