Skip to content

Prompt feature leftover items #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ed9ef6e
fix: Correctly resolve path in open and cd prompt actions
lazysegtree May 9, 2025
ac7914a
Merge pull request #802 from yorukot/prompt_improvements
lazysegtree May 10, 2025
c74daf4
feat: Add unit test for model action, put firstUse inside model struc…
lazysegtree May 10, 2025
49fa9c9
feat: Add prompt tests, Add additional utility functions for test
lazysegtree May 11, 2025
3e857c2
Merge pull request #803 from yorukot/model_unit_tests
lazysegtree May 11, 2025
4d185dd
feat: Dynamic dimensionf for prompt model, prompt and width adjustmen…
lazysegtree May 11, 2025
29c2b3e
fix: Unit test fix for windows
lazysegtree May 12, 2025
e4676c7
Merge pull request #805 from yorukot/prompt_improvements
lazysegtree May 12, 2025
ec16805
fix: PR comments, typos, test case fix, prompt min dimensions, etc
lazysegtree May 13, 2025
fb7c8f9
feat: Render unit tests for prompt model
lazysegtree May 14, 2025
da01d2e
Merge pull request #809 from yorukot/prompt_unit_tests
lazysegtree May 14, 2025
639fa69
fix: PR comments, better error handling in loading config
lazysegtree May 14, 2025
ba8aa8b
feat: Add --chooser-file and path-list --lastdir-file option
lazysegtree May 15, 2025
a2daebb
feat: Unit tests for modal quit and chooser file
lazysegtree May 15, 2025
8e296fe
feat: Add testsuite test for chooser file
lazysegtree May 15, 2025
5c7a12d
fix: PR fix - typos, error handling, other improvements
lazysegtree May 15, 2025
1b565ba
Merge pull request #812 from yorukot/chooser_file_options
lazysegtree May 15, 2025
8c98741
fix: PR fix - typos, error handling, etc
lazysegtree May 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package internal
import (
"log/slog"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"

"github.com/yorukot/superfile/src/internal/ui/rendering"
"github.com/yorukot/superfile/src/internal/ui/sidebar"
Expand Down Expand Up @@ -56,26 +54,22 @@ func initialConfig(firstFilePanelDirs []string) (toggleDotFile bool, toggleFoote
}
}

cwd, _ := os.Getwd()
for i := range firstFilePanelDirs {
if firstFilePanelDirs[i] == "" {
firstFilePanelDirs[i] = common.Config.DefaultDirectory
}

if strings.HasPrefix(firstFilePanelDirs[i], "~") {
// We only need to replace the first ~ , not all of them
// And only if its a prefix
firstFilePanelDirs[i] = strings.Replace(firstFilePanelDirs[i], "~", variable.HomeDir, 1)
}
firstFilePanelDirs[i], err = filepath.Abs(firstFilePanelDirs[i])
firstFilePanelDirs[i] = utils.ResolveAbsPath(cwd, firstFilePanelDirs[i])
// In case of unexpected path error, fallback to home dir
if err != nil {
slog.Error("Unexpected error while calculating firstFilePanelDir", "error", err)
if _, err := os.Stat(firstFilePanelDirs[i]); err != nil {
slog.Error("cannot get stats for firstFilePanelDir", "error", err)
firstFilePanelDirs[i] = variable.HomeDir
}
}

slog.Debug("Runtime information", "runtime.GOOS", runtime.GOOS)
slog.Debug("Directory configuration", "start_directories", firstFilePanelDirs)
slog.Debug("Directory configuration", "cwd", cwd, "start_directories", firstFilePanelDirs)

var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
Expand Down
1 change: 1 addition & 0 deletions src/internal/handle_panel_navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (m *model) pinnedDirectory() {
// Create new file panel
func (m *model) createNewFilePanel(location string) error {
if len(m.fileModel.filePanels) == m.fileModel.maxFilePanel {
// Todo : Define as a predefined error in errors.go
return errors.New("maximum panel count reached")
}

Expand Down
26 changes: 14 additions & 12 deletions src/internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (m *model) applyPromptModalAction(action common.ModelAction) {
actionErr = m.updateCurrentFilePanelDir(action.Location)
successMsg = "Panel directory changed"
case common.OpenPanelAction:
actionErr = m.createNewFilePanel(action.Location)
actionErr = m.createNewFilePanelRelativeToCurrent(action.Location)
successMsg = "New panel opened"
default:
actionErr = errors.New("unhandled action type")
Expand Down Expand Up @@ -345,21 +345,23 @@ func (m *model) splitPanel() error {
return m.createNewFilePanel(m.fileModel.filePanels[m.filePanelFocusIndex].location)
}

func (m *model) updateCurrentFilePanelDir(dir string) error {
currentPath := m.fileModel.filePanels[m.filePanelFocusIndex].location
newPath := dir
if !filepath.IsAbs(dir) {
// Assume relative path from current
newPath = filepath.Join(currentPath, dir)
}
func (m *model) createNewFilePanelRelativeToCurrent(path string) error {
currentDir := m.fileModel.filePanels[m.filePanelFocusIndex].location
return m.createNewFilePanel(utils.ResolveAbsPath(currentDir, path))
}

// simulates a 'cd' action
func (m *model) updateCurrentFilePanelDir(path string) error {
currentDir := m.fileModel.filePanels[m.filePanelFocusIndex].location
path = utils.ResolveAbsPath(currentDir, path)

if info, err := os.Stat(newPath); err != nil {
return fmt.Errorf("%s : no such file or directory, stats err : %w", newPath, err)
if info, err := os.Stat(path); err != nil {
return fmt.Errorf("%s : no such file or directory, stats err : %w", path, err)
} else if !info.IsDir() {
return fmt.Errorf("%s is not a directory", newPath)
return fmt.Errorf("%s is not a directory", path)
}

m.fileModel.filePanels[m.filePanelFocusIndex].location = newPath
m.fileModel.filePanels[m.filePanelFocusIndex].location = path
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions src/internal/utils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package utils
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strings"

"github.com/adrg/xdg"
"github.com/pelletier/go-toml/v2"
)

Expand Down Expand Up @@ -95,3 +98,18 @@ func LoadTomlFile(filePath string, defaultData string, target interface{}, fixFl
}
return false
}

// If path is not absolute, then append to currentDir and get absolute path
// Resolve paths starting with "~"
// currentDir should be an absolute path
func ResolveAbsPath(currentDir string, path string) string {
if strings.HasPrefix(path, "~") {
// We dont use variable.HomeDir here, as the util package cannot have dependency
// on variable package
path = strings.Replace(path, "~", xdg.Home, 1)
}
if !filepath.IsAbs(path) {
path = filepath.Join(currentDir, path)
}
return filepath.Clean(path)
}
60 changes: 60 additions & 0 deletions src/internal/utils/file_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package utils

import (
"testing"

"github.com/adrg/xdg"
"github.com/stretchr/testify/assert"
)

func TestResolveAbsPath(t *testing.T) {
testdata := []struct {
name string
cwd string
path string
expectedRes string
}{
{
name: "Path cleaup Test 1",
cwd: "/",
path: "////",
expectedRes: "/",
},
{
name: "Basic test",
cwd: "/abc",
path: "def",
expectedRes: "/abc/def",
},
{
name: "Ignore cwd for abs path",
cwd: "/abc",
path: "/def",
expectedRes: "/def",
},
{
name: "Path cleanup Test 2",
cwd: "///abc",
path: "./././def",
expectedRes: "/abc/def",
},
{
name: "Basic test with ~",
cwd: "/",
path: "~",
expectedRes: xdg.Home,
},
{
name: "~ should not be resolved if not first",
cwd: "abc",
path: "x/~",
expectedRes: "abc/x/~",
},
}

for _, tt := range testdata {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expectedRes, ResolveAbsPath(tt.cwd, tt.path))
})
}
}
Loading