mirror of https://github.com/Hilbis/Hilbish
fix: completions of executables and running absolute paths on windows
parent
925ded6cea
commit
f73c6d4aa8
|
@ -33,7 +33,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
|||
if len(fileCompletions) != 0 {
|
||||
for _, f := range fileCompletions {
|
||||
name := strings.Replace(query + f, "~", curuser.HomeDir, 1)
|
||||
if info, err := os.Stat(name); err == nil && info.Mode().Perm() & 0100 == 0 {
|
||||
if err := findExecutable(name, false, true); err != nil {
|
||||
continue
|
||||
}
|
||||
completions = append(completions, f)
|
||||
|
@ -51,7 +51,8 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
|||
// get basename from matches
|
||||
for _, match := range matches {
|
||||
// check if we have execute permissions for our match
|
||||
if info, err := os.Stat(match); err == nil && info.Mode().Perm() & 0100 == 0 {
|
||||
err := findExecutable(match, true, false)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// get basename from match
|
||||
|
|
12
exec.go
12
exec.go
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -161,15 +162,20 @@ func execCommand(cmd, old string) error {
|
|||
}
|
||||
|
||||
func lookpath(file string) error { // custom lookpath function so we know if a command is found *and* is executable
|
||||
skip := []string{"./", "/", "../", "~/"}
|
||||
var skip []string
|
||||
if runtime.GOOS == "windows" {
|
||||
skip = []string{"./", "../", "~/", "C:"}
|
||||
} else {
|
||||
skip = []string{"./", "/", "../", "~/"}
|
||||
}
|
||||
for _, s := range skip {
|
||||
if strings.HasPrefix(file, s) {
|
||||
return findExecutable(file)
|
||||
return findExecutable(file, false, false)
|
||||
}
|
||||
}
|
||||
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
||||
path := filepath.Join(dir, file)
|
||||
err := findExecutable(path)
|
||||
err := findExecutable(path, true, false)
|
||||
if err == errNotExec {
|
||||
return err
|
||||
} else if err == nil {
|
||||
|
|
|
@ -6,13 +6,19 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
func findExecutable(path string) error {
|
||||
func findExecutable(path string, inPath, dirs bool) error {
|
||||
f, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
|
||||
return nil
|
||||
if dirs {
|
||||
if m := f.Mode(); m & 0111 != 0 {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
if m := f.Mode(); !m.IsDir() && m & 0111 != 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errNotExec
|
||||
}
|
||||
|
|
|
@ -7,20 +7,30 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
func findExecutable(path string) error {
|
||||
func findExecutable(path string, inPath, dirs bool) error {
|
||||
nameExt := filepath.Ext(path)
|
||||
if nameExt == "" {
|
||||
for _, ext := range filepath.SplitList(os.Getenv("PATHEXT")) {
|
||||
_, err := os.Stat(path + ext)
|
||||
pathExts := filepath.SplitList(os.Getenv("PATHEXT"))
|
||||
if inPath {
|
||||
if nameExt == "" {
|
||||
for _, ext := range pathExts {
|
||||
_, err := os.Stat(path + ext)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return nil
|
||||
if contains(pathExts, nameExt) { return nil }
|
||||
return errNotExec
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return errNotExec
|
||||
} else {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
if contains(pathExts, nameExt) { return nil }
|
||||
return errNotExec
|
||||
}
|
||||
}
|
||||
|
||||
return os.ErrNotExist
|
||||
|
|
Loading…
Reference in New Issue