Skip to content

Commit

Permalink
Implement CLI_PAGER and CLI_AUTOWRAP (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb authored Dec 12, 2024
1 parent 9d9c200 commit 98878e3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ You can control your Spanner databases with idiomatic SQL commands.
* Allow newlines in prompt using `%n`
* System variables expansion
* Prompt2 with margin and waiting status
* Autowrap and auto adjust column width to fit within terminal width.
* Autowrap and auto adjust column width to fit within terminal width when `CLI_AUTOWRAP = TRUE`).
* Pager support when `CLI_USE_PAGER = TRUE`
* Progress bar of DDL execution.
* Utilize other libraries
* Dogfooding [`cloudspannerecosystem/memefish`](https://github.com/cloudspannerecosystem/memefish)
Expand Down Expand Up @@ -521,6 +522,8 @@ They have almost same semantics with [Spanner JDBC properties](https://cloud.goo
| CLI_INSECURE | READ_WRITE | `"FALSE"` |
| CLI_QUERY_MODE | READ_WRITE | `"PROFILE"` |
| CLI_LINT_PLAN | READ_WRITE | `"TRUE"` |
| CLI_USE_PAGER | READ_WRITE | `"TRUE"` |
| CLI_AUTOWRAP | READ_WRITE | `"TRUE"` |

### Embedded Cloud Spanner Emulator

Expand Down
46 changes: 42 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ import (
"log"
"math"
"os"
"os/exec"
"os/signal"
"regexp"
"slices"
"strconv"
"strings"
"time"

"github.com/kballard/go-shellquote"

"github.com/nyaosorg/go-readline-ny"

"github.com/hymkor/go-multiline-ny"
Expand Down Expand Up @@ -348,9 +351,12 @@ func (c *Cli) RunInteractive(ctx context.Context) int {
c.updateSystemVariables(result)
}

size, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
size = math.MaxInt
size := math.MaxInt
if c.SystemVariables.AutoWrap {
size, _, err = term.GetSize(int(os.Stdout.Fd()))
if err != nil {
size = math.MaxInt
}
}

c.PrintResult(size, result, c.SystemVariables.CLIFormat, true)
Expand Down Expand Up @@ -426,7 +432,39 @@ func (c *Cli) PrintBatchError(err error) {
}

func (c *Cli) PrintResult(screenWidth int, result *Result, mode DisplayMode, interactive bool) {
printResult(c.SystemVariables.Debug, screenWidth, c.OutStream, result, mode, interactive, c.SystemVariables.Verbose)
ostream := c.OutStream
var cmd *exec.Cmd
if c.SystemVariables.UsePager {
pagerpath := cmp.Or(os.Getenv("PAGER"), "less")

split, err := shellquote.Split(pagerpath)
if err != nil {
return
}
cmd = exec.CommandContext(context.Background(), split[0], split[1:]...)

pr, pw := io.Pipe()
ostream = pw
cmd.Stdin = pr
cmd.Stdout = c.OutStream

err = cmd.Start()
if err != nil {
log.Println(err)
return
}
defer func() {
err := pw.Close()
if err != nil {
log.Println(err)
}
err = cmd.Wait()
if err != nil {
log.Println(err)
}
}()
}
printResult(c.SystemVariables.Debug, screenWidth, ostream, result, mode, interactive, c.SystemVariables.Verbose)
}

func (c *Cli) PrintProgressingMark() func() {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/hymkor/go-multiline-ny v0.17.0
github.com/jessevdk/go-flags v1.6.1
github.com/k0kubun/pp/v3 v3.4.1
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/mattn/go-runewidth v0.0.16
github.com/ngicks/go-iterator-helper v0.0.15
github.com/nyaosorg/go-readline-ny v1.6.2
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/k0kubun/pp/v3 v3.4.1 h1:1WdFZDRRqe8UsR61N/2RoOZ3ziTEqgTPVqKrHeb779Y=
github.com/k0kubun/pp/v3 v3.4.1/go.mod h1:+SiNiqKnBfw1Nkj82Lh5bIeKQOAkPy6Xw9CAZUZ8npI=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
15 changes: 15 additions & 0 deletions system_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type systemVariables struct {
LintPlan bool
TransactionTag string
RequestTag string
UsePager bool

// it is internal variable and hidden from system variable statements
ProtoDescriptor *descriptorpb.FileDescriptorSet
Expand All @@ -58,6 +59,7 @@ type systemVariables struct {
CurrentSession *Session
ReadOnly bool
VertexAIProject string
AutoWrap bool
}

var errIgnored = errors.New("ignored")
Expand Down Expand Up @@ -453,6 +455,12 @@ var accessorMap = map[string]accessor{
}),
Setter: boolSetter(func(sysVars *systemVariables) *bool { return &sysVars.LintPlan }),
},
"CLI_USE_PAGER": boolAccessor(func(variables *systemVariables) *bool {
return &variables.UsePager
}),
"CLI_AUTOWRAP": boolAccessor(func(variables *systemVariables) *bool {
return &variables.AutoWrap
}),
"CLI_QUERY_MODE": {
Getter: func(this *systemVariables, name string) (map[string]string, error) {
if this.QueryMode == nil {
Expand Down Expand Up @@ -549,6 +557,13 @@ func stringSetter(f func(sysVars *systemVariables) *string) setter {
}
}

func boolAccessor(f func(variables *systemVariables) *bool) accessor {
return accessor{
Setter: boolSetter(f),
Getter: boolGetter(f),
}
}

func stringAccessor(f func(variables *systemVariables) *string) accessor {
return accessor{
Setter: stringSetter(f),
Expand Down

0 comments on commit 98878e3

Please sign in to comment.