func (a *App) internalRun() error { if a.commandTitle == "" { a.commandTitle = ">" } var historyFile string usr, err := user.Current() // Only load history if we can get the user if err == nil { historyFile = filepath.Join(usr.HomeDir, ".command_history") } l, err := readline.NewEx(&readline.Config{ Prompt: a.commandTitle, HistoryFile: historyFile, AutoComplete: readline.NewPrefixCompleter(a.completeOptions...), }) if err != nil { return err } a.line = l log.SetOutput(l.Stderr()) for { l, e := a.line.Readline() if e != nil { return e } if !a.parseCommand(l) { break // exit main loop } } return nil }
func usage(w io.Writer) { io.WriteString(w, ` setprompt <prompt> say <hello> bye `[1:]) } var completer = readline.NewPrefixCompleter( readline.PcItem("say", readline.PcItem("hello"), readline.PcItem("bye"), ), readline.PcItem("setprompt"), readline.PcItem("bye"), readline.PcItem("help"), readline.PcItem("go", readline.PcItem("build"), readline.PcItem("install"), readline.PcItem("test"), ), ) func main() { l, err := readline.NewEx(&readline.Config{ Prompt: "\033[31m»\033[0m ", HistoryFile: "/tmp/readline.tmp", AutoComplete: completer, }) if err != nil { panic(err)
set-log-level <log-level> [TODO] inspect-block <data-block-id> show-tree exit `[1:]) } var completer = readline.NewPrefixCompleter( readline.PcItem("insert"), readline.PcItem("bulk-insert"), readline.PcItem("update"), readline.PcItem("find"), readline.PcItem("help"), readline.PcItem("delete"), readline.PcItem("bulk-delete"), readline.PcItem("search"), readline.PcItem("set-log-level", readline.PcItem("debug"), readline.PcItem("info"), readline.PcItem("warn"), ), readline.PcItem("show-tree"), readline.PcItem("exit"), ) func main() { log.SetLevel(log.WarnLevel) log.SetOutput(os.Stderr) l, err := readline.NewEx(&readline.Config{ Prompt: "\033[31m»\033[0m ",
var completer = readline.NewPrefixCompleter( readline.PcItem("mode", readline.PcItem("vi"), readline.PcItem("emacs"), ), readline.PcItem("login"), readline.PcItem("say", readline.PcItemDynamic(listFiles("./"), readline.PcItem("with", readline.PcItem("following"), readline.PcItem("items"), ), ), readline.PcItem("hello"), readline.PcItem("bye"), ), readline.PcItem("setprompt"), readline.PcItem("setpassword"), readline.PcItem("bye"), readline.PcItem("help"), readline.PcItem("go", readline.PcItem("build", readline.PcItem("-o"), readline.PcItem("-v")), readline.PcItem("install", readline.PcItem("-v"), readline.PcItem("-vv"), readline.PcItem("-vvv"), ), readline.PcItem("test"), ), readline.PcItem("sleep"), )
io.WriteString(w, completer.Tree(" ")) } var completer = readline.NewPrefixCompleter( readline.PcItem("mode", readline.PcItem("vi"), readline.PcItem("emacs"), ), readline.PcItem("login"), readline.PcItem("say", readline.PcItem("hello"), readline.PcItem("bye"), ), readline.PcItem("setprompt"), readline.PcItem("setpassword"), readline.PcItem("bye"), readline.PcItem("help"), readline.PcItem("go", readline.PcItem("build", readline.PcItem("-o"), readline.PcItem("-v")), readline.PcItem("install", readline.PcItem("-v"), readline.PcItem("-vv"), readline.PcItem("-vvv"), ), readline.PcItem("test"), ), readline.PcItem("sleep"), ) func main() { l, err := readline.NewEx(&readline.Config{
func main() { kingpin.Parse() var conn net.Conn var err error if *rawtls { conn, err = tls.Dial("tcp", *address, &tls.Config{ InsecureSkipVerify: true, }) if err != nil { fmt.Fprintf(os.Stderr, "Connect failed: %v\n", err) return } } else { conn, err = net.Dial("tcp", *address) if err != nil { fmt.Fprintf(os.Stderr, "Connect failed: %v\n", err) return } } c := client.New(conn) go func() { err := c.Serve() if err != nil { fmt.Fprintf(os.Stderr, "\nConnection lost: %v\n", err) os.Exit(-1) } }() _, _, err = c.Version(1024*1024, qp.Version) if err != nil { fmt.Fprintf(os.Stderr, "Version failed: %v\n", err) return } root, _, err := c.Attach(nil, *user, *service) if err != nil { fmt.Fprintf(os.Stderr, "Attach failed: %v\n", err) return } cwd := root confirmation, err = readline.New("") if err != nil { fmt.Fprintf(os.Stderr, "Could not make confirmation readline instance: %v\n", err) return } if len(*command) > 0 { args := "" for i := 1; i < len(*command); i++ { if i != 1 { args += " " } args += (*command)[i] } f, ok := cmds[(*command)[0]] if !ok { fmt.Fprintf(os.Stderr, "no such command: [%s]\n", *command) return } cwd, err = f(root, cwd, args) if err != nil { fmt.Fprintf(os.Stderr, "\ncommand %s failed: %v\n", *command, err) } return } completer := readline.NewPrefixCompleter() for k := range cmds { completer.Children = append(completer.Children, readline.PcItem(k)) } rl, err := readline.NewEx(&readline.Config{ Prompt: "9p> ", AutoComplete: completer, }) if err != nil { fmt.Fprintf(os.Stderr, "failed to create readline: %v\n", err) return } defer rl.Close() fmt.Fprintf(os.Stderr, "Welcome to the qptools 9P cli.\nPress tab to see available commands.\n") for { line, err := rl.Readline() if err != nil { // io.EOF break } idx := strings.Index(line, " ") var cmd, args string if idx != -1 { cmd = line[:idx] args = line[idx+1:] } else { cmd = line } f, ok := cmds[cmd] if !ok { fmt.Fprintf(os.Stderr, "no such command: [%s]\n", cmd) continue } cwd, err = f(root, cwd, args) if err != nil { fmt.Fprintf(os.Stderr, "\ncommand %s failed: %v\n", cmd, err) } } }
func main() { loop := true if len(os.Args) < 4 { fmt.Printf("Too few arguments\n") return } addr := os.Args[1] user := os.Args[2] service := os.Args[3] c := &convenience.Client{} err := c.Dial("tcp", addr, user, service) if err != nil { fmt.Printf("Connect failed: %v\n", err) return } var cmds map[string]func(string) cmds = map[string]func(string){ "ls": func(s string) { if s == "" { s = "/" } strs, err := c.List(s) if err != nil { fmt.Printf("ls failed: %v\n", err) return } fmt.Printf("%v\n", strs) }, "cat": func(s string) { strs, err := c.Read(s) if err != nil { fmt.Printf("cat failed: %v\n", err) return } fmt.Printf("%v\n", strs) }, "get": func(string) { fmt.Printf("get is not yet implemented\n") }, "put": func(string) { fmt.Printf("put is not yet implemented\n") }, "mkdir": func(s string) { err := c.Create(s, true) if err != nil { fmt.Printf("mkdir failed: %v\n", err) return } }, "rm": func(s string) { err := c.Remove(s) if err != nil { fmt.Printf("rm failed: %v\n", err) return } }, "chmod": func(string) { fmt.Printf("chmod is not yet implemented\n") }, "quit": func(string) { fmt.Printf("bye\n") loop = false }, "help": func(string) { fmt.Printf("Available commands: \n") for k := range cmds { fmt.Printf("\t%s\n", k) } }, } completer := readline.NewPrefixCompleter() for k := range cmds { completer.Children = append(completer.Children, readline.PcItem(k)) } rl, err := readline.NewEx(&readline.Config{ Prompt: "9p> ", AutoComplete: completer, }) if err != nil { fmt.Printf("failed to create readline: %v\n", err) return } defer rl.Close() for loop { line, err := rl.Readline() if err != nil { // io.EOF break } idx := strings.Index(line, " ") var cmd, args string if idx != -1 { cmd = line[:idx] args = line[idx+1:] } else { cmd = line } f, ok := cmds[cmd] if !ok { fmt.Printf("no such command: [%s]\n", cmd) continue } f(args) } }