diff --git a/cmd/export.go b/cmd/export.go index 23ea60c..d29eaf4 100644 --- a/cmd/export.go +++ b/cmd/export.go @@ -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) } diff --git a/cmd/list.go b/cmd/list.go index aa576f1..4fc0fbe 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -62,7 +62,7 @@ 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) } @@ -70,7 +70,7 @@ func noteList(cmd *cobra.Command, args []string) { 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) @@ -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) } diff --git a/cmd/remove.go b/cmd/remove.go index d555006..4e2384f 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -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) } diff --git a/cmd/root.go b/cmd/root.go index 64dfc95..776123c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") @@ -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() @@ -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") @@ -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") diff --git a/cmd/space.go b/cmd/space.go index 91ed4d8..afa7076 100644 --- a/cmd/space.go +++ b/cmd/space.go @@ -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) } diff --git a/cmd/table.go b/cmd/table.go index 9b09d95..0a56b67 100644 --- a/cmd/table.go +++ b/cmd/table.go @@ -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) } diff --git a/db/list.go b/db/list.go index 11bbd36..e08c0ff 100644 --- a/db/list.go +++ b/db/list.go @@ -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 @@ -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 @@ -161,8 +165,14 @@ 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", @@ -170,7 +180,7 @@ func (d *DB) ListSpaces(sortOpts *SortOpts) ([]string, error) { ) } - 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)