diff --git a/main.go b/main.go index fcd36ab..84123d1 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,54 @@ import ( "fmt" "os" "runtime" + "strings" "github.com/nilock/tuido/tui" + "github.com/nilock/tuido/tuido" "github.com/nilock/tuido/utils" ) func main() { + if len(os.Args) > 1 { + switch os.Args[1] { + case "list": + listCmd := flag.NewFlagSet("list", flag.ExitOnError) + maxItems := listCmd.Int("max", 0, "maximum number of items to display") + inclSnoozed := listCmd.Bool("z", false, "include snoozed items") + listCmd.BoolVar(inclSnoozed, "zzz", false, "include snoozed items") + inclAll := listCmd.Bool("a", false, "include snoozed and completed/cancelled items") + listCmd.BoolVar(inclAll, "all", false, "include snoozed and completed/cancelled items") + listCmd.Parse(os.Args[2:]) + + path := "." + if listCmd.NArg() > 0 { + path = listCmd.Arg(0) + } + + runList(path, *maxItems, *inclSnoozed || *inclAll, *inclAll) + return + + case "create", "add": + text := strings.Join(os.Args[2:], " ") + if text == "" { + fmt.Fprintln(os.Stderr, "Usage: tuido create ") + os.Exit(1) + } + runCreate(text) + return + + case "version", "-version", "--version": + showVersionInfo() + return + + case "help", "-help", "--help", "-h": + printHelp() + return + } + } + var showVersion = flag.Bool("version", false, "show version and platform information") + flag.Usage = printHelp flag.Parse() if *showVersion { @@ -22,6 +63,68 @@ func main() { tui.Run() } +func runList(path string, max int, inclSnoozed bool, inclDone bool) { + files := tui.GetFiles(path, tui.GetConfigExtensions()) + all := []*tuido.Item{} + for _, f := range files { + all = append(all, tui.GetItems(f)...) + } + + tui.SortItems(all) + + filtered := all[:0] + for _, item := range all { + s := item.Satus() + if !inclDone && (s == tuido.Checked || s == tuido.Obsolete) { + continue + } + if !inclSnoozed && !item.Active() { + continue + } + filtered = append(filtered, item) + } + + count := len(filtered) + if max > 0 && max < count { + count = max + } + + for i := 0; i < count; i++ { + fmt.Printf("%s\n", filtered[i].String()) + } +} + +func runCreate(text string) { + writeTo := tui.GetConfigWriteTo() + item := tuido.New(writeTo, -1, "") + err := item.SetText(text) + if err != nil { + fmt.Printf("Error creating item: %v\n", err) + os.Exit(1) + } + fmt.Printf("Created: %s\n", item.String()) +} + +func printHelp() { + fmt.Print(`Usage: tuido [command] [options] + +Without a command, opens the interactive TUI. + +Commands: + list [options] [path] List open/in-progress items (default path: .) + -z, --zzz Include snoozed items + -a, --all Include snoozed and completed/cancelled items + --max N Limit output to N items + create Create a new todo item + add Alias for create + version Show version and platform information + help Show this help + +Flags: + -version Show version and platform information +`) +} + func showVersionInfo() { version := utils.Version() goos := runtime.GOOS diff --git a/tui/config.go b/tui/config.go index cccb28f..cbb2e14 100644 --- a/tui/config.go +++ b/tui/config.go @@ -105,3 +105,11 @@ func parseConfig(file *os.File) config { return cfg } + +func GetConfigExtensions() []string { + return runConfig.extensions +} + +func GetConfigWriteTo() string { + return runConfig.writeto +} diff --git a/tui/tui.go b/tui/tui.go index d75cb9c..f4154cd 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -46,7 +46,7 @@ func Run() { os.Exit(1) } if wtStat.IsDir() { - writeDirFiles := getFiles(runConfig.writeto, runConfig.extensions) + writeDirFiles := GetFiles(runConfig.writeto, runConfig.extensions) for _, f := range writeDirFiles { files[f] = struct{}{} } @@ -54,7 +54,7 @@ func Run() { // [ ] replace with subdir check #active=2022-05-26 #zzz=2 if wrkdirStr != runConfig.writeto { - wdFiles := getFiles(wrkdirStr, runConfig.extensions) + wdFiles := GetFiles(wrkdirStr, runConfig.extensions) for _, f := range wdFiles { files[f] = struct{}{} } @@ -62,10 +62,10 @@ func Run() { items := []*tuido.Item{} for f := range files { - items = append(items, getItems(f)...) + items = append(items, GetItems(f)...) } - sortItems(items) + SortItems(items) tui := newTUI(items, runConfig) tui.houseKeeping() @@ -313,7 +313,7 @@ func (t *tui) populateRenderSelection() { // Only sort if no filter is active - preserve fuzzy search ranking if len(t.filter.Value()) == 0 { - sortItems(t.renderSelection) + SortItems(t.renderSelection) } // ensure the previous selection value is still in range @@ -448,7 +448,7 @@ func itemHasTag(itemTags []tuido.Tag, filter tuido.Tag) bool { func (t tui) Init() tea.Cmd { return tick() } -func getItems(file string) []*tuido.Item { +func GetItems(file string) []*tuido.Item { items := []*tuido.Item{} if f, err := os.Open(file); err != nil { @@ -471,7 +471,7 @@ func getItems(file string) []*tuido.Item { } } -func getFiles(wd string, extensions []string) []string { +func GetFiles(wd string, extensions []string) []string { files := []string{} @@ -501,7 +501,7 @@ func getFiles(wd string, extensions []string) []string { return files } -func sortItems(items []*tuido.Item) { +func SortItems(items []*tuido.Item) { sort.SliceStable(items, func(i, j int) bool { if items[i].Importance() > items[j].Importance() { return true