-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
226 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright © 2024 Jacob Peyron <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/bdazl/note/db" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type finder struct { | ||
*regexp.Regexp | ||
Expression string | ||
Insensitive bool | ||
} | ||
|
||
func finderFromArgs(args []string) (*finder, error) { | ||
var ( | ||
expr = strings.Join(args, " ") | ||
err error | ||
regex *regexp.Regexp | ||
) | ||
|
||
if regexpArg && !posixArg { | ||
regex, err = regexp.Compile(expr) | ||
if err != nil { | ||
return nil, fmt.Errorf("compile: %w", err) | ||
} | ||
} else if posixArg { | ||
regex, err = regexp.CompilePOSIX(expr) | ||
if err != nil { | ||
return nil, fmt.Errorf("compile: %w", err) | ||
} | ||
} | ||
|
||
return &finder{ | ||
Regexp: regex, | ||
Expression: expr, | ||
Insensitive: insensitiveArg, | ||
}, nil | ||
} | ||
|
||
func (f *finder) Match(str string) bool { | ||
if f.Regexp != nil { | ||
return f.Regexp.MatchString(str) | ||
} else if !f.Insensitive { | ||
return strings.Contains(str, f.Expression) | ||
} else { | ||
// Make this more efficient | ||
lower := strings.ToLower(str) | ||
return strings.Contains(lower, f.Expression) | ||
} | ||
} | ||
|
||
func noteFind(cmd *cobra.Command, args []string) { | ||
finder, err := finderFromArgs(args) | ||
if err != nil { | ||
quitError("pattern", err) | ||
} | ||
|
||
style, color, err := styleColorOpts() | ||
if err != nil { | ||
quitError("args", err) | ||
} | ||
|
||
d := dbOpen() | ||
defer d.Close() | ||
|
||
notes := make(db.Notes, 0) | ||
for iter := range d.IterateNotes(nil, allArg || trashArg, nil) { | ||
if iter.Err != nil { | ||
quitError("db iterate", iter.Err) | ||
} | ||
|
||
if !trashArg && iter.Note.Space == TrashSpace { | ||
continue | ||
} | ||
|
||
if finder.Match(iter.Note.Content) { | ||
notes = append(notes, iter.Note) | ||
} | ||
} | ||
|
||
if idArg { | ||
ids := notes.GetIDs() | ||
printIds(ids, listArg) | ||
} else { | ||
pprintNotes(notes, style, color) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright © 2024 Jacob Peyron <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
package db | ||
|
||
import "fmt" | ||
|
||
type NoteIterator struct { | ||
Note | ||
Err error | ||
} | ||
|
||
func (d *DB) IterateNotes(spaces []string, all bool, sortOpts *SortOpts) <-chan NoteIterator { | ||
ch := make(chan NoteIterator) | ||
|
||
go func() { | ||
defer close(ch) | ||
|
||
page := 0 | ||
pageOpts := &PageOpts{ | ||
Limit: 10, | ||
} | ||
|
||
for { | ||
notes, err := d.SelectNotes(spaces, all, sortOpts, pageOpts) | ||
if err != nil { | ||
fmt.Printf("Error: %v", err.Error()) | ||
ch <- NoteIterator{Err: err} | ||
return | ||
} | ||
|
||
if len(notes) == 0 { | ||
return | ||
} | ||
|
||
for _, note := range notes { | ||
ch <- NoteIterator{Note: note} | ||
} | ||
|
||
page += 1 | ||
pageOpts.Offset = pageOpts.Limit * page | ||
} | ||
}() | ||
|
||
return ch | ||
} |