Skip to content

Commit

Permalink
cli: add \x command to toggle records display format
Browse files Browse the repository at this point in the history
`psql` has a `\x` command to toggle extended output for results. CRDB
already supports this output format via `\set display_format=records`.
This commit adds `\x [on|off]` to toggle between `records` and `table`
display formats.

The `auto` option from `psql` to automatically enable extended output
depending on the result width is not supported, only `on` and `off`.

Resolves #56706.

Release note (cli change): A `\x [on|off]` command has been added to
toggle the `records` display format, following `psql` behavior.
  • Loading branch information
erikgrinaker committed Nov 19, 2020
1 parent 5fb712f commit 07ecb7f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/cli/interactive_tests/test_local_cmds.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,30 @@ eexpect "display_format\ttsv"
eexpect root@
end_test

start_test "Check that \\x toggles display format"
send "\\x\r\\set\r"
eexpect "Option*|*display_format"
eexpect "Value*|*records"
eexpect root@

send "\\x\r\\set\r"
eexpect "display_format*|*table"
eexpect root@
end_test

start_test "Check that \\x with on or off enables/disables records display format"
send "\\x on\r\\set\r"
eexpect "Option*|*display_format"
eexpect "Value*|*records"
eexpect root@

send "\\x off\r\\set\r"
eexpect "display_format*|*table"
eexpect root@
end_test

start_test "Check various ways to set a boolean flag."
send "\\set display_format=tsv\r"
send "\\set show_times=false\r\\set\r"
eexpect "show_times\tfalse"
eexpect root@
Expand Down
25 changes: 25 additions & 0 deletions pkg/cli/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Informational
\du list the users for all databases.
\d [TABLE] show details about columns in the specified table, or alias for '\dt' if no table is specified.
Formatting
\x [on|off] toggle records display format.
Operating System
\! CMD run an external command and print its results on standard output.
Expand Down Expand Up @@ -1161,6 +1164,28 @@ func (c *cliState) doHandleCliCmd(loopState, nextState cliStateEnum) cliStateEnu
}
return c.invalidSyntax(errState, `%s. Try \? for help`, c.lastInputLine)

case `\x`:
format := tableDisplayRecords
switch len(cmd) {
case 1:
if cliCtx.tableDisplayFormat == tableDisplayRecords {
format = tableDisplayTable
}
case 2:
b, err := parseBool(cmd[1])
if err != nil {
return c.invalidSyntax(errState, `%s. Try \? for help.`, c.lastInputLine)
} else if b {
format = tableDisplayRecords
} else {
format = tableDisplayTable
}
default:
return c.invalidSyntax(errState, `%s. Try \? for help.`, c.lastInputLine)
}
cliCtx.tableDisplayFormat = format
return loopState

case `\demo`:
return c.handleDemo(cmd[1:], loopState, errState)

Expand Down

0 comments on commit 07ecb7f

Please sign in to comment.