all, completed and verbose flags for ls
This commit is contained in:
		
							parent
							
								
									019fc93c06
								
							
						
					
					
						commit
						3bbb21dfc5
					
				
							
								
								
									
										31
									
								
								cmd/ls.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								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")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user