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(), " ")) } } }
func FormatReport(r *Report) string { if r == nil { return "" } buf := &bytes.Buffer{} buf.WriteString(PeriodHeadline(r.From, r.To, r.Duration)) buf.WriteString("\n\n") t := table.New() t.Add( table.String("DATE"), table.String("DAY"), table.String("HOURS"), table.String("TOTAL"), ) var trackedTotal time.Duration for _, day := range r.Days { trackedTotal += day.Tracked trackedS := FormatDuration(day.Tracked) trackedTotalS := FormatDuration(trackedTotal) // @TODO add better padding support to table t.Add( table.String(day.From.Format("2006-01-02 ")), table.String(day.From.Format("Mon ")), table.String(trackedS+" ").Align(table.Right), table.String(trackedTotalS).Align(table.Right), ) } buf.WriteString(Indent(t.String(), " ")) buf.WriteString("\n\n") for _, day := range r.Days { if day.Tracked == 0 { continue } trackedS := FormatDuration(day.Tracked) dayS := day.From.Format("2006-01-02 (Monday)") fmt.Fprintf(buf, "%s - %s\n\n", dayS, trackedS) buf.WriteString(Indent(strings.Join(day.Notes, "\n"), " ")) buf.WriteString("\n\n") } return buf.String() }