CompleteMatch is the [complete.MatchFunc] for the shell.
(data any, text string, posLine, posChar int)
| 17 | |
| 18 | // CompleteMatch is the [complete.MatchFunc] for the shell. |
| 19 | func (sh *Shell) CompleteMatch(data any, text string, posLine, posChar int) (md complete.Matches) { |
| 20 | comps := complete.Completions{} |
| 21 | text = text[:posChar] |
| 22 | md.Seed = complete.SeedPath(text) |
| 23 | fullPath := complete.SeedSpace(text) |
| 24 | fullPath = errors.Log1(homedir.Expand(fullPath)) |
| 25 | parent := strings.TrimSuffix(fullPath, md.Seed) |
| 26 | dir := filepath.Join(sh.Config.Dir, parent) |
| 27 | if filepath.IsAbs(parent) { |
| 28 | dir = parent |
| 29 | } |
| 30 | entries := errors.Log1(os.ReadDir(dir)) |
| 31 | for _, entry := range entries { |
| 32 | icon := icons.File |
| 33 | if entry.IsDir() { |
| 34 | icon = icons.Folder |
| 35 | } |
| 36 | name := strings.ReplaceAll(entry.Name(), " ", `\ `) // escape spaces |
| 37 | comps = append(comps, complete.Completion{ |
| 38 | Text: name, |
| 39 | Icon: icon, |
| 40 | Desc: filepath.Join(sh.Config.Dir, name), |
| 41 | }) |
| 42 | } |
| 43 | if parent == "" { |
| 44 | for cmd := range sh.Builtins { |
| 45 | comps = append(comps, complete.Completion{ |
| 46 | Text: cmd, |
| 47 | Icon: icons.Terminal, |
| 48 | Desc: "Builtin command: " + cmd, |
| 49 | }) |
| 50 | } |
| 51 | for cmd := range sh.Commands { |
| 52 | comps = append(comps, complete.Completion{ |
| 53 | Text: cmd, |
| 54 | Icon: icons.Terminal, |
| 55 | Desc: "Command: " + cmd, |
| 56 | }) |
| 57 | } |
| 58 | // todo: write something that looks up all files on path -- should cache that per |
| 59 | // path string setting |
| 60 | } |
| 61 | md.Matches = complete.MatchSeedCompletion(comps, md.Seed) |
| 62 | return md |
| 63 | } |
| 64 | |
| 65 | // CompleteEdit is the [complete.EditFunc] for the shell. |
| 66 | func (sh *Shell) CompleteEdit(data any, text string, cursorPos int, completion complete.Completion, seed string) (ed complete.Edit) { |