From d10e096b3607c2145694d879ed98aec70421b4f4 Mon Sep 17 00:00:00 2001 From: simeji Date: Thu, 5 Jan 2017 00:43:41 +0900 Subject: [PATCH] Add features : scroll to bottom and top --- README.md | 3 +++ cmd/jid/jid.go | 9 +++++++++ engine.go | 18 ++++++++++++++++++ engine_test.go | 10 ++++++++++ 4 files changed, 40 insertions(+) diff --git a/README.md b/README.md index dc044c1..a45db26 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,10 @@ jid < file.json |`CTRL` + `E`|To the end of the 'Filter'| |`CTRL` + `J`|Scroll json buffer 1 line downwards| |`CTRL` + `K`|Scroll json buffer 1 line upwards| +|`CTRL` + `G`|Scroll json buffer to bottom| +|`CTRL` + `T`|Scroll json buffer to top| |`CTRL` + `L`|Change view mode whole json or keys (only object)| +|`ESC`|Hide a candidate box| ### Option diff --git a/cmd/jid/jid.go b/cmd/jid/jid.go index e20ef87..06e7086 100644 --- a/cmd/jid/jid.go +++ b/cmd/jid/jid.go @@ -104,8 +104,17 @@ CTRL-J CTRL-K Scroll json buffer 1 line upwards +CTRL-G + Scroll json buffer to bottom + +CTRL-T + Scroll json buffer to top + CTRL-L Change view mode whole json or keys (only object) +ESC + Hide a candidate box + ` } diff --git a/engine.go b/engine.go index 767103e..029434a 100644 --- a/engine.go +++ b/engine.go @@ -101,9 +101,13 @@ func (e *Engine) Run() EngineResultInterface { e.queryCursorIdx = e.query.Length() } + bl := len(contents) contents = e.getContents() e.setCandidateData() e.queryConfirm = false + if bl != len(contents) { + e.contentOffset = 0 + } ta := &TerminalDrawAttributes{ Query: e.query.StringGet(), @@ -140,6 +144,10 @@ func (e *Engine) Run() EngineResultInterface { e.scrollToAbove() case termbox.KeyCtrlJ: e.scrollToBelow() + case termbox.KeyCtrlG: + e.scrollToBottom(len(contents)) + case termbox.KeyCtrlT: + e.scrollToTop() case termbox.KeyCtrlL: e.toggleKeymode() case termbox.KeyCtrlU: @@ -220,11 +228,21 @@ func (e *Engine) deleteLineQuery() { func (e *Engine) scrollToBelow() { e.contentOffset++ } + func (e *Engine) scrollToAbove() { if o := e.contentOffset - 1; o >= 0 { e.contentOffset = o } } + +func (e *Engine) scrollToBottom(rownum int) { + e.contentOffset = rownum - 1 +} + +func (e *Engine) scrollToTop() { + e.contentOffset = 0 +} + func (e *Engine) toggleKeymode() { e.keymode = !e.keymode } diff --git a/engine_test.go b/engine_test.go index 9d02acc..9492847 100644 --- a/engine_test.go +++ b/engine_test.go @@ -103,6 +103,16 @@ func TestScrollToBelow(t *testing.T) { e.scrollToBelow() assert.Equal(3, e.contentOffset) } +func TestScrollToBottomAndTop(t *testing.T) { + var assert = assert.New(t) + e := getEngine(`{"named":"go","NameTest":[1,2,3]}`, "") + + e.scrollToBottom(5) + assert.Equal(4, e.contentOffset) + + e.scrollToTop() + assert.Equal(0, e.contentOffset) +} func TestGetContents(t *testing.T) { var assert = assert.New(t)