10 // Args describes command line arguments
12 // All lists of all arguments in command line (not including the command itself)
14 // Completed lists of all completed arguments in command line,
15 // If the last one is still being typed - no space after it,
16 // it won't appear in this list of arguments.
18 // Last argument in command line, the one being typed, if the last
19 // character in the command line is a space, this argument will be empty,
20 // otherwise this would be the last word.
22 // LastCompleted is the last argument that was fully typed.
23 // If the last character in the command line is space, this would be the
24 // last word, otherwise, it would be the word before that.
28 // Directory gives the directory of the current written
29 // last argument if it represents a file name being written.
30 // in case that it is not, we fall back to the current directory.
31 func (a Args) Directory() string {
32 if info, err := os.Stat(a.Last); err == nil && info.IsDir() {
33 return fixPathForm(a.Last, a.Last)
35 dir := filepath.Dir(a.Last)
36 if info, err := os.Stat(dir); err != nil || !info.IsDir() {
39 return fixPathForm(a.Last, dir)
42 func newArgs(line string) Args {
47 parts := splitFields(line)
50 completed = removeLast(parts[1:])
56 LastCompleted: last(completed),
60 // splitFields returns a list of fields from the given command line.
61 // If the last character is space, it appends an empty field in the end
62 // indicating that the field before it was completed.
63 // If the last field is of the form "a=b", it splits it to two fields: "a", "b",
64 // So it can be completed.
65 func splitFields(line string) []string {
66 parts := strings.Fields(line)
68 // Add empty field if the last field was completed.
69 if len(line) > 0 && unicode.IsSpace(rune(line[len(line)-1])) {
70 parts = append(parts, "")
73 // Treat the last field if it is of the form "a=b"
74 parts = splitLastEqual(parts)
78 func splitLastEqual(line []string) []string {
82 parts := strings.Split(line[len(line)-1], "=")
83 return append(line[:len(line)-1], parts...)
86 func (a Args) from(i int) Args {
92 if i > len(a.Completed) {
95 a.Completed = a.Completed[i:]
99 func removeLast(a []string) []string {
106 func last(args []string) string {
110 return args[len(args)-1]