Skip to content

Commit

Permalink
Merge pull request #46 from Alkorin/scroll
Browse files Browse the repository at this point in the history
Add PageUp & PageDown to scroll messages
  • Loading branch information
Alkorin authored Feb 6, 2019
2 parents 76abccf + 73238af commit b79877f
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (gui *GoCUI) NewSpaceHandler(s *Space) {
gui.updateSpaceList()
if len(gui.spacesList) == 1 {
gui.updateMessages()
gui.updateSpaceStatus()
}
}

Expand Down Expand Up @@ -122,6 +123,12 @@ func (gui *GoCUI) keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("cmd", gocui.KeyCtrlH, gocui.ModNone, gui.showHelp); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyPgup, gocui.ModNone, gui.msgUp); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyPgdn, gocui.ModNone, gui.msgDown); err != nil {
return err
}
return nil
}

Expand All @@ -135,6 +142,33 @@ func (gui *GoCUI) previousSpace(g *gocui.Gui, v *gocui.View) error {
return nil
}

func (gui *GoCUI) msgUp(g *gocui.Gui, v *gocui.View) error {
return gui.msgScroll(-5)
}

func (gui *GoCUI) msgDown(g *gocui.Gui, v *gocui.View) error {
return gui.msgScroll(5)
}

func (gui *GoCUI) msgScroll(delta int) error {
v, _ := gui.View("messages")

// Current position
_, viewHeight := v.Size()
ox, oy := v.Origin()

if viewHeight+oy+delta > len(v.ViewBufferLines())-1 {
// We are at the bottom, enable Autoscroll
v.Autoscroll = true
} else {
// Set autoscroll to false and scroll.
v.Autoscroll = false
v.SetOrigin(ox, oy+delta)
}
gui.updateSpaceStatus()
return nil
}

func (gui *GoCUI) hideHelp(g *gocui.Gui, v *gocui.View) error {
g.SetViewOnTop("messages")
g.SetCurrentView("cmd")
Expand All @@ -161,13 +195,19 @@ func (gui *GoCUI) moveToSpace(i int) {
}

gui.currentSpaceIndex = i

// Reset flags
gui.spacesList[i].NewMessage = false
gui.spacesList[i].Highlight = false

// Force go to bottom for messages
v, _ := gui.View("messages")
v.Autoscroll = true

// Refresh
gui.updateMessages()
gui.updateSpaceStatus()
gui.updateSpaceList()
gui.updateMessages()
}

func (gui *GoCUI) sendMessage(g *gocui.Gui, v *gocui.View) error {
Expand Down Expand Up @@ -280,15 +320,23 @@ func (gui *GoCUI) updateSpaceStatus() {
gui.spacesMutex.RLock()
defer gui.spacesMutex.RUnlock()

v, err := g.View("spaceStatus")
if err != nil {
return err
}
v, _ := g.View("spaceStatus")
v.Clear()

space := gui.spacesList[gui.currentSpaceIndex]

fmt.Fprintf(v, "[%s]", space.DisplayName())

msgView, _ := g.View("messages")
if msgView.Autoscroll == false {
_, viewHeight := msgView.Size()
_, oy := msgView.Origin()
nbLines := len(msgView.ViewBufferLines())

moreLines := nbLines - viewHeight - oy
fmt.Fprintf(v, " -MORE(%d)-", moreLines)
}

return nil
})
}
Expand Down

0 comments on commit b79877f

Please sign in to comment.