]> git.lizzy.rs Git - micro.git/blob - cmd/micro/autocomplete.go
Improve tab completion in command mode
[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 // OptionComplete autocompletes options
96 func OptionComplete(input string) (string, []string) {
97         var suggestions []string
98         localSettings := DefaultLocalSettings()
99         for option := range globalSettings {
100                 if strings.HasPrefix(option, input) {
101                         suggestions = append(suggestions, option)
102                 }
103         }
104         for option := range localSettings {
105                 if strings.HasPrefix(option, input) {
106                         suggestions = append(suggestions, option)
107                 }
108         }
109
110         var chosen string
111         if len(suggestions) == 1 {
112                 chosen = suggestions[0]
113         }
114         return chosen, suggestions
115 }