diff options
| author | Eyal Posener <[email protected]> | 2017-05-13 00:45:08 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-13 00:45:08 +0300 |
| commit | 9123548bc5f7725948b6096be4ef490af71194b3 (patch) | |
| tree | 33e21d60ec1412ff7aaf1c73363208e79d96d8b3 /args.go | |
| parent | 136e52e074be4a85fb9e9bccf93d060c46e67561 (diff) | |
| parent | bc5d682221f38e82bbd977d5567da8b884a30cbc (diff) | |
Merge pull request #16 from posener/recursive-dirs
Recursive directories and complete by go packages
Diffstat (limited to 'args.go')
| -rw-r--r-- | args.go | 26 |
1 files changed, 23 insertions, 3 deletions
@@ -1,23 +1,43 @@ package complete +import ( + "os" + "path/filepath" +) + // Args describes command line arguments type Args struct { // All lists of all arguments in command line (not including the command itself) - All []string + All []string // Completed lists of all completed arguments in command line, // If the last one is still being typed - no space after it, // it won't appear in this list of arguments. - Completed []string + Completed []string // Last argument in command line, the one being typed, if the last // character in the command line is a space, this argument will be empty, // otherwise this would be the last word. - Last string + Last string // LastCompleted is the last argument that was fully typed. // If the last character in the command line is space, this would be the // last word, otherwise, it would be the word before that. LastCompleted string } +// Directory gives the directory of the current written +// last argument if it represents a file name being written. +// in case that it is not, we fall back to the current directory. +func (a Args) Directory() string { + if info, err := os.Stat(a.Last); err == nil && info.IsDir() { + return a.Last + } + dir := filepath.Dir(a.Last) + _, err := os.Stat(dir) + if err != nil { + return "./" + } + return dir +} + func newArgs(line []string) Args { completed := removeLast(line) return Args{ |
