Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do autocomplete commands in cli application ( golang )?

Tags:

go

I want to do autocomplete my commands when press "TAB" key "\t" and execute command when press "ENTER" key "\r?\n". This code for example:

package main
import (
    "bufio"
    "fmt"
)
func main() {
    reader := bufio.NewReader(os.Stdin)
    command, err := reader.ReadString('\n')
    if err != nil {
         fmt.Printf("%s\n", err)
    } else {
        fmt.Printf("%s\n", command)
    }
}
like image 900
Andrew Makhnev Avatar asked Apr 28 '16 12:04

Andrew Makhnev


1 Answers

If you want bash-like functionality, then it's a lot more complicated than just using a bufio reader.

Check https://github.com/chzyer/readline

like image 77
OneOfOne Avatar answered Sep 30 '22 11:09

OneOfOne