diff options
author | Alex Goodman <wagoodman@gmail.com> | 2019-02-17 13:25:14 -0500 |
---|---|---|
committer | Alex Goodman <wagoodman@gmail.com> | 2019-02-17 13:25:14 -0500 |
commit | 184d3a2b0c82d149f17e17c6494d3a4e4a1f93c8 (patch) | |
tree | bc01cb46f3b7ec2a456e582f5ab3cb7cae9fdb68 /ui | |
parent | eb31a6290efa47dbf812654bdbcfd0e3619c99f8 (diff) |
added layerview pgup/pgdown
Diffstat (limited to 'ui')
-rw-r--r-- | ui/layerview.go | 69 | ||||
-rw-r--r-- | ui/ui.go | 30 |
2 files changed, 83 insertions, 16 deletions
diff --git a/ui/layerview.go b/ui/layerview.go index 98dce2e..0179e06 100644 --- a/ui/layerview.go +++ b/ui/layerview.go @@ -28,6 +28,8 @@ type LayerView struct { keybindingCompareAll []keybinding.Key keybindingCompareLayer []keybinding.Key + keybindingPageDown []keybinding.Key + keybindingPageUp []keybinding.Key } // NewDetailsView creates a new view object attached the the global [gocui] screen object. @@ -59,6 +61,16 @@ func NewLayerView(name string, gui *gocui.Gui, layers []image.Layer) (layerView log.Panicln(err) } + layerView.keybindingPageUp, err = keybinding.ParseAll(viper.GetString("keybinding.page-up")) + if err != nil { + log.Panicln(err) + } + + layerView.keybindingPageDown, err = keybinding.ParseAll(viper.GetString("keybinding.page-down")) + if err != nil { + log.Panicln(err) + } + return layerView } @@ -84,6 +96,17 @@ func (view *LayerView) Setup(v *gocui.View, header *gocui.View) error { return err } + for _, key := range view.keybindingPageUp { + if err := view.gui.SetKeybinding(view.Name, key.Value, key.Modifier, func(*gocui.Gui, *gocui.View) error { return view.PageUp() }); err != nil { + return err + } + } + for _, key := range view.keybindingPageDown { + if err := view.gui.SetKeybinding(view.Name, key.Value, key.Modifier, func(*gocui.Gui, *gocui.View) error { return view.PageDown() }); err != nil { + return err + } + } + for _, key := range view.keybindingCompareLayer { if err := view.gui.SetKeybinding(view.Name, key.Value, key.Modifier, func(*gocui.Gui, *gocui.View) error { return view.setCompareMode(CompareLayer) }); err != nil { return err @@ -99,6 +122,13 @@ func (view *LayerView) Setup(v *gocui.View, header *gocui.View) error { return view.Render() } +// height obtains the height of the current pane (taking into account the lost space due to the header). +func (view *LayerView) height() uint { + _, height := view.view.Size() + return uint(height - 1) +} + + // IsVisible indicates if the layer view pane is currently initialized. func (view *LayerView) IsVisible() bool { if view == nil { @@ -107,6 +137,45 @@ func (view *LayerView) IsVisible() bool { return true } +// PageDown moves to next page putting the cursor on top +func (view *LayerView) PageDown() error { + step := int(view.height())+1 + targetLayerIndex := view.LayerIndex + step + + if targetLayerIndex > len(view.Layers) { + step -= targetLayerIndex - (len(view.Layers) -1) + targetLayerIndex = view.LayerIndex + step + } + + if step > 0 { + err := CursorStep(view.gui, view.view, step) + if err == nil { + view.SetCursor(view.LayerIndex + step) + } + } + return nil +} + +// PageUp moves to previous page putting the cursor on top +func (view *LayerView) PageUp() error { + step := int(view.height())+1 + targetLayerIndex := view.LayerIndex - step + + if targetLayerIndex < 0 { + step += targetLayerIndex + targetLayerIndex = view.LayerIndex - step + } + + if step > 0 { + err := CursorStep(view.gui, view.view, -step) + if err == nil { + view.SetCursor(view.LayerIndex - step) + } + } + return nil +} + + // CursorDown moves the cursor down in the layer pane (selecting a higher layer). func (view *LayerView) CursorDown() error { if view.LayerIndex < len(view.Layers) { @@ -110,31 +110,29 @@ func toggleFilterView(g *gocui.Gui, v *gocui.View) error { // CursorDown moves the cursor down in the currently selected gocui pane, scrolling the screen as needed. func CursorDown(g *gocui.Gui, v *gocui.View) error { + return CursorStep(g, v,1) +} + +// CursorUp moves the cursor up in the currently selected gocui pane, scrolling the screen as needed. +func CursorUp(g *gocui.Gui, v *gocui.View) error { + return CursorStep(g, v,-1) +} + +// Moves the cursor the given step distance, setting the origin to the new cursor line +func CursorStep(g *gocui.Gui, v *gocui.View, step int) error { cx, cy := v.Cursor() // if there isn't a next line - line, err := v.Line(cy + 1) + line, err := v.Line(cy+step) if err != nil { // todo: handle error } if len(line) == 0 { - return errors.New("unable to move cursor down, empty line") + return errors.New("unable to move the cursor, empty line") } - if err := v.SetCursor(cx, cy+1); err != nil { + if err := v.SetCursor(cx, cy+step); err != nil { ox, oy := v.Origin() - if err := v.SetOrigin(ox, oy+1); err != nil { - return err - } - } - return nil -} - -// CursorUp moves the cursor up in the currently selected gocui pane, scrolling the screen as needed. -func CursorUp(g *gocui.Gui, v *gocui.View) error { - ox, oy := v.Origin() - cx, cy := v.Cursor() - if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { - if err := v.SetOrigin(ox, oy-1); err != nil { + if err := v.SetOrigin(ox, oy+step); err != nil { return err } } |