Skip to content
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

actionPaths: add windows volume support #785

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.vscode
example/example
example/cmd/_test_files/*.txt
caraparse/caraparse
docs/book
example/cmd/_test_files/*.txt
example/example
.vscode
24 changes: 23 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"unicode"

"github.com/rsteube/carapace/internal/common"
"github.com/rsteube/carapace/internal/env"
Expand Down Expand Up @@ -114,14 +116,30 @@ func expandHome(s string) (string, error) {
if err != nil {
return "", err
}
home = filepath.ToSlash(home)
s = strings.Replace(s, "~/", home+"/", 1)
}
return s, nil
}

// hasVolumePrefix checks if given path has a volume prefix (only for GOOS=windows)
func hasVolumePrefix(path string) bool {
switch {
case runtime.GOOS != "windows":
return false
case len(path) < 2:
return false
case unicode.IsLetter(rune(path[0])) && path[1] == ':':
return true
default:
return false
}
}

// Abs returns an absolute representation of path.
func (c Context) Abs(path string) (string, error) {
if !strings.HasPrefix(path, "/") && !strings.HasPrefix(path, "~") { // path is relative
path = filepath.ToSlash(path)
if !strings.HasPrefix(path, "/") && !strings.HasPrefix(path, "~") && !hasVolumePrefix(path) { // path is relative
switch c.Dir {
case "":
path = "./" + path
Expand All @@ -135,10 +153,14 @@ func (c Context) Abs(path string) (string, error) {
return "", err
}

if len(path) == 2 && hasVolumePrefix(path) {
path += "/" // prevent `C:` -> `C:./current/working/directory`
}
result, err := filepath.Abs(path)
if err != nil {
return "", err
}
result = filepath.ToSlash(result)

if strings.HasSuffix(path, "/") && !strings.HasSuffix(result, "/") {
result += "/"
Expand Down
9 changes: 7 additions & 2 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ import (

func actionPath(fileSuffixes []string, dirOnly bool) Action {
return ActionCallback(func(c Context) Action {
if len(c.Value) == 2 && hasVolumePrefix(c.Value) {
// TODO should be fixed in Abs or wherever this is happening
return ActionValues(c.Value + "/") // prevent `C:` -> `C:.`
}

abs, err := c.Abs(c.Value)
if err != nil {
return ActionMessage(err.Error())
}

displayFolder := filepath.Dir(c.Value)
displayFolder := filepath.ToSlash(filepath.Dir(c.Value))
if displayFolder == "." {
displayFolder = ""
} else if !strings.HasSuffix(displayFolder, "/") {
displayFolder = displayFolder + "/"
}

actualFolder := filepath.Dir(abs)
actualFolder := filepath.ToSlash(filepath.Dir(abs))
files, err := ioutil.ReadDir(actualFolder)
if err != nil {
return ActionMessage(err.Error())
Expand Down