Skip to content

Commit 7d6af91

Browse files
authored
Merge pull request #9 from fiatjaf/spaces-newlines
quoting and unquoting strings, printing empty lines correctly and handling `"`
2 parents 8079b00 + 68394b4 commit 7d6af91

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

commands/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var (
66
OK = []byte("ok")
77
TRUE = []byte("true")
88
FALSE = []byte("false")
9-
SCAN_MORE = []byte(`"it" for more`)
9+
SCAN_MORE = []byte(`'it' for more`)
1010

1111
units = []string{"KB", "MB", "GB", "TB", "PB"}
1212
)

commands/help.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package commands
22

33
import "lmdb-cli/core"
44

5-
var helpText = []byte(`
5+
var helpText = `
66
del KEY - removes the key/value
77
get KEY FORMAT - gets the value. FORMAT is optional or 'json' or 'hex'
88
set KEY - creates or overwrites the key with the specified value
@@ -20,12 +20,11 @@ var helpText = []byte(`
2020
2121
exit - exits the program
2222
(aliases: quit, CTRL-C)
23-
`)
23+
`
2424

25-
type Help struct {
26-
}
25+
type Help struct{}
2726

2827
func (cmd Help) Execute(context *core.Context, input []byte) (err error) {
29-
context.Output(helpText)
28+
context.OutputString(helpText)
3029
return nil
3130
}

commands/parser.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"bytes"
55
"encoding/hex"
66
"errors"
7+
"strconv"
78
"unicode"
89
)
910

1011
const (
1112
STATE_NONE int = iota
1213
STATE_WORD
1314
STATE_QUOTE
14-
STATE_ESCAPED
1515
)
1616

1717
var (
@@ -45,6 +45,7 @@ func parse(in []byte) [][]byte {
4545
arg = arg[2 : 2+n]
4646
}
4747
}
48+
4849
results = append(results, arg)
4950
}
5051

@@ -53,13 +54,11 @@ func parse(in []byte) [][]byte {
5354
case STATE_NONE:
5455
if isQuote(b) {
5556
state = STATE_QUOTE
57+
arg = append(arg, '"')
5658
} else if !isWhiteSpace(b) {
5759
arg = append(arg, b)
5860
state = STATE_WORD
5961
}
60-
case STATE_ESCAPED:
61-
arg = append(arg, b)
62-
state = STATE_QUOTE
6362
case STATE_WORD:
6463
if isWhiteSpace(b) {
6564
pushArg()
@@ -69,9 +68,12 @@ func parse(in []byte) [][]byte {
6968
arg = append(arg, b)
7069
}
7170
case STATE_QUOTE:
72-
if b == '\\' {
73-
state = STATE_ESCAPED
74-
} else if isQuote(b) {
71+
if isQuote(b) {
72+
arg = append(arg, '"')
73+
unquoted, err := strconv.Unquote(string(arg))
74+
if err == nil {
75+
arg = []byte(unquoted)
76+
}
7577
pushArg()
7678
arg = make([]byte, 0)
7779
state = STATE_NONE

core/context.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"log"
88
"path"
9+
"strconv"
910

1011
"github.com/bmatsuo/lmdb-go/lmdb"
1112
)
@@ -156,15 +157,29 @@ func (c *Context) Close() {
156157
c.Env.Close()
157158
}
158159

160+
func (c *Context) OutputString(data string) {
161+
c.writer.Write([]byte(data))
162+
}
163+
159164
func (c *Context) Output(data []byte) {
165+
if len(data) == 0 {
166+
c.writer.Write([]byte{'\n'})
167+
return
168+
}
169+
160170
n := len(data)
161171
readableCharacters := 0
162172
for _, b := range data {
163-
if b >= 33 && b <= 126 {
173+
if b >= 32 && b <= 126 || b == 10 {
164174
readableCharacters++
165175
}
166176
}
167177
if readableCharacters > n*2/3 {
178+
sdata := string(data)
179+
quoted := strconv.QuoteToASCII(sdata)
180+
if quoted[1:len(quoted)-1] != sdata {
181+
data = []byte(quoted)
182+
}
168183
c.writer.Write(data)
169184
} else {
170185
c.writer.Write([]byte("0x" + hex.EncodeToString(data)))

0 commit comments

Comments
 (0)