Skip to content

Commit

Permalink
spaces that start with '.' in the name are now considered hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
bdazl committed Oct 27, 2024
1 parent c27d468 commit eb8b300
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func noteExport(cmd *cobra.Command, args []string) {
quitError("file format", err)
}

notes, err := collectNotes(spacesArg)
notes, err := selectNotes(spacesArg)
if err != nil {
quitError("collect notes", err)
}
Expand Down
15 changes: 3 additions & 12 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func noteList(cmd *cobra.Command, args []string) {
quitError("args", err)
}

notes, err := collectNotes(args)
notes, err := selectNotes(args)
if err != nil {
quitError("collect notes", err)
}

pprintNotes(notes, style, color)
}

func collectNotes(spaces []string) (db.Notes, error) {
func selectNotes(spaces []string) (db.Notes, error) {
sortOpts, pageOpts, err := listOpts()
if err != nil {
return nil, fmt.Errorf("args: %w", err)
Expand All @@ -79,16 +79,7 @@ func collectNotes(spaces []string) (db.Notes, error) {
d := dbOpen()
defer d.Close()

lsSpaces := spaces
if len(spaces) == 0 {
allSpaces, err := d.ListSpaces(nil)
if err != nil {
return nil, fmt.Errorf("ls spaces: %w", err)
}
lsSpaces = allSpaces
}

notes, err := d.ListNotes(lsSpaces, sortOpts, pageOpts)
notes, err := d.SelectNotes(spaces, allArg, sortOpts, pageOpts)
if err != nil {
return nil, fmt.Errorf("db list: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func noteRemove(cmd *cobra.Command, args []string) {
defer db.Close()

if len(ids) == 0 {
// allInSpaceArg is set and no args provided means find all notes in space
allNotesInSpace, err := db.ListNotes([]string{allInSpaceArg}, nil, nil)
// allInSpaceArg is set and no args provided means find all notes in specific space
allNotesInSpace, err := db.SelectNotes([]string{allInSpaceArg}, false, nil, nil)
if err != nil {
quitError("db list", err)
}
Expand Down
27 changes: 16 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ func init() {
globalFlags.StringVarP(&configPathArg, "config", "c", dfltConfig, "config file")
globalFlags.StringVar(&storagePathArg, "db", dfltStore, "database store containing your notes")

sortKeys := getSortKeys()
sortUsage := fmt.Sprintf("column to sort notes by (%v)", sortKeys)
selectFlagSet := pflag.NewFlagSet("select", pflag.ExitOnError)
selectFlagSet.BoolVarP(&allArg, "all", "a", false, "list notes from hidden spaces")
selectFlagSet.StringVarP(&sortByArg, "sort", "S", "id", sortUsage)
selectFlagSet.IntVarP(&limitArg, "limit", "l", 0, "limit amount of notes listed, 0 means no limit")
selectFlagSet.IntVarP(&offsetArg, "offset", "o", 0, "begin list notes at some offset (only if limit > 0)")
selectFlagSet.BoolVarP(&descendingArg, "descending", "d", false, "descending order")

rootFlags := rootCmd.Flags()
rootFlags.AddFlagSet(selectFlagSet)

initFlags := initCmd.Flags()
initFlags.BoolVar(&forceArg, "force", false, "determines if existing files will be overwritten")

Expand All @@ -361,24 +373,16 @@ func init() {
removeFlags.BoolVar(&noConfirmArg, "no-confirm", false, "skip confirmation dialog")
removeFlags.BoolVar(&permanentArg, "permanent", false, "note is completely removed from the db")

sortKeys := getSortKeys()
sortUsage := fmt.Sprintf("column to sort notes by (%v)", sortKeys)
collectFlagSet := pflag.NewFlagSet("collect", pflag.ExitOnError)
collectFlagSet.StringVarP(&sortByArg, "sort", "S", "id", sortUsage)
collectFlagSet.IntVarP(&limitArg, "limit", "l", 0, "limit amount of notes listed, 0 means no limit")
collectFlagSet.IntVarP(&offsetArg, "offset", "o", 0, "begin list notes at some offset (only if limit > 0)")
collectFlagSet.BoolVarP(&descendingArg, "descending", "d", false, "descending order")

printFlagSet := pflag.NewFlagSet("print", pflag.ExitOnError)
_ = printFlagSet.String("style", string(LightStyle), "output style (raw, light, full)")
_ = printFlagSet.String("color", "auto", "color option (auto, no|never, yes|always)")

listFlags := listCmd.Flags()
listFlags.AddFlagSet(collectFlagSet)
listFlags.AddFlagSet(selectFlagSet)
listFlags.AddFlagSet(printFlagSet)

tableFlags := tableCmd.Flags()
tableFlags.AddFlagSet(collectFlagSet)
tableFlags.AddFlagSet(selectFlagSet)
tableFlags.UintVarP(&previewArg, "preview", "p", 5, "preview word count to display in table")

getFlags := getCmd.Flags()
Expand All @@ -389,6 +393,7 @@ func init() {
idFlags.BoolVarP(&descendingArg, "descending", "d", false, "descending order")

spaceFlags := spaceCmd.Flags()
spaceFlags.BoolVarP(&allArg, "all", "a", false, "list hidden spaces")
spaceFlags.BoolVarP(&listArg, "list", "l", false, "separate each space with a newline")
spaceFlags.BoolVarP(&descendingArg, "descending", "d", false, "descending order")

Expand All @@ -403,7 +408,7 @@ func init() {
importFlags.BoolVar(&forceArg, "force-format", false, importForceUsage)

exportFlags := exportCmd.Flags()
exportFlags.AddFlagSet(collectFlagSet)
exportFlags.AddFlagSet(selectFlagSet)
exportFlags.AddFlagSet(inoutFlagSet)
exportFlags.StringSliceVarP(&spacesArg, "spaces", "s", []string{}, "limit export to notes from space(s)")
exportFlags.BoolVar(&forceArg, "force", false, "determines if existing file will be overwritten")
Expand Down
4 changes: 2 additions & 2 deletions cmd/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func noteSpace(cmd *cobra.Command, args []string) {
sort.Strings(spaces)
}
} else {
// Or list all spaces
lsSpaces, err := d.ListSpaces(sortOpts)
// Or list all (or at least some) spaces
lsSpaces, err := d.SelectSpaces(allArg, sortOpts)
if err != nil {
quitError("db list", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
)

func noteTable(cmd *cobra.Command, args []string) {
notes, err := collectNotes(args)
notes, err := selectNotes(args)
if err != nil {
quitError("collect notes", err)
}
Expand Down
18 changes: 14 additions & 4 deletions db/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (p *PageOpts) Check() error {
return nil
}

func (d *DB) ListNotes(spaces []string, sortOpts *SortOpts, pageOpts *PageOpts) (Notes, error) {
func (d *DB) SelectNotes(spaces []string, all bool, sortOpts *SortOpts, pageOpts *PageOpts) (Notes, error) {
var (
addParams = []any{}
sortQueryAdd = "ORDER BY pinned DESC" // By default we always sort pinned first
Expand Down Expand Up @@ -132,6 +132,10 @@ func (d *DB) ListNotes(spaces []string, sortOpts *SortOpts, pageOpts *PageOpts)
for _, space := range spaces {
addParams = append(addParams, space)
}
} else if !all {
// The caller has not specified individual spaces but also wants to hide notes
// from spaces starting with '.'
spaceQueryAdd = "WHERE space NOT LIKE '.%'"
}

// Prepare the SQL query
Expand Down Expand Up @@ -161,16 +165,22 @@ func (d *DB) ListNotes(spaces []string, sortOpts *SortOpts, pageOpts *PageOpts)
return notes, nil
}

func (d *DB) ListSpaces(sortOpts *SortOpts) ([]string, error) {
orderBy := ""
func (d *DB) SelectSpaces(all bool, sortOpts *SortOpts) ([]string, error) {
var (
orderBy = ""
where = ""
)
if !all {
where = "WHERE space NOT LIKE '.%'"
}
if sortOpts != nil {
orderBy = fmt.Sprintf(
"ORDER BY %v %v",
sortOpts.SortColumn, orderString(sortOpts.Ascending),
)
}

query := fmt.Sprintf("SELECT DISTINCT space FROM notes %v", orderBy)
query := fmt.Sprintf("SELECT DISTINCT space FROM notes %v %v", where, orderBy)
rows, err := d.db.Query(query)
if err != nil {
return nil, fmt.Errorf("query error: %w", err)
Expand Down

0 comments on commit eb8b300

Please sign in to comment.