Skip to content

Commit

Permalink
cache: provide sorted (id, creation, edit) list of bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Sep 2, 2018
1 parent 90a45b4 commit 919f98e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 46 deletions.
4 changes: 2 additions & 2 deletions cache/bug_excerpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
* Sorting
*/

type BugsByCreationTime []*BugExcerpt
type BugsByCreationTime []BugExcerpt

func (b BugsByCreationTime) Len() int {
return len(b)
Expand Down Expand Up @@ -70,7 +70,7 @@ func (b BugsByCreationTime) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}

type BugsByEditTime []*BugExcerpt
type BugsByEditTime []BugExcerpt

func (b BugsByEditTime) Len() int {
return len(b)
Expand Down
55 changes: 53 additions & 2 deletions cache/repo_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -203,8 +204,58 @@ func (c *RepoCache) ResolveBugPrefix(prefix string) (*BugCache, error) {
return cached, nil
}

func (c *RepoCache) AllBugIds() ([]string, error) {
return bug.ListLocalIds(c.repo)
func (c *RepoCache) AllBugOrderById() []string {
result := make([]string, len(c.excerpts))

i := 0
for key := range c.excerpts {
result[i] = key
i++
}

sort.Strings(result)

return result
}

func (c *RepoCache) AllBugsOrderByEdit() []string {
excerpts := make([]BugExcerpt, len(c.excerpts))

i := 0
for _, val := range c.excerpts {
excerpts[i] = val
i++
}

sort.Sort(BugsByEditTime(excerpts))

result := make([]string, len(excerpts))

for i, val := range excerpts {
result[i] = val.Id
}

return result
}

func (c *RepoCache) AllBugsOrderByCreation() []string {
excerpts := make([]BugExcerpt, len(c.excerpts))

i := 0
for _, val := range c.excerpts {
excerpts[i] = val
i++
}

sort.Sort(BugsByCreationTime(excerpts))

result := make([]string, len(excerpts))

for i, val := range excerpts {
result[i] = val.Id
}

return result
}

// ClearAllBugs clear all bugs kept in memory
Expand Down
24 changes: 12 additions & 12 deletions commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ import (
"fmt"

"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util"
"github.com/spf13/cobra"
)

func runLsBug(cmd *cobra.Command, args []string) error {
//backend, err := cache.NewRepoCache(repo)
//if err != nil {
// return err
//}

// Todo: read bugs from backend
backend, err := cache.NewRepoCache(repo)
if err != nil {
return err
}

bugs := bug.ReadAllLocalBugs(repo)
allIds := backend.AllBugsOrderByCreation()

for b := range bugs {
if b.Err != nil {
return b.Err
for _, id := range allIds {
b, err := backend.ResolveBug(id)
if err != nil {
return err
}

snapshot := b.Bug.Compile()
snapshot := b.Snapshot()

var author bug.Person

Expand All @@ -37,7 +37,7 @@ func runLsBug(cmd *cobra.Command, args []string) error {
authorFmt := fmt.Sprintf("%-15.15s", author.Name)

fmt.Printf("%s %s\t%s\t%s\t%s\n",
util.Cyan(b.Bug.HumanId()),
util.Cyan(b.HumanId()),
util.Yellow(snapshot.Status),
titleFmt,
util.Magenta(authorFmt),
Expand Down
6 changes: 1 addition & 5 deletions graphql/resolvers/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ func (repoResolver) AllBugs(ctx context.Context, obj *models.Repository, after *
}

// Simply pass a []string with the ids to the pagination algorithm
source, err := obj.Repo.AllBugIds()

if err != nil {
return models.BugConnection{}, err
}
source := obj.Repo.AllBugOrderById()

// The edger create a custom edge holding just the id
edger := func(id string, offset int) connections.Edge {
Expand Down
36 changes: 11 additions & 25 deletions termui/bug_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,30 +212,25 @@ func (bt *bugTable) disable(g *gocui.Gui) error {
}

func (bt *bugTable) paginate(max int) error {
allIds, err := bt.repo.AllBugIds()
if err != nil {
return err
}
bt.allIds = bt.repo.AllBugsOrderByCreation()

bt.allIds = allIds

return bt.doPaginate(allIds, max)
return bt.doPaginate(max)
}

func (bt *bugTable) doPaginate(allIds []string, max int) error {
func (bt *bugTable) doPaginate(max int) error {
// clamp the cursor
bt.pageCursor = maxInt(bt.pageCursor, 0)
bt.pageCursor = minInt(bt.pageCursor, len(allIds))
bt.pageCursor = minInt(bt.pageCursor, len(bt.allIds))

nb := minInt(len(allIds)-bt.pageCursor, max)
nb := minInt(len(bt.allIds)-bt.pageCursor, max)

if nb < 0 {
bt.bugs = []*cache.BugCache{}
return nil
}

// slice the data
ids := allIds[bt.pageCursor : bt.pageCursor+nb]
ids := bt.allIds[bt.pageCursor : bt.pageCursor+nb]

bt.bugs = make([]*cache.BugCache, len(ids))

Expand Down Expand Up @@ -360,34 +355,25 @@ func (bt *bugTable) cursorClamp(v *gocui.View) error {
func (bt *bugTable) nextPage(g *gocui.Gui, v *gocui.View) error {
_, max := v.Size()

allIds, err := bt.repo.AllBugIds()
if err != nil {
return err
}

bt.allIds = allIds
bt.allIds = bt.repo.AllBugsOrderByCreation()

if bt.pageCursor+max >= len(allIds) {
if bt.pageCursor+max >= len(bt.allIds) {
return nil
}

bt.pageCursor += max

return bt.doPaginate(allIds, max)
return bt.doPaginate(max)
}

func (bt *bugTable) previousPage(g *gocui.Gui, v *gocui.View) error {
_, max := v.Size()
allIds, err := bt.repo.AllBugIds()
if err != nil {
return err
}

bt.allIds = allIds
bt.allIds = bt.repo.AllBugsOrderByCreation()

bt.pageCursor = maxInt(0, bt.pageCursor-max)

return bt.doPaginate(allIds, max)
return bt.doPaginate(max)
}

func (bt *bugTable) newBug(g *gocui.Gui, v *gocui.View) error {
Expand Down

0 comments on commit 919f98e

Please sign in to comment.