Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
fix error with empty list of candidates.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Mar 16, 2020
1 parent 26f75a9 commit b24f06f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Then jiq will be running. Now you can dig JSON data incrementally.
When you enter `.bb.aaa[2]`, you will see the following.

```
[Filter]> .bb.aaa[2]
[jq]> .bb.aaa[2]
[
1,
2
Expand Down Expand Up @@ -110,4 +110,3 @@ traffic analytics for this repo:
|`CTRL` + `N` / PageDown|Scroll json buffer 'Page Down'|
|`CTRL` + `P` / PageUp|Scroll json buffer 'Page Up'|
|`ESC`|Hide a candidate box|

30 changes: 15 additions & 15 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
DefaultY int = 1
FilterPrompt string = "[Filter]> "
FilterPrompt string = "[jq]> "
)

type Engine struct {
Expand Down Expand Up @@ -149,12 +149,12 @@ func (e *Engine) Run() *EngineResult {
e.scrollToAbove()
case termbox.KeyCtrlJ, termbox.KeyArrowDown:
e.scrollToBelow()
case termbox.KeyCtrlN, termbox.KeyPgdn:
_, h := termbox.Size()
e.scrollPageDown(len(contents), h)
case termbox.KeyCtrlP, termbox.KeyPgup:
_, h := termbox.Size()
e.scrollPageUp(h)
case termbox.KeyCtrlN, termbox.KeyPgdn:
_, h := termbox.Size()
e.scrollPageDown(len(contents), h)
case termbox.KeyCtrlP, termbox.KeyPgup:
_, h := termbox.Size()
e.scrollPageUp(h)
case termbox.KeyEsc:
e.candidatemode = false
case termbox.KeyEnter:
Expand Down Expand Up @@ -198,7 +198,7 @@ func (e *Engine) makeCandidates() {
keys, err := jqrun(validUntilNow+" | keys", e.json, []string{"-c"})
if err == nil {
candidates := strings.Split(keys[1:len(keys)-1], ",")
if len(candidates) > 0 && candidates[0][0] == '"' {
if len(candidates[0]) > 0 && candidates[0][0] == '"' {
// only suggest if keys are strings
for _, cand := range candidates {
// filter out candidates with the wrong prefix
Expand Down Expand Up @@ -258,15 +258,15 @@ func (e *Engine) scrollToAbove() {
}
}
func (e *Engine) scrollPageDown(rownum int, height int) {
co := rownum - 1
if o := rownum - e.contentOffset; o > height {
co = e.contentOffset + (height - DefaultY)
}
e.contentOffset = co
co := rownum - 1
if o := rownum - e.contentOffset; o > height {
co = e.contentOffset + (height - DefaultY)
}
e.contentOffset = co
}
func (e *Engine) scrollPageUp(height int) {
co := 0
if o := e.contentOffset - (height - DefaultY); o > 0 {
co := 0
if o := e.contentOffset - (height - DefaultY); o > 0 {
co = o
}
e.contentOffset = co
Expand Down

0 comments on commit b24f06f

Please sign in to comment.