Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add page up/down #82

Merged
merged 2 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ jid < file.json
|`CTRL` + `K`|Scroll json buffer 1 line upwards|
|`CTRL` + `G`|Scroll json buffer to bottom|
|`CTRL` + `T`|Scroll json buffer to top|
|`CTRL` + `N`|Scroll json buffer 'Page Down'|
|`CTRL` + `P`|Scroll json buffer 'Page Up'|
|`CTRL` + `L`|Change view mode whole json or keys (only object)|
|`ESC`|Hide a candidate box|

Expand Down
8 changes: 7 additions & 1 deletion cmd/jid/jid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/simeji/jid"
)

const VERSION = "0.7.3"
const VERSION = "0.7.5"

func main() {
content := os.Stdin
Expand Down Expand Up @@ -114,6 +114,12 @@ CTRL-G
CTRL-T
Scroll json buffer to top

CTRL-N
Scroll json buffer 'Page Down'

CTRL-P
Scroll json buffer 'Page Up'

CTRL-L
Change view mode whole json or keys (only object)

Expand Down
32 changes: 27 additions & 5 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io"
"strings"

"github.com/nsf/termbox-go"
termbox "github.com/nsf/termbox-go"
)

const (
Expand Down Expand Up @@ -151,6 +151,12 @@ func (e *Engine) Run() EngineResultInterface {
e.scrollToBottom(len(contents))
case termbox.KeyCtrlT:
e.scrollToTop()
case termbox.KeyCtrlN:
_, h := termbox.Size()
e.scrollPageDown(len(contents), h)
case termbox.KeyCtrlP:
_, h := termbox.Size()
e.scrollPageUp(h)
case termbox.KeyCtrlL:
e.toggleKeymode()
case termbox.KeyCtrlU:
Expand All @@ -165,14 +171,14 @@ func (e *Engine) Run() EngineResultInterface {
var err error
if e.prettyResult {
cc, _, _, err = e.manager.GetPretty(e.query, true)
}else{
} else {
cc, _, _, err = e.manager.Get(e.query, true)
}

return &EngineResult{
content: cc,
qs: e.query.StringGet(),
err: err,
content: cc,
qs: e.query.StringGet(),
err: err,
}
}
e.confirmCandidate()
Expand Down Expand Up @@ -253,6 +259,22 @@ func (e *Engine) scrollToTop() {
e.contentOffset = 0
}

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
}

func (e *Engine) scrollPageUp(height int) {
co := 0
if o := e.contentOffset - (height - DefaultY); o > 0 {
co = o
}
e.contentOffset = co
}

func (e *Engine) toggleKeymode() {
e.keymode = !e.keymode
}
Expand Down
37 changes: 37 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,43 @@ func TestScrollToBottomAndTop(t *testing.T) {
assert.Equal(0, e.contentOffset)
}

func TestScrollPageUpDown(t *testing.T) {
var assert = assert.New(t)
e := getEngine(`{"named":"go","NameTest":[1,2,3]}`, "")

cl := len(e.getContents())
// Then DefaultY = 1
e.scrollPageDown(cl, 3)
assert.Equal(2, e.contentOffset)
e.scrollPageDown(cl, 3)
assert.Equal(4, e.contentOffset)

e.scrollPageUp(3)
assert.Equal(2, e.contentOffset)

// term height changed
e.scrollPageDown(cl, 5)
assert.Equal(6, e.contentOffset)

e.scrollPageDown(cl, 5)
assert.Equal(7, e.contentOffset)

e.scrollPageDown(cl, 5)
assert.Equal(7, e.contentOffset)

e.scrollPageUp(5)
assert.Equal(3, e.contentOffset)

e.scrollPageUp(5)
assert.Equal(0, e.contentOffset)

e.scrollPageUp(5)
assert.Equal(0, e.contentOffset)

// term height > content size + default Y (a filter query line)
e.scrollPageDown(cl, 10)
assert.Equal(7, e.contentOffset)
}
func TestGetContents(t *testing.T) {
var assert = assert.New(t)

Expand Down