Skip to content

Commit

Permalink
Add vim-key navigation to the hexdumper widget
Browse files Browse the repository at this point in the history
This is a hand-coded widget that does not rely on standard composite
widgets. That was done because constructing the hexdumper widget using
dozens of piles and columns for capturing each hex byte resulted in a
sluggish experience on slow machines like the raspberry pi. So, I need
to manually add vim-key navigation.
  • Loading branch information
gcla committed Aug 1, 2020
1 parent 680f0fd commit 50033b6
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions widgets/hexdumper2/hexdumper2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/gcla/gowid"
"github.com/gcla/gowid/gwutil"
"github.com/gcla/gowid/vim"
"github.com/gcla/gowid/widgets/list"
"github.com/gcla/gowid/widgets/styled"
"github.com/gcla/termshark/v2/format"
Expand All @@ -41,6 +42,10 @@ type Options struct {
LineNumUnselected string
LineNumSelected string
PaletteIfCopying string
DownKeys []vim.KeyPress
UpKeys []vim.KeyPress
LeftKeys []vim.KeyPress
RightKeys []vim.KeyPress
}

type Widget struct {
Expand All @@ -53,6 +58,7 @@ type Widget struct {
lineNumSelected string
offset int // scroll position, bit of a hack
paletteIfCopying string
opt Options
gowid.AddressProvidesID
styled.UsePaletteIfSelectedForCopy
Callbacks *gowid.Callbacks
Expand All @@ -72,6 +78,19 @@ func New(data []byte, opts ...Options) *Widget {
opt = opts[0]
}

if opt.DownKeys == nil {
opt.DownKeys = vim.AllDownKeys
}
if opt.UpKeys == nil {
opt.UpKeys = vim.AllUpKeys
}
if opt.LeftKeys == nil {
opt.LeftKeys = vim.AllLeftKeys
}
if opt.RightKeys == nil {
opt.RightKeys = vim.AllRightKeys
}

res := &Widget{
data: data,
layers: opt.StyledLayers,
Expand All @@ -81,6 +100,7 @@ func New(data []byte, opts ...Options) *Widget {
lineNumSelected: opt.LineNumSelected,
paletteIfCopying: opt.PaletteIfCopying,
UsePaletteIfSelectedForCopy: styled.UsePaletteIfSelectedForCopy{Entry: opt.PaletteIfCopying},
opt: opt,
Callbacks: gowid.NewCallbacks(),
}

Expand Down Expand Up @@ -459,23 +479,23 @@ func (w *Widget) realUserInput(ev interface{}, size gowid.IRenderSize, focus gow
switch ev := ev.(type) {
case *tcell.EventKey:

switch ev.Key() {
case tcell.KeyRight, tcell.KeyCtrlF:
switch {
case vim.KeyIn(ev, w.opt.RightKeys):
//res = Scroll(w, 1, w.Wrap(), app)
pos := w.Position()
if pos < len(w.data) {
w.SetPosition(pos+1, app)
res = true
}
case tcell.KeyLeft, tcell.KeyCtrlB:
case vim.KeyIn(ev, w.opt.LeftKeys):
pos := w.Position()
if pos > 0 {
w.SetPosition(pos-1, app)
res = true
}
case tcell.KeyDown, tcell.KeyCtrlN:
case vim.KeyIn(ev, w.opt.DownKeys):
scrollDown = true
case tcell.KeyUp, tcell.KeyCtrlP:
case vim.KeyIn(ev, w.opt.UpKeys):
scrollUp = true
}

Expand Down

0 comments on commit 50033b6

Please sign in to comment.