~chrisppy/beagles

93f1e545d9dfe625cc93580db931bec44d49b2ba — Chris Palmer 12 days ago 879cae1 main
cleanup list
6 files changed, 54 insertions(+), 154 deletions(-)

M go.mod
M go.sum
M ui/application.go
M ui/list.go
M ui/navigation.go
M ui/ui.go
M go.mod => go.mod +1 -1
@@ 13,7 13,7 @@ require (
	github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
	github.com/stretchr/testify v1.4.0 // indirect
	gitlab.com/tslocum/cbind v0.1.2
	gitlab.com/tslocum/cview v1.5.0
	gitlab.com/tslocum/cview v1.5.1-0.20201009235145-c33ce9563344
	go.etcd.io/bbolt v1.3.5
	golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect
	gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect

M go.sum => go.sum +2 -2
@@ 59,8 59,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/urfave/cli v1.22.3/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
gitlab.com/tslocum/cbind v0.1.2 h1:ptDjO7WeOl1HglprsK18L8I9JeRkmtuBoBBaYw/6/Ow=
gitlab.com/tslocum/cbind v0.1.2/go.mod h1:HfB7qAhHSZbn1rFK8M9SvSN5NG6ScAg/3h3iE6xdeeI=
gitlab.com/tslocum/cview v1.5.0 h1:oT4/1IIxxxQpswt1neIKC4WKuDSfKfmHmiCgnsdLya4=
gitlab.com/tslocum/cview v1.5.0/go.mod h1:i9NyxtwBtkiVFrwmsh3Bv3dunvipjZrKX0TTdPHbzcw=
gitlab.com/tslocum/cview v1.5.1-0.20201009235145-c33ce9563344 h1:5lIlOHV7tDcasoWAuBs8GVYmDFmnDem/PMYuJHs/TUQ=
gitlab.com/tslocum/cview v1.5.1-0.20201009235145-c33ce9563344/go.mod h1:i9NyxtwBtkiVFrwmsh3Bv3dunvipjZrKX0TTdPHbzcw=
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

M ui/application.go => ui/application.go +2 -1
@@ 41,7 41,8 @@ func (w *app) exit() {
}

func (w *app) run(root tui.Primitive, fullscreen bool) error {
	return w.Widget.SetRoot(root, true).Run()
	w.Widget.SetRoot(root, true)
	return w.Widget.Run()
}

func (w *app) switchTo(widget tui.Primitive) {

M ui/list.go => ui/list.go +24 -53
@@ 41,55 41,42 @@ func newList(config *config.Config) *list {
	return &list{Widget: view}
}

func (w *list) increment() string {
	w.Widget.Transform(tui.TransformNextItem)
func (w *list) onChange(handler func(index int, item *tui.ListItem)) {
	w.Widget.SetChangedFunc(handler)
}

	return w.getKey()
func (w *list) increment() {
	w.Widget.Transform(tui.TransformNextItem)
}

func (w *list) decrement() string {
func (w *list) decrement() {
	w.Widget.Transform(tui.TransformPreviousItem)

	return w.getKey()
}

func (w *list) pageUp() string {
func (w *list) pageUp() {
	w.Widget.Transform(tui.TransformPreviousPage)

	return w.getKey()
}

func (w *list) pageDown() string {
func (w *list) pageDown() {
	w.Widget.Transform(tui.TransformNextPage)

	return w.getKey()
}

func (w *list) getTitle(index int) (string, string) {
	if w.size() == 0 {
		return "", ""
func (w *list) ref(item *tui.ListItem) string {
	if item == nil {
		return ""
	}
	return w.Widget.GetItemText(index)

	return item.GetSecondaryText()
}

func (w *list) getKey() string {
	_, key := w.getTitle(w.index())

	return key
	return w.ref(w.Widget.GetCurrentItem())
}

func (w *list) setIndex(newIndex int) {
	w.Widget.SetCurrentItem(newIndex)
}

func (w *list) setIndexByKey(item *db.Item) {
	if item == nil {
		return
	}

	w.setIndex(w.find(item))
}

func (w *list) index() int {
	return w.Widget.GetCurrentItemIndex()
}


@@ 145,13 132,11 @@ func (w *list) clear() {
	w.Widget.Clear()
}

func (w *list) refresh(queue db.Queue, items db.Items) *db.Item {
func (w *list) refresh(queue db.Queue, items db.Items) {
	if len(queue) == 0 {
		return nil
		return
	}

	item := items[w.getKey()]

	w.clear()

	i := 0


@@ 162,30 147,20 @@ func (w *list) refresh(queue db.Queue, items db.Items) *db.Item {
	}
	sort.Slice(qitems, func(i, j int) bool { return qitems[i].Compare(qitems[j]) })

	var it *db.Item
	for x, item := range qitems {
		if x == 0 {
			it = item
		}
	for _, item := range qitems {
		w.add(item)
	}

	w.setIndexByKey(item)

	if item != nil {
		return item
	}

	return it
	w.setIndex(0)
}

func (w *list) refreshFav(fav db.Favorites, items db.Items) *db.Item {
	w.clear()

func (w *list) refreshFav(fav db.Favorites, items db.Items) {
	if len(fav) == 0 {
		return nil
		return
	}

	w.clear()

	i := 0
	fitems := make([]*db.Item, len(fav))
	for key := range fav {


@@ 194,13 169,9 @@ func (w *list) refreshFav(fav db.Favorites, items db.Items) *db.Item {
	}
	sort.Slice(fitems, func(i, j int) bool { return fitems[i].Compare(fitems[j]) })

	var it *db.Item
	for x, item := range fitems {
		if x == 0 {
			it = item
		}
	for _, item := range fitems {
		w.add(item)
	}

	return it
	w.setIndex(0)
}

M ui/navigation.go => ui/navigation.go +8 -34
@@ 70,20 70,14 @@ func (i *UI) moveUp(event *tcell.EventKey) *tcell.EventKey {
			return nil
		}

		key := i.list.decrement()
		if key != "" {
			i.content.setText(i.DB.Items[key])
		}
		i.list.decrement()
		return nil
	case favStatus:
		if i.favList.size() == 0 {
			return nil
		}

		key := i.favList.decrement()
		if key != "" {
			i.favContent.setText(i.DB.Items[key])
		}
		i.favList.decrement()
		return nil
	case subStatus:
		if i.subTree.size() == 0 {


@@ 117,22 111,14 @@ func (i *UI) moveDown(event *tcell.EventKey) *tcell.EventKey {
			return nil
		}

		key := i.list.increment()
		if key != "" {
			i.content.setText(i.DB.Items[key])
		}

		i.list.increment()
		return nil
	case favStatus:
		if i.favList.size() == 0 {
			return nil
		}

		key := i.favList.increment()
		if key != "" {
			i.favContent.setText(i.DB.Items[key])
		}

		i.favList.increment()
		return nil
	case subStatus:
		if i.subTree.size() == 0 {


@@ 166,20 152,14 @@ func (i *UI) movePageUp(event *tcell.EventKey) *tcell.EventKey {
			return nil
		}

		key := i.list.pageUp()
		if key != "" {
			i.content.setText(i.DB.Items[key])
		}
		i.list.pageUp()
		return nil
	case favStatus:
		if i.favList.size() == 0 {
			return nil
		}

		key := i.favList.pageUp()
		if key != "" {
			i.favContent.setText(i.DB.Items[key])
		}
		i.favList.pageUp()
		return nil
	case subStatus:
		if i.subTree.size() == 0 {


@@ 213,20 193,14 @@ func (i *UI) movePageDown(event *tcell.EventKey) *tcell.EventKey {
			return nil
		}

		key := i.list.pageDown()
		if key != "" {
			i.content.setText(i.DB.Items[key])
		}
		i.list.pageDown()
		return nil
	case favStatus:
		if i.favList.size() == 0 {
			return nil
		}

		key := i.favList.pageDown()
		if key != "" {
			i.favContent.setText(i.DB.Items[key])
		}
		i.favList.pageDown()
		return nil
	case subStatus:
		if i.subTree.size() == 0 {

M ui/ui.go => ui/ui.go +17 -63
@@ 353,6 353,18 @@ func (i *UI) Init() {

	i.app.processInput(c.Capture)

	i.list.onChange(func(_ int, item *tui.ListItem) {
		if it, ok := i.DB.Items[i.list.ref(item)]; ok {
			i.content.setText(it)
		}
	})

	i.favList.onChange(func(_ int, item *tui.ListItem) {
		if it, ok := i.DB.Items[i.list.ref(item)]; ok {
			i.favContent.setText(it)
		}
	})

	i.commandLine.onChange(func(text string) {
		if text == "" {
			i.setStatus(i.status)


@@ 400,14 412,8 @@ func (i *UI) Init() {

// Run will execute the application
func (i *UI) Run() error {
	if item := i.list.refresh(i.DB.Queue, i.DB.Items); item != nil {
		i.content.setText(item)
	}

	if item := i.favList.refreshFav(i.DB.Favorites, i.DB.Items); item != nil {
		i.favContent.setText(item)
	}

	i.list.refresh(i.DB.Queue, i.DB.Items)
	i.favList.refreshFav(i.DB.Favorites, i.DB.Items)
	i.subTree.refresh(i.DB.Feeds, i.DB.Items, false)

	return i.app.run(i.pages, true)


@@ 417,7 423,6 @@ func (i *UI) updateFeed(args []string) {
	nitems, err := i.DB.Update()
	if err != nil {
		i.Logger.Errorln(err.Error())
		return
	}

	if i.Config.Podcast.AutoDownload && len(nitems) != 0 {


@@ 429,13 434,7 @@ func (i *UI) updateFeed(args []string) {
	}

	i.app.draw(func() {
		i.content.clear()

		item := i.list.refresh(i.DB.Queue, i.DB.Items)
		if item != nil {
			i.content.setText(item)
		}

		i.list.refresh(i.DB.Queue, i.DB.Items)
		i.subTree.refresh(i.DB.Feeds, i.DB.Items, i.hideRead)

		i.commandLine.setText("")


@@ 455,13 454,8 @@ func (i *UI) addFeed(args []string) {
			i.setStatus(i.status)
			return
		}
		i.content.clear()

		item := i.list.refresh(i.DB.Queue, i.DB.Items)
		if item != nil {
			i.content.setText(item)
		}

		i.list.refresh(i.DB.Queue, i.DB.Items)
		i.subTree.refresh(i.DB.Feeds, i.DB.Items, i.hideRead)

		i.commandLine.setText("")


@@ 498,13 492,8 @@ func (i *UI) deleteFeed(args []string) {
			i.setStatus(i.status)
			return
		}
		i.content.clear()

		item := i.list.refresh(i.DB.Queue, i.DB.Items)
		if item != nil {
			i.content.setText(item)
		}

		i.list.refresh(i.DB.Queue, i.DB.Items)
		i.subTree.refresh(i.DB.Feeds, i.DB.Items, i.hideRead)

		i.commandLine.setText("")


@@ 660,24 649,7 @@ func (i *UI) markRead(event *tcell.EventKey) *tcell.EventKey {
	case listStatus, contentStatus:
		key = i.list.getKey()
		i.app.draw(func() {
			index := i.list.index()
			i.list.remove()

			size := i.list.size()
			if size == 0 {
				i.content.setText(nil)
			} else {
				switch index {
				case size:
					index--
					i.list.setIndex(index)
				case 0:
					i.list.setIndex(0)
				default:
					i.list.setIndex(index)
				}
				i.content.setText(i.DB.Items[i.list.getKey()])
			}
		})
		treeRefresh = true
	case subStatus, subContentStatus:


@@ 756,25 728,7 @@ func (i *UI) markUnfavorite(event *tcell.EventKey) *tcell.EventKey {
	case favStatus, favContentStatus:
		key = i.list.getKey()
		i.app.draw(func() {
			index := i.favList.index()
			i.favList.remove()

			size := i.favList.size()
			if size == 0 {
				i.favContent.setText(nil)
			} else {
				switch index {
				case size:
					index--
					i.favList.setIndex(index)
				case 0:
					i.favList.setIndex(0)
				default:
					i.favList.setIndex(index)
				}
				i.favContent.setText(i.DB.Items[i.favList.getKey()])
			}

		})
	default:
		return event