You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
676 B

  1. package main
  2. import (
  3. "strings"
  4. "github.com/chzyer/readline"
  5. )
  6. func main() {
  7. rl, err := readline.NewEx(&readline.Config{
  8. Prompt: "> ",
  9. HistoryFile: "/tmp/readline-multiline",
  10. DisableAutoSaveHistory: true,
  11. })
  12. if err != nil {
  13. panic(err)
  14. }
  15. defer rl.Close()
  16. var cmds []string
  17. for {
  18. line, err := rl.Readline()
  19. if err != nil {
  20. break
  21. }
  22. line = strings.TrimSpace(line)
  23. if len(line) == 0 {
  24. continue
  25. }
  26. cmds = append(cmds, line)
  27. if !strings.HasSuffix(line, ";") {
  28. rl.SetPrompt(">>> ")
  29. continue
  30. }
  31. cmd := strings.Join(cmds, " ")
  32. cmds = cmds[:0]
  33. rl.SetPrompt("> ")
  34. rl.SaveHistory(cmd)
  35. println(cmd)
  36. }
  37. }