Skip to content

Commit

Permalink
Improve syntax highlight (#118)
Browse files Browse the repository at this point in the history
* Improve highlight using go-multiline-ny v0.19.0

* Update README.md
  • Loading branch information
apstndb authored Jan 22, 2025
1 parent afbee45 commit 343980a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ You can control your Spanner databases with idiomatic SQL commands.
* Pager support when `CLI_USE_PAGER = TRUE`
* Progress bar of DDL execution.
* Syntax highlight when `CLI_ENABLE_HIGHLIGHT = TRUE`
* Note: Currently, highlighting is single line basis. See https://github.com/hymkor/go-multiline-ny/issues/6.
* Utilize other libraries
* Dogfooding [`cloudspannerecosystem/memefish`](https://github.com/cloudspannerecosystem/memefish)
* Spin out memefish logic as [`apstndb/gsqlutils`](https://github.com/apstndb/gsqlutils).
Expand Down
68 changes: 38 additions & 30 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/apstndb/adcplus"
"github.com/cloudspannerecosystem/memefish"
"github.com/cloudspannerecosystem/memefish/token"
"github.com/fatih/color"
"github.com/kballard/go-shellquote"
"github.com/ngicks/go-iterator-helper/hiter/stringsiter"
"github.com/nyaosorg/go-readline-ny"
Expand Down Expand Up @@ -243,49 +244,56 @@ func commentHighlighter() highlighterFunc {

var alnumRe = regexp.MustCompile("^[a-zA-Z0-9]+$")

func setLineEditor(ed *readline.Editor, enableHighlight bool) {
if enableHighlight {
ed.Highlight = []readline.Highlight{
// Note: multiline comments break highlight because of restriction of go-multiline-ny
{Pattern: commentHighlighter(), Sequence: "\x1B[37;49;2m"},
func colorToSequence(attr ...color.Attribute) string {
var sb strings.Builder
color.New(attr...).SetWriter(&sb)
return sb.String()
}

// string literals(including string-based literals like timestamp literals) and byte literals
{Pattern: kindHighlighter(token.TokenString, token.TokenBytes), Sequence: "\x1B[32;49;22m"},
var defaultHighlights = []readline.Highlight{
// Note: multiline comments break highlight because of restriction of go-multiline-ny
{Pattern: commentHighlighter(), Sequence: colorToSequence(color.FgWhite, color.Faint)},

// unclosed string literals
// Note: multiline literals break highlight because of restriction of go-multiline-ny
{Pattern: errorHighlighter(func(me *memefish.Error) bool {
return me.Message == errMessageUnclosedStringLiteral || me.Message == errMessageUnclosedTripleQuotedStringLiteral
}), Sequence: "\x1B[32;49;22m"},
// string literals(including string-based literals like timestamp literals) and byte literals
{Pattern: kindHighlighter(token.TokenString, token.TokenBytes), Sequence: colorToSequence(color.FgGreen, color.Bold)},

// numbers
{Pattern: kindHighlighter(token.TokenFloat, token.TokenInt), Sequence: "\x1B[34;49;22m"},
// unclosed string literals
// Note: multiline literals break highlight because of restriction of go-multiline-ny
{Pattern: errorHighlighter(func(me *memefish.Error) bool {
return me.Message == errMessageUnclosedStringLiteral || me.Message == errMessageUnclosedTripleQuotedStringLiteral
}), Sequence: colorToSequence(color.FgHiGreen, color.Bold)},

// params
{Pattern: kindHighlighter(token.TokenParam), Sequence: "\x1B[35;49;22m"},
// numbers
{Pattern: kindHighlighter(token.TokenFloat, token.TokenInt), Sequence: colorToSequence(color.FgHiBlue, color.Bold)},

// keywords
{Pattern: tokenHighlighter(func(tok token.Token) bool {
return alnumRe.MatchString(string(tok.Kind))
}), Sequence: "\x1B[33;49;22m"},
// params
{Pattern: kindHighlighter(token.TokenParam), Sequence: colorToSequence(color.FgMagenta, color.Bold)},

// idents
{Pattern: kindHighlighter(token.TokenIdent), Sequence: "\x1B[1m"},
}
ed.ResetColor = "\x1B[0m"
ed.DefaultColor = "\x1B[39;49;0m"
} else {
// keywords
{Pattern: tokenHighlighter(func(tok token.Token) bool {
return alnumRe.MatchString(string(tok.Kind))
}), Sequence: colorToSequence(color.FgHiYellow, color.Bold)},

// idents
{Pattern: kindHighlighter(token.TokenIdent), Sequence: colorToSequence(color.FgHiWhite)},
}

func setLineEditor(ed *multiline.Editor, enableHighlight bool) {
if color.NoColor || !enableHighlight {
ed.Highlight = nil
ed.DefaultColor = ""
ed.ResetColor = ""
return
}

ed.Highlight = defaultHighlights
ed.ResetColor = colorToSequence(color.Reset)
ed.DefaultColor = colorToSequence(color.Reset)
}

func (c *Cli) RunInteractive(ctx context.Context) int {
ed := &multiline.Editor{}

type ac = readline.AnonymousCommand
type _ = ac

err := ed.BindKey(keys.CtrlJ, readline.AnonymousCommand(ed.NewLine))
if err != nil {
return c.ExitOnError(err)
Expand Down Expand Up @@ -340,7 +348,7 @@ func (c *Cli) RunInteractive(ctx context.Context) int {
c.waitingStatus = ""

for {
setLineEditor(&ed.LineEditor, c.SystemVariables.EnableHighlight)
setLineEditor(ed, c.SystemVariables.EnableHighlight)
input, err := readInteractiveInput(ctx, ed)

// reset default
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ require (
github.com/apstndb/spanvalue v0.1.4
github.com/bufbuild/protocompile v0.14.1
github.com/cloudspannerecosystem/memefish v0.3.1
github.com/fatih/color v1.18.0
github.com/go-json-experiment/json v0.0.0-20250116043007-0640c115aea5
github.com/google/go-cmp v0.6.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0
github.com/hymkor/go-multiline-ny v0.18.4
github.com/hymkor/go-multiline-ny v0.19.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.18
github.com/nyaosorg/go-readline-ny v1.7.1
github.com/nyaosorg/go-readline-ny v1.7.2
github.com/olekukonko/tablewriter v0.0.5
github.com/samber/lo v1.47.0
github.com/testcontainers/testcontainers-go/modules/gcloud v0.35.0
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2583,6 +2583,8 @@ github.com/ettle/strcase v0.1.1/go.mod h1:hzDLsPC7/lwKyBOywSHEP89nt2pDgdy+No1NBA
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
Expand Down Expand Up @@ -2790,8 +2792,8 @@ github.com/hamba/avro/v2 v2.17.2/go.mod h1:Q9YK+qxAhtVrNqOhwlZTATLgLA8qxG2vtvkhK
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hymkor/go-multiline-ny v0.18.4 h1:70UU1kfSU4jMNqTnoUe4540Nh5GhBLOW9jyLURSbB3I=
github.com/hymkor/go-multiline-ny v0.18.4/go.mod h1:8OTEhgedoh8bgs4BeI/JxeAipLVl2XkjyVeYfAirkrU=
github.com/hymkor/go-multiline-ny v0.19.0 h1:ta9D0ru8RkcryityylxjNLzE7Zz2VSt/Zq8qYUTiZrY=
github.com/hymkor/go-multiline-ny v0.19.0/go.mod h1:F+BC4rwSyjXyMD/vadcpuWlR30w1cEFMjbu0DGtdt/I=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -2881,8 +2883,8 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/ngicks/go-iterator-helper v0.0.18 h1:a9a3ndHDyYSsI9bLTV4LOUA9cg6NpwPyfL20t4HoLVw=
github.com/ngicks/go-iterator-helper v0.0.18/go.mod h1:g++KxWVGEkOnIhXVvpNNOdn7ON57aOpfu80ccBvPVHI=
github.com/nyaosorg/go-readline-ny v1.7.1 h1:iDRf9BdhI5itBvvQUywNxH+j9Il1YsbZBj3O5v9GnpY=
github.com/nyaosorg/go-readline-ny v1.7.1/go.mod h1:54AzdC//M5EzTWRdvUHv2ChuYgp58mRrStTlpxiCmT0=
github.com/nyaosorg/go-readline-ny v1.7.2 h1:qRAVduAukv0bXGFEImlSUmTlVVGWISiWzIAwwPY26u8=
github.com/nyaosorg/go-readline-ny v1.7.2/go.mod h1:54AzdC//M5EzTWRdvUHv2ChuYgp58mRrStTlpxiCmT0=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down

0 comments on commit 343980a

Please sign in to comment.