~whereswaldon/rosebud

fd484cf501b851b43cf4c954fd526c6a847f03da — Chris Waldon 1 year, 2 months ago c9e3fd4
cmd/rosebud: show newest transactions first

This commit starts sorting transactions by their date to ensure the
latest transactions appear at the top of the list.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 19 insertions(+), 4 deletions(-)

M cmd/rosebud/main.go
M cmd/rosebud/main.go => cmd/rosebud/main.go +19 -4
@@ 7,6 7,7 @@ import (
	"log"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"



@@ 87,8 88,11 @@ type UI struct {
	Explorer *explorer.Explorer
	Conn     bus.Connection

	Theme         *apptheme.Theme
	Txs           []*ledger.Transaction
	Theme *apptheme.Theme
	Txs   []*ledger.Transaction
	// Filtered is a slice of indices into Txs that have been filtered and
	// sorted by user interactions.
	Filtered      []int
	Balances      []*ledger.Account
	OutFilePath   string
	CacheFilePath string


@@ 150,6 154,7 @@ func (ui *UI) Layout(gtx C) D {
func (ui *UI) requestFiles() {
	type LoadInfo struct {
		Txs           []*ledger.Transaction
		Filtered      []int
		Balances      []*ledger.Account
		Tree          *AccountTreeNode
		OutFilePath   string


@@ 199,10 204,19 @@ func (ui *UI) requestFiles() {
			if err != nil {
				return LoadInfo{}, fmt.Errorf("failed parsing input file: %w", err)
			}
			filtered := make([]int, len(transactions))
			for i := range filtered {
				filtered[i] = i
			}
			// sort the elements of filtered by the dates of the transactions they refer to.
			sort.Slice(filtered, func(i, j int) bool {
				return transactions[filtered[i]].Date.After(transactions[filtered[j]].Date)
			})
			balances := ledger.GetBalances(transactions, nil)
			tree := BuildAccountTree(balances)
			return LoadInfo{
				Txs:           transactions,
				Filtered:      filtered,
				Balances:      balances,
				Tree:          tree,
				OutFilePath:   outFilePath,


@@ 214,6 228,7 @@ func (ui *UI) requestFiles() {
				log.Printf("failed loading: %v", err)
			} else {
				ui.Txs = li.Txs
				ui.Filtered = li.Filtered
				ui.Tree = li.Tree
				ui.Balances = li.Balances
				ui.OutFilePath = li.OutFilePath


@@ 334,8 349,8 @@ func (u *UI) layoutTransactions(gtx C) D {
			return apptheme.TxForm(u.Theme, &u.TxEditor).Layout(gtx)
		}),
		layout.Flexed(1, func(gtx C) D {
			return list.Layout(gtx, len(u.Txs), func(gtx C, index int) D {
				tx := u.Txs[index]
			return list.Layout(gtx, len(u.Filtered), func(gtx C, index int) D {
				tx := u.Txs[u.Filtered[index]]
				return theme.Transaction(u.Theme.Th, tx).Layout(gtx)
			})
		}),