]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Fix some issues with unicode handling
[micro.git] / cmd / micro / autocomplete.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7 )
8
9 // This file is meant (for now) for autocompletion in command mode, not
10 // while coding. This helps micro autocomplete commands and then filenames
11 // for example with `vsplit filename`.
12
13 // FileComplete autocompletes filenames
14 func FileComplete(input string) (string, []string) {
15         dirs := strings.Split(input, "/")
16         var files []os.FileInfo
17         var err error
18         if len(dirs) > 1 {
19                 files, err = ioutil.ReadDir(strings.Join(dirs[:len(dirs)-1], "/"))
20         } else {
21                 files, err = ioutil.ReadDir(".")
22         }
23         var suggestions []string
24         if err != nil {
25                 return "", suggestions
26         }
27         for _, f := range files {
28                 name := f.Name()
29                 if f.IsDir() {
30                         name += "/"
31                 }
32                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
33                         suggestions = append(suggestions, name)
34                 }
35         }
36
37         var chosen string
38         if len(suggestions) == 1 {
39                 if len(dirs) > 1 {
40                         chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
41                 } else {
42                         chosen = suggestions[0]
43                 }
44         }
45
46         return chosen, suggestions
47 }
48
49 // CommandComplete autocompletes commands
50 func CommandComplete(input string) (string, []string) {
51         var suggestions []string
52         for cmd := range commands {
53                 if strings.HasPrefix(cmd, input) {
54                         suggestions = append(suggestions, cmd)
55                 }
56         }
57
58         var chosen string
59         if len(suggestions) == 1 {
60                 chosen = suggestions[0]
61         }
62         return chosen, suggestions
63 }
64
65 // HelpComplete autocompletes help topics
66 func HelpComplete(input string) (string, []string) {
67         var suggestions []string
68
69         for _, topic := range helpFiles {
70                 if strings.HasPrefix(topic, input) {
71
72                         suggestions = append(suggestions, topic)
73                 }
74         }
75
76         var chosen string
77         if len(suggestions) == 1 {
78                 chosen = suggestions[0]
79         }
80         return chosen, suggestions
81 }
82
83 // OptionComplete autocompletes options
84 func OptionComplete(input string) (string, []string) {
85         var suggestions []string
86         for option := range settings {
87                 if strings.HasPrefix(option, input) {
88                         suggestions = append(suggestions, option)
89                 }
90         }
91
92         var chosen string
93         if len(suggestions) == 1 {
94                 chosen = suggestions[0]
95         }
96         return chosen, suggestions
97 }