Determine when to apply autoscroll

When shrinking the window, scrolling was limited to only the current
view buffer. This should implement auto-scrolling. It's a little bit
janky, but should be sufficient for now.
This commit is contained in:
Dave Gallant
2020-04-07 23:25:27 -04:00
parent 69139ee7f6
commit a13d212968
3 changed files with 55 additions and 61 deletions

View File

@@ -1,17 +1,15 @@
package controller
import (
"time"
config "github.com/davegallant/srv/config"
feeds "github.com/davegallant/srv/feeds"
)
// Controller keeps everything together
type Controller struct {
Config config.Configuration
lastUpdate time.Time
Rss *feeds.RSS
Config config.Configuration
Rss *feeds.RSS
CurrentFeed int
}
// Init initiates the controller with config
@@ -19,4 +17,5 @@ func (c *Controller) Init(conf string) {
c.Config = config.LoadConfiguration(conf)
c.Rss = &feeds.RSS{}
c.Rss.Update(c.Config.Feeds)
c.CurrentFeed = 0
}

View File

@@ -11,43 +11,19 @@ import (
"github.com/jroimartin/gocui"
)
// Controller can access Feeds and Config
// Controller can access internal state
var Controller *controller.Controller
var (
viewArr = []string{"feeds", "Items"}
active = 0
currentFeed = 0 // TODO: move to Controller
viewArr = []string{"feeds", "Items"}
active = 0
)
func cursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
v.SetCursor(cx, cy-1)
}
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
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 {
return err
}
}
}
return nil
}
// openFeed opens all items in the feed
func openFeed(g *gocui.Gui, v *gocui.View) error {
_, cy := v.Cursor()
currentFeed = cy
feed := Controller.Rss.Feeds[currentFeed]
_, oy := v.Origin()
feed := Controller.Rss.Feeds[oy]
Controller.CurrentFeed = oy
ov, _ := g.View("Items")
ov.Clear()
@@ -61,8 +37,8 @@ func openFeed(g *gocui.Gui, v *gocui.View) error {
// openItem opens the feed in an external browser
func openItem(g *gocui.Gui, v *gocui.View) error {
_, cy := v.Cursor()
item := Controller.Rss.Feeds[currentFeed].Items[cy]
_, oy := v.Origin()
item := Controller.Rss.Feeds[Controller.CurrentFeed].Items[oy]
err := exec.Command(
Controller.Config.ExternalViewer,
append(Controller.Config.ExternalViewerArgs, item.Link)...).Start()
@@ -74,30 +50,8 @@ func openItem(g *gocui.Gui, v *gocui.View) error {
return nil
}
func showLoading(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("loading", maxX/2-4, maxY/2-1, maxX/2+4, maxY/2+1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprintln(v, "Loading")
}
return nil
}
func hideLoading(g *gocui.Gui) error {
if err := g.DeleteView("loading"); err != nil {
if err != gocui.ErrUnknownView {
return err
}
}
return nil
}
func refreshFeeds(g *gocui.Gui, v *gocui.View) error {
showLoading(g)
Controller.Rss.Update(Controller.Config.Feeds)
//hideLoading(g)
return nil
}
@@ -137,10 +91,10 @@ func layout(g *gocui.Gui) error {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Feeds"
v.Highlight = true
v.SelBgColor = selectionBgColor
v.SelFgColor = selectionFgColor
v.Title = "Feeds"
if _, err = setCurrentViewOnTop(g, "feeds"); err != nil {
return err
@@ -153,10 +107,10 @@ func layout(g *gocui.Gui) error {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Items"
v.Highlight = true
v.SelBgColor = selectionBgColor
v.SelFgColor = selectionFgColor
v.Title = "Items"
}
return nil
}

41
cui/scroll.go Normal file
View File

@@ -0,0 +1,41 @@
package cui
import (
"strings"
"github.com/jroimartin/gocui"
)
func scroll(g *gocui.Gui, v *gocui.View, direction int) error {
if v != nil {
_, y := v.Size()
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 {
return err
}
}
// If we're nearing a boundary
if oy+direction > strings.Count(v.ViewBuffer(), "\n")+y-direction {
v.Autoscroll = true
} else {
v.Autoscroll = false
v.SetOrigin(ox, oy+direction)
return nil
}
}
return nil
}
func cursorDown(g *gocui.Gui, v *gocui.View) error {
return scroll(g, v, 1)
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
return scroll(g, v, -1)
}