Skip to content

Commit 82654b8

Browse files
authored
Merge pull request #51 from simeji/v0.7.0
V0.7.0
2 parents e0b9167 + a245252 commit 82654b8

File tree

7 files changed

+393
-113
lines changed

7 files changed

+393
-113
lines changed

README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You can drill down JSON interactively by using filtering queries like [jq](https
88

99
## Demo
1010

11-
![demo-jid-main](https://github.com/simeji/jid/wiki/images/demo-jid-main-640.gif)
11+
![demo-jid-main](https://github.com/simeji/jid/wiki/images/demo-jid-main-640-colorize.gif)
1212

1313
## Installation
1414

@@ -112,6 +112,11 @@ jid < file.json
112112

113113
### Option
114114

115-
First argument: Initial query
116-
117-
-q : Print query (for jq)
115+
|option|description|
116+
|:-----------|:----------|
117+
|First argument ($1) | Initial query|
118+
|-h | print a help|
119+
|-help | print a help|
120+
|-version | print the version and exit|
121+
|-q | Output query mode (for jq)|
122+
|-M | monochrome output mode|

cmd/jid/jid.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ func main() {
1616
var qm bool
1717
var help bool
1818
var version bool
19+
var mono bool
1920
qs := "."
2021

2122
flag.BoolVar(&qm, "q", false, "Output query mode")
2223
flag.BoolVar(&help, "h", false, "print a help")
2324
flag.BoolVar(&help, "help", false, "print a help")
2425
flag.BoolVar(&version, "version", false, "print the version and exit")
26+
flag.BoolVar(&mono, "M", false, "monochrome output mode")
2527
flag.Parse()
2628

2729
if help {
@@ -38,7 +40,12 @@ func main() {
3840
qs = args[0]
3941
}
4042

41-
e, err := jid.NewEngine(content, qs)
43+
ea := &jid.EngineAttribute{
44+
DefaultQuery: qs,
45+
Monochrome: mono,
46+
}
47+
48+
e, err := jid.NewEngine(content, ea)
4249

4350
if err != nil {
4451
fmt.Println(err)

engine.go

+46-42
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,42 @@ type EngineResultInterface interface {
2424
}
2525

2626
type Engine struct {
27-
manager *JsonManager
28-
query QueryInterface
29-
term *Terminal
30-
complete []string
31-
keymode bool
32-
candidates []string
33-
candidatemode bool
34-
candidateidx int
35-
contentOffset int
36-
queryConfirm bool
37-
cursorOffsetX int
27+
manager *JsonManager
28+
query QueryInterface
29+
queryCursorIdx int
30+
term *Terminal
31+
complete []string
32+
keymode bool
33+
candidates []string
34+
candidatemode bool
35+
candidateidx int
36+
contentOffset int
37+
queryConfirm bool
3838
}
3939

40-
func NewEngine(s io.Reader, qs string) (EngineInterface, error) {
40+
type EngineAttribute struct {
41+
DefaultQuery string
42+
Monochrome bool
43+
}
44+
45+
func NewEngine(s io.Reader, ea *EngineAttribute) (EngineInterface, error) {
4146
j, err := NewJsonManager(s)
4247
if err != nil {
4348
return nil, err
4449
}
4550
e := &Engine{
4651
manager: j,
47-
term: NewTerminal(FilterPrompt, DefaultY),
48-
query: NewQuery([]rune(qs)),
52+
term: NewTerminal(FilterPrompt, DefaultY, ea.Monochrome),
53+
query: NewQuery([]rune(ea.DefaultQuery)),
4954
complete: []string{"", ""},
5055
keymode: false,
5156
candidates: []string{},
5257
candidatemode: false,
5358
candidateidx: 0,
5459
contentOffset: 0,
5560
queryConfirm: false,
56-
cursorOffsetX: 0,
5761
}
58-
e.cursorOffsetX = len(e.query.Get())
62+
e.queryCursorIdx = e.query.Length()
5963
return e, nil
6064
}
6165

@@ -94,7 +98,7 @@ func (e *Engine) Run() EngineResultInterface {
9498

9599
if e.query.StringGet() == "" {
96100
e.query.StringSet(".")
97-
e.cursorOffsetX = len(e.query.StringGet())
101+
e.queryCursorIdx = e.query.Length()
98102
}
99103

100104
contents = e.getContents()
@@ -103,15 +107,17 @@ func (e *Engine) Run() EngineResultInterface {
103107

104108
ta := &TerminalDrawAttributes{
105109
Query: e.query.StringGet(),
106-
CursorOffsetX: e.cursorOffsetX,
107110
Contents: contents,
108111
CandidateIndex: e.candidateidx,
109112
ContentsOffsetY: e.contentOffset,
110113
Complete: e.complete[0],
111114
Candidates: e.candidates,
115+
CursorOffset: e.query.IndexOffset(e.queryCursorIdx),
116+
}
117+
err = e.term.Draw(ta)
118+
if err != nil {
119+
panic(err)
112120
}
113-
114-
e.term.draw(ta)
115121

116122
switch ev := termbox.PollEvent(); ev.Type {
117123
case termbox.EventKey:
@@ -193,21 +199,22 @@ func (e *Engine) setCandidateData() {
193199
func (e *Engine) confirmCandidate() {
194200
_, _ = e.query.PopKeyword()
195201
_ = e.query.StringAdd(".")
196-
q := e.query.StringAdd(e.candidates[e.candidateidx])
197-
e.cursorOffsetX = len(q)
202+
_ = e.query.StringAdd(e.candidates[e.candidateidx])
203+
e.queryCursorIdx = e.query.Length()
198204
e.queryConfirm = true
199205
}
200206

201207
func (e *Engine) deleteChar() {
202-
if e.cursorOffsetX > 0 {
203-
_ = e.query.Delete(e.cursorOffsetX - 1)
204-
e.cursorOffsetX -= 1
208+
if i := e.queryCursorIdx - 1; i > 0 {
209+
_ = e.query.Delete(i)
210+
e.queryCursorIdx--
205211
}
212+
206213
}
207214

208215
func (e *Engine) deleteLineQuery() {
209216
_ = e.query.StringSet("")
210-
e.cursorOffsetX = 0
217+
e.queryCursorIdx = 0
211218
}
212219

213220
func (e *Engine) scrollToBelow() {
@@ -225,14 +232,12 @@ func (e *Engine) deleteWordBackward() {
225232
if k, _ := e.query.StringPopKeyword(); k != "" && !strings.Contains(k, "[") {
226233
_ = e.query.StringAdd(".")
227234
}
228-
e.cursorOffsetX = len(e.query.Get())
235+
e.queryCursorIdx = e.query.Length()
229236
}
230237
func (e *Engine) tabAction() {
231238
if !e.candidatemode {
232239
e.candidatemode = true
233-
if e.query.StringGet() == "" {
234-
_ = e.query.StringAdd(".")
235-
} else if e.complete[0] != e.complete[1] && e.complete[0] != "" {
240+
if e.complete[0] != e.complete[1] && e.complete[0] != "" {
236241
if k, _ := e.query.StringPopKeyword(); !strings.Contains(k, "[") {
237242
_ = e.query.StringAdd(".")
238243
}
@@ -243,36 +248,35 @@ func (e *Engine) tabAction() {
243248
} else {
244249
e.candidateidx = e.candidateidx + 1
245250
}
246-
e.cursorOffsetX = len(e.query.Get())
251+
e.queryCursorIdx = e.query.Length()
247252
}
248253
func (e *Engine) escapeCandidateMode() {
249254
e.candidatemode = false
250255
}
251256
func (e *Engine) inputChar(ch rune) {
252-
b := len(e.query.Get())
253-
q := e.query.StringInsert(string(ch), e.cursorOffsetX)
254-
if b < len(q) {
255-
e.cursorOffsetX += 1
256-
}
257+
_ = e.query.Insert([]rune{ch}, e.queryCursorIdx)
258+
e.queryCursorIdx++
257259
}
258260

259261
func (e *Engine) moveCursorBackward() {
260-
if e.cursorOffsetX > 0 {
261-
e.cursorOffsetX -= 1
262+
if i := e.queryCursorIdx - 1; i >= 0 {
263+
e.queryCursorIdx--
262264
}
263265
}
266+
264267
func (e *Engine) moveCursorForward() {
265-
if len(e.query.Get()) > e.cursorOffsetX {
266-
e.cursorOffsetX += 1
268+
if e.query.Length() > e.queryCursorIdx {
269+
e.queryCursorIdx++
267270
}
268271
}
272+
269273
func (e *Engine) moveCursorWordBackwark() {
270274
}
271275
func (e *Engine) moveCursorWordForward() {
272276
}
273277
func (e *Engine) moveCursorToTop() {
274-
e.cursorOffsetX = 0
278+
e.queryCursorIdx = 0
275279
}
276280
func (e *Engine) moveCursorToEnd() {
277-
e.cursorOffsetX = len(e.query.Get())
281+
e.queryCursorIdx = e.query.Length()
278282
}

0 commit comments

Comments
 (0)