Skip to content

Commit

Permalink
Merge pull request #52 from simeji/develop
Browse files Browse the repository at this point in the history
Add features : scroll to bottom and top
  • Loading branch information
simeji authored Jan 4, 2017
2 parents 82654b8 + d10e096 commit 1fe54ef
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions cmd/jid/jid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`
}
18 changes: 18 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1fe54ef

Please sign in to comment.