func cmdStart(d db.DB, resume bool, categoryS string) { entries, err := active(d) if err != nil { fatal(err) } now := time.Now() path, err := d.CategoryPath(ParseCategory(categoryS), true) if err != nil { fatal(err) } entry := &db.Entry{CategoryID: path.CategoryID(), Start: now} if resume { last, err := Last(d) if err != nil { fatal(err) } if !last.End.IsZero() { entry.Start = last.End } if entry.CategoryID == "" { entry.CategoryID = last.CategoryID } } if err := d.SaveEntry(entry); err != nil { fatal(err) } FprintEntry(os.Stdout, entry, path, PrintHideDuration|PrintHideEnd) if err := endAt(d, entries, now); err != nil { fatal(err) } }
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) } } }