|
| 1 | +//go:build new_readline |
| 2 | + |
| 3 | +package rline |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/reeflective/readline/inputrc" |
| 7 | + "golang.org/x/term" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "os/signal" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "github.com/mattn/go-isatty" |
| 14 | + "github.com/reeflective/readline" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + // ErrInterrupt is the interrupt error. |
| 19 | + ErrInterrupt = readline.ErrInterrupt |
| 20 | +) |
| 21 | + |
| 22 | +// baseRline should be embedded in a struct implementing the IO interface, |
| 23 | +// as it keeps implementation specific state. |
| 24 | +type baseRline struct { |
| 25 | + instance *readline.Shell |
| 26 | + prompt string |
| 27 | +} |
| 28 | + |
| 29 | +// Prompt sets the prompt for the next interactive line read. |
| 30 | +func (l *rline) Prompt(s string) { |
| 31 | + l.prompt = s |
| 32 | +} |
| 33 | + |
| 34 | +// Completer sets the auto-completer. |
| 35 | +func (l *rline) Completer(a Completer) { |
| 36 | + l.instance.Completer = func(line []rune, cursor int) readline.Completions { |
| 37 | + candidates, _ := a.Complete(line, cursor) |
| 38 | + values := make([]string, len(candidates)) |
| 39 | + for candidate := range candidates { |
| 40 | + values = append(values, string(candidate)) |
| 41 | + } |
| 42 | + return readline.CompleteValues(values...) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// SetOutput sets the output format func. |
| 47 | +func (l *rline) SetOutput(f func(string) string) { |
| 48 | + l.instance.SyntaxHighlighter = func(line []rune) string { |
| 49 | + return f(string(line)) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// New readline input/output handler. |
| 54 | +func New(forceNonInteractive bool, out, histfile string) (IO, error) { |
| 55 | + // determine if interactive |
| 56 | + interactive, cygwin := false, false |
| 57 | + if !forceNonInteractive { |
| 58 | + interactive = isatty.IsTerminal(os.Stdout.Fd()) && isatty.IsTerminal(os.Stdin.Fd()) |
| 59 | + cygwin = isatty.IsCygwinTerminal(os.Stdout.Fd()) && isatty.IsCygwinTerminal(os.Stdin.Fd()) |
| 60 | + } |
| 61 | + var stdout io.WriteCloser |
| 62 | + var closers []func() error |
| 63 | + switch { |
| 64 | + case out != "": |
| 65 | + var err error |
| 66 | + stdout, err = os.OpenFile(out, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0o644) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + closers = append(closers, stdout.Close) |
| 71 | + interactive = false |
| 72 | + default: |
| 73 | + stdout = os.Stdout |
| 74 | + } |
| 75 | + // configure stderr |
| 76 | + var stderr io.Writer = os.Stderr |
| 77 | + // TODO handle interrupts? |
| 78 | + options := []inputrc.Option{inputrc.WithName("usql")} |
| 79 | + /* |
| 80 | + &readline.Config{ |
| 81 | + HistoryFile: histfile, |
| 82 | + DisableAutoSaveHistory: true, |
| 83 | + InterruptPrompt: "^C", |
| 84 | + HistorySearchFold: true, |
| 85 | + Stdin: stdin, |
| 86 | + Stdout: stdout, |
| 87 | + Stderr: stderr, |
| 88 | + FuncIsTerminal: func() bool { |
| 89 | + return interactive || cygwin |
| 90 | + }, |
| 91 | + FuncFilterInputRune: func(r rune) (rune, bool) { |
| 92 | + if r == readline.CharCtrlZ { |
| 93 | + return r, false |
| 94 | + } |
| 95 | + return r, true |
| 96 | + }, |
| 97 | + } |
| 98 | + */ |
| 99 | + // create readline instance |
| 100 | + shell := readline.NewShell(options...) |
| 101 | + var history readline.History |
| 102 | + if histfile != "" { |
| 103 | + history, err := readline.NewHistoryFromFile(histfile) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + shell.History.Add("default", history) |
| 108 | + } |
| 109 | + |
| 110 | + n := func() ([]rune, error) { |
| 111 | + line, err := shell.Readline() |
| 112 | + return []rune(line), err |
| 113 | + } |
| 114 | + pw := func(prompt string) (string, error) { |
| 115 | + _, err := shell.Printf(prompt) |
| 116 | + if err != nil { |
| 117 | + return "", err |
| 118 | + } |
| 119 | + return readPassword() |
| 120 | + } |
| 121 | + if forceNonInteractive { |
| 122 | + n, pw = nil, nil |
| 123 | + } |
| 124 | + result := &rline{ |
| 125 | + baseRline: baseRline{instance: shell}, |
| 126 | + nextLine: n, |
| 127 | + close: func() error { |
| 128 | + for _, f := range closers { |
| 129 | + _ = f() |
| 130 | + } |
| 131 | + return nil |
| 132 | + }, |
| 133 | + stdout: stdout, |
| 134 | + stderr: stderr, |
| 135 | + isInteractive: interactive || cygwin, |
| 136 | + passwordPrompt: pw, |
| 137 | + } |
| 138 | + shell.Prompt.Primary(func() string { |
| 139 | + return result.prompt |
| 140 | + }) |
| 141 | + if history != nil { |
| 142 | + result.saveHistory = func(input string) error { |
| 143 | + _, err := history.Write(input) |
| 144 | + return err |
| 145 | + } |
| 146 | + } |
| 147 | + return result, nil |
| 148 | +} |
| 149 | + |
| 150 | +func readPassword() (string, error) { |
| 151 | + stdin := syscall.Stdin |
| 152 | + oldState, err := term.GetState(stdin) |
| 153 | + if err != nil { |
| 154 | + return "", err |
| 155 | + } |
| 156 | + defer term.Restore(stdin, oldState) |
| 157 | + |
| 158 | + sigch := make(chan os.Signal, 1) |
| 159 | + signal.Notify(sigch, os.Interrupt) |
| 160 | + go func() { |
| 161 | + for _ = range sigch { |
| 162 | + term.Restore(stdin, oldState) |
| 163 | + os.Exit(1) |
| 164 | + } |
| 165 | + }() |
| 166 | + |
| 167 | + password, err := term.ReadPassword(stdin) |
| 168 | + if err != nil { |
| 169 | + return "", err |
| 170 | + } |
| 171 | + return string(password), nil |
| 172 | +} |
0 commit comments