Skip to content

Commit

Permalink
Merge pull request #785 from rsteube/absolute-path
Browse files Browse the repository at this point in the history
actionPaths: add windows volume support
  • Loading branch information
rsteube authored Jun 20, 2023
2 parents 89ff5fe + 173b04b commit 668c426
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
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

0 comments on commit 668c426

Please sign in to comment.