Show number of results

pull/6/head
Mickaël Menu 3 years ago
parent 4cd3df749b
commit f2c41f35fc
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -146,7 +146,7 @@ func (d *NoteDAO) exists(path string) (bool, error) {
return exists, nil
}
func (d *NoteDAO) Find(callback func(note.Match) error, filters ...note.Filter) error {
func (d *NoteDAO) Find(callback func(note.Match) error, filters ...note.Filter) (int, error) {
rows, err := func() (*sql.Rows, error) {
snippetCol := `""`
orderTerm := `n.title ASC`
@ -196,11 +196,14 @@ func (d *NoteDAO) Find(callback func(note.Match) error, filters ...note.Filter)
}()
if err != nil {
return err
return 0, err
}
defer rows.Close()
count := 0
for rows.Next() {
count++
var (
id, wordCount int
title, body, snippet string
@ -228,5 +231,5 @@ func (d *NoteDAO) Find(callback func(note.Match) error, filters ...note.Filter)
})
}
return nil
return count, nil
}

@ -362,11 +362,12 @@ func TestNoteDAOFindInMultiplePath(t *testing.T) {
func testNoteDAOFind(t *testing.T, expected []note.Match, filters ...note.Filter) {
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
actual := make([]note.Match, 0)
err := dao.Find(func(m note.Match) error {
count, err := dao.Find(func(m note.Match) error {
actual = append(actual, m)
return nil
}, filters...)
assert.Nil(t, err)
assert.Equal(t, count, len(expected))
assert.Equal(t, actual, expected)
})
}

@ -47,7 +47,7 @@ func (cmd *List) Run(container *Container) error {
filters = append(filters, note.MatchFilter(cmd.Match))
}
return note.List(
count, err := note.List(
note.ListOpts{
Format: opt.NewNotEmptyString(cmd.Format),
Filters: filters,
@ -59,6 +59,12 @@ func (cmd *List) Run(container *Container) error {
},
printNote,
)
if err == nil {
fmt.Printf("\nFound %d result(s)\n", count)
}
return err
})
}

@ -33,8 +33,9 @@ func (m Match) String() string {
}
// Finder retrieves notes matching the given Filter.
// Returns the number of matches.
type Finder interface {
Find(callback func(Match) error, filters ...Filter) error
Find(callback func(Match) error, filters ...Filter) (int, error)
}
type ListOpts struct {
@ -50,11 +51,11 @@ type ListDeps struct {
// List finds notes matching given criteria and formats them according to user
// preference.
func List(opts ListOpts, deps ListDeps, callback func(formattedNote string) error) error {
func List(opts ListOpts, deps ListDeps, callback func(formattedNote string) error) (int, error) {
templ := matchTemplate(opts.Format)
template, err := deps.Templates.Load(templ)
if err != nil {
return err
return 0, err
}
return deps.Finder.Find(func(note Match) error {

Loading…
Cancel
Save