Browse Source

h2i: auto-complete commands on TAB

w/ @bradfitz
Blake Mizerany 10 years ago
parent
commit
93a9d4ac82
1 changed files with 16 additions and 5 deletions
  1. 16 5
      h2i/h2i.go

+ 16 - 5
h2i/h2i.go

@@ -135,6 +135,16 @@ func (a *h2i) Main() error {
 	}{os.Stdin, os.Stdout}
 
 	a.term = terminal.NewTerminal(screen, "> ")
+	a.term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
+		if key != '\t' {
+			return
+		}
+		name, _, ok := lookupCommand(line)
+		if !ok {
+			return
+		}
+		return name, len(name), true
+	}
 
 	errc := make(chan error, 2)
 	go func() { errc <- a.readFrames() }()
@@ -157,7 +167,7 @@ func (a *h2i) readConsole() error {
 			continue
 		}
 		cmd, args := f[0], f[1:]
-		if fn, ok := lookupCommand(cmd); ok {
+		if _, fn, ok := lookupCommand(cmd); ok {
 			err = fn(a, args)
 		} else {
 			a.logf("Unknown command %q", line)
@@ -171,21 +181,22 @@ func (a *h2i) readConsole() error {
 	}
 }
 
-func lookupCommand(prefix string) (c command, ok bool) {
+func lookupCommand(prefix string) (name string, c command, ok bool) {
 	prefix = strings.ToLower(prefix)
 	if c, ok = commands[prefix]; ok {
-		return
+		return prefix, c, ok
 	}
 
 	for full, candidate := range commands {
 		if strings.HasPrefix(full, prefix) {
 			if c != nil {
-				return nil, false // ambiguous
+				return "", nil, false // ambiguous
 			}
 			c = candidate
+			name = full
 		}
 	}
-	return c, c != nil
+	return name, c, c != nil
 }
 
 var errExitApp = errors.New("internal sentinel error value to quit the console reading loop")