From 7be96504b4ef96bda7020958ea603a41ea7aac70 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Mon, 14 Mar 2022 21:22:10 -0400 Subject: [PATCH] feat: case insensitive file complete (closes #104) --- complete.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/complete.go b/complete.go index 301b051..585c276 100644 --- a/complete.go +++ b/complete.go @@ -2,8 +2,10 @@ package main import ( "path/filepath" + "runtime" "strings" "os" + "unicode" ) func fileComplete(query, ctx string, fields []string) []string { @@ -76,7 +78,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) { func matchPath(path, pref string) ([]string, error) { var entries []string - matches, err := filepath.Glob(path + "*") + matches, err := filepath.Glob(desensitize(path) + "*") if err == nil { args := []string{ "\"", "\\\"", @@ -108,3 +110,21 @@ func matchPath(path, pref string) ([]string, error) { return entries, err } + +func desensitize(text string) string { + if runtime.GOOS == "windows" { + return text + } + + p := strings.Builder{} + + for _, r := range text { + if unicode.IsLetter(r) { + p.WriteString("[" + string(unicode.ToLower(r)) + string(unicode.ToUpper(r)) + "]") + } else { + p.WriteString(string(r)) + } + } + + return p.String() +}