diff --git a/cmd/ls.go b/cmd/ls.go index 739bfc6..08658de 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -37,13 +37,30 @@ Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - todos := getAllTodos() + var todos []Todo + all, err := cmd.Flags().GetBool("all") + errPanic(err, "") + comp, err := cmd.Flags().GetBool("completed") + errPanic(err, "") + verbose, err := cmd.Flags().GetBool("verbose") + errPanic(err, "") + if all { + todos = getAllTodos() + } else if comp { + todos = getDoneTodos() + } else { + todos = getUnDoneTodos() + } ws, err := unix.IoctlGetWinsize(int(os.Stdin.Fd()), unix.TIOCGWINSZ) errPanic(err, "failed to determine terminal width") cols := int(ws.Col) t := table.NewWriter() t.SetOutputMirror(os.Stdout) - t.AppendHeader(table.Row{"#", "Description", "Due date", "Completed"}) + if verbose { + t.AppendHeader(table.Row{"#", "Description", "Due date", "Completed", "Created at"}) + } else { + t.AppendHeader(table.Row{"#", "Description", "Due date", "Completed"}) + } for _, todo := range todos { date := todo.DueDate.Format("02/01/2006 15:04") d := time.Time{} @@ -58,7 +75,11 @@ to quickly create a Cobra application.`, if todo.IsDone { comp = '✓' } - t.AppendRow([]interface{}{todo.ID, desc, date, string(comp)}) + if verbose { + t.AppendRow([]interface{}{todo.ID, desc, date, string(comp), todo.CreatedAt.Format("02/01/2006 15:04")}) + } else { + t.AppendRow([]interface{}{todo.ID, desc, date, string(comp)}) + } } t.SetStyle(table.StyleLight) t.SetAllowedRowLength(cols) @@ -80,4 +101,8 @@ func insertNth(s string, n int) string { func init() { rootCmd.AddCommand(lsCmd) + lsCmd.Flags().BoolP("all", "a", false, "show all todos") + lsCmd.Flags().BoolP("completed", "c", false, "show all completed todos") + lsCmd.Flags().BoolP("verbose", "v", false, "show all fields of todos") + lsCmd.MarkFlagsMutuallyExclusive("all", "completed") } diff --git a/cmd/todo.go b/cmd/todo.go index a93ce8d..d73dd4d 100644 --- a/cmd/todo.go +++ b/cmd/todo.go @@ -97,6 +97,7 @@ func getAllTodos() []Todo { return todos } +// getDoneTodos returns all completed todos. func getDoneTodos() []Todo { db := openDB() var todos []Todo @@ -106,11 +107,11 @@ func getDoneTodos() []Todo { return todos } - +// getDoneTodos returns all uncompleted todos. func getUnDoneTodos() []Todo { db := openDB() var todos []Todo - res := db.Where(&Todo{IsDone: false}).Find(&todos) + res := db.Where("is_done = ?", false).Find(&todos) errPanic(res.Error, "failed to get all todos") return todos