Skip to content

Commit

Permalink
clisqlshell: don't process \p and \r in multi-line editors
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
knz committed Nov 20, 2022
1 parent 887c280 commit 1091326
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cli/clisqlshell/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type editor interface {
addHistory(line string) error
canPrompt() bool
setPrompt(prompt string)
multilineEdit() bool
}

type sqlShell interface {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/clisqlshell/editor_bubbline.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (b *bubblineReader) canPrompt() bool {
return true
}

func (b *bubblineReader) multilineEdit() bool {
return false
}

func (b *bubblineReader) setPrompt(prompt string) {
b.ins.Prompt = prompt
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/clisqlshell/editor_bufio.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (b *bufioReader) setPrompt(prompt string) {
}
}

func (b *bufioReader) multilineEdit() bool {
return false
}

func (b *bufioReader) getLine() (string, error) {
fmt.Fprint(b.wout, b.prompt)
l, err := b.buf.ReadString('\n')
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/clisqlshell/editor_editline.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func (b *editlineReader) setPrompt(prompt string) {
b.ins.SetLeftPrompt(prompt)
}

func (b *editlineReader) multilineEdit() bool {
return false
}

func (b *editlineReader) GetCompletions(word string) []string {
if b.sql.inCopy() {
return []string{word + "\t"}
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/clisqlshell/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,16 +1247,28 @@ func (c *cliState) doHandleCliCmd(loopState, nextState cliStateEnum) cliStateEnu
return c.runOpen(cmd[1:], loopState, errState)

case `\p`:
if c.ins.multilineEdit() {
fmt.Fprintln(c.iCtx.stderr, `warning: \p is ineffective with this editor`)
break
}
// This is analogous to \show but does not need a special case.
// Implemented for compatibility with psql.
fmt.Fprintln(c.iCtx.stdout, strings.Join(c.partialLines, "\n"))

case `\r`:
if c.ins.multilineEdit() {
fmt.Fprintln(c.iCtx.stderr, `warning: \r is ineffective with this editor`)
break
}
// Reset the input buffer so far. This is useful when e.g. a user
// got confused with string delimiters and multi-line input.
return cliStartLine

case `\show`:
if c.ins.multilineEdit() {
fmt.Fprintln(c.iCtx.stderr, `warning: \show is ineffective with this editor`)
break
}
fmt.Fprintln(c.iCtx.stderr, `warning: \show is deprecated. Use \p.`)
if len(c.partialLines) == 0 {
fmt.Fprintf(c.iCtx.stderr, "No input so far. Did you mean SHOW?\n")
Expand Down

0 comments on commit 1091326

Please sign in to comment.