From b24f06fabb766059db624dd8bc41ef1ff4d208ad Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 16 Mar 2020 14:12:08 -0300 Subject: [PATCH] fix error with empty list of candidates. --- README.md | 3 +-- engine.go | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 40cc73f..63f5373 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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| - diff --git a/engine.go b/engine.go index 273d2ef..15fb1ab 100644 --- a/engine.go +++ b/engine.go @@ -11,7 +11,7 @@ import ( const ( DefaultY int = 1 - FilterPrompt string = "[Filter]> " + FilterPrompt string = "[jq]> " ) type Engine struct { @@ -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: @@ -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 @@ -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