Ejemplo n.º 1
0
Archivo: cmds.go Proyecto: hiroapp/cli
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(), "  "))
		}
	}
}
Ejemplo n.º 2
0
Archivo: cmds.go Proyecto: hiroapp/cli
func cmdReport(d db.DB, categoryS, periodS, firstDayS string) {
	period, err := datetime.ParsePeriod(periodS)
	if err != nil {
		fatal(err)
	} else if period == datetime.Day {
		fatal(errors.New("bad period: day"))
	}
	firstDay, err := datetime.ParseWeekday(firstDayS)
	if err != nil {
		fatal(err)
	}
	path, err := d.CategoryPath(ParseCategory(categoryS), false)
	if err != nil {
		fatal(err)
	}
	entryItr, err := d.Query(db.Query{CategoryID: path.CategoryID()})
	if err != nil {
		fatal(err)
	}
	defer entryItr.Close()
	entry, err := entryItr.Next()
	if err == io.EOF {
		return
	} else if err != nil {
		fatal(err)
	}
	// @TODO move logic into separate function
	now := time.Now()
	reportItr := datetime.NewIterator(entry.Start, period, false, firstDay)
	report := &Report{Duration: period}
	report.From, report.To = reportItr.Next()
	dayItr := datetime.NewIterator(report.To, datetime.Day, false, firstDay)
	day := &ReportDay{}
	day.From, day.To = dayItr.Next()
	report.Days = append([]*ReportDay{day}, report.Days...)
	noteAssigned := false

outer:
	for {
		var overlap time.Duration
		for {
			overlap = entry.PartialDuration(now, day.From, day.To)
			if overlap > 0 {
				day.Tracked += overlap
				if !noteAssigned {
					note := strings.Trim(entry.Note, "\n")
					if note != "" {
						day.Notes = append([]string{note}, day.Notes...)
					}
					noteAssigned = true
				}
			}
			if !entry.Start.Before(day.From) {
				entry, err = entryItr.Next()
				if err == io.EOF {
					fmt.Fprint(os.Stdout, FormatReport(report))
					break outer
				} else if err != nil {
					fatal(err)
				}
				noteAssigned = false
				continue
			}
			day = &ReportDay{}
			day.From, day.To = dayItr.Next()
			if day.To.Before(report.From) {
				fmt.Fprint(os.Stdout, FormatReport(report))
				report = &Report{Duration: period}
				report.From, report.To = reportItr.Next()
			}
			report.Days = append([]*ReportDay{day}, report.Days...)
		}
	}
}