diff options
| author | Eyal Posener <[email protected]> | 2017-05-15 23:52:04 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-18 23:29:55 +0300 |
| commit | 61d9904ba1f47bf5bbd3497ac0c9f5787adb8633 (patch) | |
| tree | 17357f14eadb4654287eb664dfbe02914167f4db /utils.go | |
| parent | 659bd9e3d5a0113fb862bcbd06a983b0e74e8df7 (diff) | |
Fix './' prefix for file completion
Diffstat (limited to 'utils.go')
| -rw-r--r-- | utils.go | 27 |
1 files changed, 21 insertions, 6 deletions
@@ -3,10 +3,11 @@ package complete import ( "os" "path/filepath" + "strings" ) -// relativePath changes a file name to a relative name -func relativePath(file string) string { +// fixPathForm changes a file name to a relative name +func fixPathForm(last string, file string) string { // get wording directory for relative name workDir, err := os.Getwd() if err != nil { @@ -17,15 +18,29 @@ func relativePath(file string) string { if err != nil { return file } + + // if last is absolute, return path as absolute + if filepath.IsAbs(last) { + return fixDirPath(abs) + } + rel, err := filepath.Rel(workDir, abs) if err != nil { return file } - if rel != "." { + + // fix ./ prefix of path + if rel != "." && strings.HasPrefix(last, ".") { rel = "./" + rel } - if info, err := os.Stat(rel); err == nil && info.IsDir() { - rel += "/" + + return fixDirPath(rel) +} + +func fixDirPath(path string) string { + info, err := os.Stat(path) + if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") { + path += "/" } - return rel + return path } |
