]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Add show command to show the value of an option
[micro.git] / cmd / micro / autocomplete.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "strings"
7
8         "github.com/mitchellh/go-homedir"
9 )
10
11 // This file is meant (for now) for autocompletion in command mode, not
12 // while coding. This helps micro autocomplete commands and then filenames
13 // for example with `vsplit filename`.
14
15 // FileComplete autocompletes filenames
16 func FileComplete(input string) (string, []string) {
17         dirs := strings.Split(input, "/")
18         var files []os.FileInfo
19         var err error
20         if len(dirs) > 1 {
21                 home, _ := homedir.Dir()
22
23                 directories := strings.Join(dirs[:len(dirs)-1], "/")
24                 if strings.HasPrefix(directories, "~") {
25                         directories = strings.Replace(directories, "~", home, 1)
26                 }
27                 files, err = ioutil.ReadDir(directories)
28         } else {
29                 files, err = ioutil.ReadDir(".")
30         }
31         var suggestions []string
32         if err != nil {
33                 return "", suggestions
34         }
35         for _, f := range files {
36                 name := f.Name()
37                 if f.IsDir() {
38                         name += "/"
39                 }
40                 if strings.HasPrefix(name, dirs[len(dirs)-1]) {
41                         suggestions = append(suggestions, name)
42                 }
43         }
44
45         var chosen string
46         if len(suggestions) == 1 {
47                 if len(dirs) > 1 {
48                         chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/" + suggestions[0]
49                 } else {
50                         chosen = suggestions[0]
51                 }
52         } else {
53                 if len(dirs) > 1 {
54                         chosen = strings.Join(dirs[:len(dirs)-1], "/") + "/"
55                 }
56         }
57
58         return chosen, suggestions
59 }
60
61 // CommandComplete autocompletes commands
62 func CommandComplete(input string) (string, []string) {
63         var suggestions []string
64         for cmd := range commands {
65                 if strings.HasPrefix(cmd, input) {
66                         suggestions = append(suggestions, cmd)
67                 }
68         }
69
70         var chosen string
71         if len(suggestions) == 1 {
72                 chosen = suggestions[0]
73         }
74         return chosen, suggestions
75 }
76
77 // HelpComplete autocompletes help topics
78 func HelpComplete(input string) (string, []string) {
79         var suggestions []string
80
81         for _, topic := range helpFiles {
82                 if strings.HasPrefix(topic, input) {
83
84                         suggestions = append(suggestions, topic)
85                 }
86         }
87
88         var chosen string
89         if len(suggestions) == 1 {
90                 chosen = suggestions[0]
91         }
92         return chosen, suggestions
93 }
94
95 func contains(s []string, e string) bool {
96         for _, a := range s {
97                 if a == e {
98                         return true
99                 }
100         }
101         return false
102 }
103
104 // OptionComplete autocompletes options
105 func OptionComplete(input string) (string, []string) {
106         var suggestions []string
107         localSettings := DefaultLocalSettings()
108         for option := range globalSettings {
109                 if strings.HasPrefix(option, input) {
110                         suggestions = append(suggestions, option)
111                 }
112         }
113         for option := range localSettings {
114                 if strings.HasPrefix(option, input) && !contains(suggestions, option) {
115                         suggestions = append(suggestions, option)
116                 }
117         }
118
119         var chosen string
120         if len(suggestions) == 1 {
121                 chosen = suggestions[0]
122         }
123         return chosen, suggestions
124 }