func cmdLs(d db.DB, categoryS string, asc bool) { if categories, err := d.Categories(); err != nil { fatal(err) } else if path, err := d.CategoryPath(ParseCategory(categoryS), false); err != nil { fatal(err) } else if itr, err := d.Query(db.Query{Asc: asc, CategoryID: path.CategoryID()}); err != nil { fatal(err) } else { FprintIterator(os.Stdout, itr, categories, PrintDefault) } }
func cmdRm(d db.DB, id string) { entry, err := ById(d, id) if err != nil { fatal(err) } else if err := d.Remove(id); err != nil { fatal(err) } categories, err := d.Categories() if err != nil { fatal(err) } FprintEntry(os.Stdout, entry, categories.Path(entry.CategoryID), PrintDefault) }
func endAt(d db.DB, entries []*db.Entry, t time.Time) error { categories, err := d.Categories() if err != nil { return err } for _, entry := range entries { entry.End = t if err := d.SaveEntry(entry); err != nil { return err } FprintEntry(os.Stdout, entry, categories.Path(entry.CategoryID), PrintDefault) } return nil }
func cmdEdit(d db.DB, id string) { var ( entry *db.Entry err error ) if id != "" { entry, err = ById(d, id) } else { entry, err = Last(d) } if err != nil { fatal(err) } categories, err := d.Categories() if err != nil { fatal(err) } e := term.NewEditor() FprintEntry(e, entry, categories.Path(entry.CategoryID), PrintSeparator|PrintHideDuration) if err := e.Run(); err != nil { fatal(err) } else if doc, err := ParseEntryDocument(e); err != nil { fatal(err) } else if doc == nil { return } else { entry := &db.Entry{ ID: doc.ID, Start: doc.Start, End: doc.End, Note: doc.Note, } if path, err := d.CategoryPath(doc.Category, false); err != nil { fatal(err) } else { entry.CategoryID = path.CategoryID() } if err := d.SaveEntry(entry); err != nil { fatal(err) } else { FprintIterator(os.Stdout, db.EntryIterator([]*db.Entry{entry}), categories, PrintDefault) } } }
func cmdSummary(d db.DB, periodS, firstDayS string) { period, err := datetime.ParsePeriod(periodS) if err != nil { fatal(err) } firstDay, err := datetime.ParseWeekday(firstDayS) if err != nil { fatal(err) } categories, err := d.Categories() if err != nil { fatal(err) } itr, err := NewSummaryIterator(d, period, firstDay, time.Now()) if err != nil { fatal(err) } defer itr.Close() for { if summary, err := itr.Next(); err == io.EOF { break } else if err != nil { fatal(err) } else { fmt.Printf("%s\n\n", PeriodHeadline(summary.From, summary.To, period)) names := make(map[string]string) order := make([]string, 0, len(summary.Categories)) for id, _ := range summary.Categories { names[id] = FormatCategory(categories.Path(id)) order = append(order, id) } slice.Sort(order, func(i, j int) bool { return summary.Categories[order[i]] > summary.Categories[order[j]] }) t := table.New().Padding(" ") for _, id := range order { d := FormatDuration(summary.Categories[id]) t.Add(table.String(names[id]), table.String(d).Align(table.Right)) } fmt.Printf("%s\n", Indent(t.String(), " ")) } } }