Skip to content

Commit

Permalink
fixup! fix sum-up test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-he committed Jul 27, 2024
1 parent dc2bdc9 commit 3b6cd04
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 42 deletions.
11 changes: 6 additions & 5 deletions internal/control/cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ func (command *AddCommand) Execute(args []string) error {
if strings.ContainsRune(command.Category, '|') {
panic("ERROR: category name cannot contain '|'")
}
categoryName := model.CategoryName(command.Category)
found := false
for _, category := range configData.Categories {
if category.Name == command.Category {
for _, categoryFromConfig := range configData.Categories {
if categoryFromConfig.Name == string(categoryName) {
found = true
break
}
}
if !found {
fmt.Fprintf(os.Stderr, "WARNING: category '%s' not found in config data\n", command.Category)
fmt.Fprintf(os.Stderr, "WARNING: category '%s' not found in config data\n", categoryName)
}

// verify times
Expand Down Expand Up @@ -98,7 +99,7 @@ func (command *AddCommand) Execute(args []string) error {
Start: start,
End: end,
Name: command.Name,
Cat: model.Category{Name: command.Category},
Cat: model.Category{Name: categoryName},
})

if command.RepeatInterval != "" {
Expand All @@ -124,7 +125,7 @@ func (command *AddCommand) Execute(args []string) error {
Start: currentStart,
End: currentEnd,
Name: command.Name,
Cat: model.Category{Name: command.Category},
Cat: model.Category{Name: categoryName},
}
events = append(events, event)

Expand Down
2 changes: 1 addition & 1 deletion internal/control/cli/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewController(
}
defer controller.goToDay(date)

categoryGetter := func(name string) model.Category {
categoryGetter := func(name model.CategoryName) model.Category {
cat, ok := categoryStyling.GetKnownCategoriesByName()[name]
if ok {
return *cat
Expand Down
8 changes: 4 additions & 4 deletions internal/control/cli/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (command *SummarizeCommand) Execute(args []string) error {
}

cat := model.Category{
Name: category.Name,
Name: model.CategoryName(category.Name),
Priority: category.Priority,
Goal: goal,
}
Expand All @@ -78,10 +78,10 @@ func (command *SummarizeCommand) Execute(args []string) error {
}

filterCategories := len(Opts.SummarizeCommand.CategoryFilterString) > 0
includeCategoriesByName := make(map[string]struct{})
includeCategoriesByName := make(map[model.CategoryName]struct{})
if filterCategories {
for _, name := range strings.Split(Opts.SummarizeCommand.CategoryFilterString, ",") {
includeCategoriesByName[name] = struct{}{}
includeCategoriesByName[model.CategoryName(name)] = struct{}{}
}
}

Expand All @@ -92,7 +92,7 @@ func (command *SummarizeCommand) Execute(args []string) error {
return fmt.Errorf("can't create file data provider (%w)", err)
}

categoryIncluded := func(categoryName string) bool {
categoryIncluded := func(categoryName model.CategoryName) bool {
_, ok := includeCategoriesByName[categoryName]
return ok
}
Expand Down
10 changes: 5 additions & 5 deletions internal/control/cli/timesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (command *TimesheetCommand) Execute(args []string) error {
}

cat := model.Category{
Name: category.Name,
Name: model.CategoryName(category.Name),
Priority: category.Priority,
Goal: goal,
}
Expand Down Expand Up @@ -133,11 +133,11 @@ func (command *TimesheetCommand) Execute(args []string) error {
return fmt.Errorf("category exclude filter regex is invalid (%s)", err.Error())
}
}
matcher := func(catName string) bool {
if includeRegex != nil && !includeRegex.MatchString(catName) {
matcher := func(catName model.CategoryName) bool {
if includeRegex != nil && !includeRegex.MatchString(string(catName)) {
return false
}
if excludeRegex != nil && excludeRegex.MatchString(catName) {
if excludeRegex != nil && excludeRegex.MatchString(string(catName)) {
return false
}
return true
Expand All @@ -146,7 +146,7 @@ func (command *TimesheetCommand) Execute(args []string) error {
func() {
fmt.Fprintln(os.Stderr, "PROSPECTIVE MATCHES:")
for _, cat := range configData.Categories {
if matcher(cat.Name) {
if matcher(model.CategoryName(cat.Name)) {
fmt.Fprintf(os.Stderr, " '%s'\n", cat.Name)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/control/cli/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (command *TUICommand) Execute(_ []string) error {
}

cat := model.Category{
Name: category.Name,
Name: model.CategoryName(category.Name),
Priority: category.Priority,
Goal: goal,
Deprecated: category.Deprecated,
Expand Down
16 changes: 8 additions & 8 deletions internal/model/backlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (t Task) toBaseTask() BaseTask {
log.Warn().
Str("subtask", subtask.Name).
Str("parent-task", t.Name).
Str("subtask-category", subtask.Category.Name).
Str("parent-task-category", t.Category.Name).
Str("subtask-category", string(subtask.Category.Name)).
Str("parent-task-category", string(t.Category.Name)).
Msg("subtask has different category from parent, which will be lost")
}
result.Subtasks = append(result.Subtasks, subtask.toBaseTask())
Expand All @@ -55,7 +55,7 @@ func (t Task) toBaseTask() BaseTask {

// BacklogStored.
type BacklogStored struct {
TasksByCategory map[string][]BaseTask `yaml:",inline"`
TasksByCategory map[CategoryName][]BaseTask `yaml:",inline"`
}

// BaseTask.
Expand All @@ -70,7 +70,7 @@ type BaseTask struct {
func (b *Backlog) Write(w io.Writer) error {

toBeWritten := BacklogStored{
TasksByCategory: map[string][]BaseTask{},
TasksByCategory: map[CategoryName][]BaseTask{},
}
for _, task := range b.Tasks {
categoryName := task.Category.Name
Expand All @@ -94,7 +94,7 @@ func (b *Backlog) Write(w io.Writer) error {

// BacklogFromReader reads and deserializes a backlog from the io.Reader and returns the
// backlog.
func BacklogFromReader(r io.Reader, categoryGetter func(string) Category) (*Backlog, error) {
func BacklogFromReader(r io.Reader, categoryGetter func(CategoryName) Category) (*Backlog, error) {
data, err := io.ReadAll(r)
if err != nil {
return &Backlog{}, fmt.Errorf("unable to read from reader (%s)", err.Error())
Expand All @@ -107,8 +107,8 @@ func BacklogFromReader(r io.Reader, categoryGetter func(string) Category) (*Back
}
log.Debug().Int("N-Cats", len(stored.TasksByCategory)).Msg("read storeds")

var mapSubtasks func(cat string, tasks []BaseTask) []*Task
toTask := func(cat string, b BaseTask) *Task {
var mapSubtasks func(cat CategoryName, tasks []BaseTask) []*Task
toTask := func(cat CategoryName, b BaseTask) *Task {
return &Task{
Name: b.Name,
Category: categoryGetter(cat),
Expand All @@ -117,7 +117,7 @@ func BacklogFromReader(r io.Reader, categoryGetter func(string) Category) (*Back
Subtasks: mapSubtasks(cat, b.Subtasks),
}
}
mapSubtasks = func(cat string, tasks []BaseTask) []*Task {
mapSubtasks = func(cat CategoryName, tasks []BaseTask) []*Task {
result := []*Task{}
for _, t := range tasks {
result = append(result, toTask(cat, t))
Expand Down
2 changes: 1 addition & 1 deletion internal/model/category.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model

type CategoryName = string
type CategoryName string

type Category struct {
Name CategoryName `dpedit:"name"`
Expand Down
6 changes: 3 additions & 3 deletions internal/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (e *Event) Clone() Event {
func (e *Event) String() string {
start := e.Start.String()
end := e.End.String()
cat := e.Cat.Name
name := e.Name
catName := e.Cat.Name
eventName := e.Name

return (start + "|" + end + "|" + cat + "|" + name)
return (start + "|" + end + "|" + string(catName) + "|" + eventName)
}

// ...
Expand Down
2 changes: 1 addition & 1 deletion internal/model/event_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (l *EventList) SumUpByCategory() map[CategoryName]time.Duration {

// GetTimesheetEntry returns the TimesheetEntry for this day for a given
// category (e.g. "work").
func (l *EventList) GetTimesheetEntry(matcher func(string) bool) (*TimesheetEntry, error) {
func (l *EventList) GetTimesheetEntry(matcher func(CategoryName) bool) (*TimesheetEntry, error) {
startFound := false
var firstStart time.Time
var lastEnd time.Time
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type DataProvider interface {
SetEventCategory(EventIdentifier, model.Category) error
SetEventAllData(EventIdentifier, model.Event) error

SumUpTimespanByCategory(start time.Time, end time.Time) map[string]time.Duration
SumUpTimespanByCategory(start time.Time, end time.Time) map[model.CategoryName]time.Duration

// need something here for mutability, e.g. constructing an editor...

Expand Down
2 changes: 1 addition & 1 deletion internal/storage/providers/files_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (p *FilesDataProvider) CommitState() error {
}

// TODO: doc SumUpTimespanByCategory
func (p *FilesDataProvider) SumUpTimespanByCategory(start time.Time, end time.Time) map[string]time.Duration {
func (p *FilesDataProvider) SumUpTimespanByCategory(start time.Time, end time.Time) map[model.CategoryName]time.Duration {
p.log.Fatal().Msg("TODO IMPL(SumUpTimespanByCategory)")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/providers/files_provider_file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func newEventFromDaywiseFileLine(date model.Date, line string) *model.Event {
e.End = model.DateAndTimestampToGotime(date, endTime)

e.Name = nameString
e.Cat.Name = catString
e.Cat.Name = model.CategoryName(catString)

return &e
}
4 changes: 2 additions & 2 deletions internal/styling/category_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type CategoryStyling struct {
// GetKnownCategoriesByName returns a mapping of category names to the fully
// parameterized categories (i.E. including priority), provided they exist.
// Allows ensuring that category data stays consistent across the program.
func (cs *CategoryStyling) GetKnownCategoriesByName() map[string]*model.Category {
result := make(map[string]*model.Category)
func (cs *CategoryStyling) GetKnownCategoriesByName() map[model.CategoryName]*model.Category {
result := make(map[model.CategoryName]*model.Category)

for i := range cs.styles {
cat := &cs.styles[i].Cat
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/panes/backlog_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (p *BacklogPane) Draw() {
p.Renderer.DrawText(
xBase+3, yOffset+1, wBase-2-2, 1,
style.Italicized(),
util.TruncateAt(t.Category.Name, wBase-2-2),
util.TruncateAt(string(t.Category.Name), wBase-2-2),
)
if t.Deadline != nil {
deadline := t.Deadline.Format("2006-01-02 15:04:05")
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/panes/events_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (p *EventsPane) Draw() {

style, err := p.styleForCategory(e.Cat)
if err != nil {
p.log.Error().Err(err).Str("category-name", e.Cat.Name).Msg("an error occurred getting category style")
p.log.Error().Err(err).Str("category-name", string(e.Cat.Name)).Msg("an error occurred getting category style")
style = p.Stylesheet.CategoryFallback
}
if !p.isCurrentDay() {
Expand Down Expand Up @@ -167,7 +167,7 @@ func (p *EventsPane) Draw() {
catStyling = bottomStyling.NormalizeFromBG(0.2).Unbolded().Italicized()
}
catWidth := pos.W - 2 - 1
p.Renderer.DrawText(pos.X+pos.W-1-catWidth, pos.Y+1, catWidth, 1, catStyling, util.TruncateAt(e.Cat.Name, catWidth))
p.Renderer.DrawText(pos.X+pos.W-1-catWidth, pos.Y+1, catWidth, 1, catStyling, util.TruncateAt(string(e.Cat.Name), catWidth))
}

}
Expand Down
17 changes: 13 additions & 4 deletions internal/ui/panes/summary_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ja-he/dayplan/internal/styling"
"github.com/ja-he/dayplan/internal/ui"
"github.com/ja-he/dayplan/internal/util"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand All @@ -22,6 +23,8 @@ type SummaryPane struct {
events func() ([]*model.Event, error)

categories *styling.CategoryStyling

log zerolog.Logger
}

// EnsureHidden informs the pane that it is not being shown so that it can take
Expand Down Expand Up @@ -57,13 +60,18 @@ func (p *SummaryPane) Draw() {
Events: events,
}
summary := el.SumUpByCategory()
categoriesByName := p.categories.GetKnownCategoriesByName()

maxDuration := time.Duration(0)
categories := make([]model.Category, len(summary))
{ // get sorted keys to have deterministic order
i := 0
for category, duration := range summary {
categories[i] = category
for categoryName, duration := range summary {
c, ok := categoriesByName[categoryName]
if !ok {
p.log.Warn().Str("category", string(categoryName)).Msg("unknown category")
}
categories[i] = *c
if duration > maxDuration {
maxDuration = duration
}
Expand All @@ -73,7 +81,7 @@ func (p *SummaryPane) Draw() {
}
row := 2
for _, category := range categories {
duration := summary[category]
duration := summary[category.Name]
style, err := p.categories.GetStyle(category)
if err != nil {
style = p.Stylesheet.CategoryFallback
Expand All @@ -83,7 +91,7 @@ func (p *SummaryPane) Draw() {
durationLen := 20
barWidth := int(float64(duration) / float64(maxDuration) * float64(w-catLen-durationLen))
p.Renderer.DrawBox(x+catLen+durationLen, y+row, barWidth, 1, categoryStyling)
p.Renderer.DrawText(x, y+row, catLen, 1, p.Stylesheet.SummaryDefault, util.TruncateAt(category.Name, catLen))
p.Renderer.DrawText(x, y+row, catLen, 1, p.Stylesheet.SummaryDefault, util.TruncateAt(string(category.Name), catLen))
p.Renderer.DrawText(x+catLen, y+row, durationLen, 1, categoryStyling, "("+duration.String()+")")
row++
}
Expand Down Expand Up @@ -120,5 +128,6 @@ func NewSummaryPane(
titleString: titleString,
events: events,
categories: categories,
log: log.With().Str("component", "summary-pane").Logger(),
}
}
2 changes: 1 addition & 1 deletion internal/ui/panes/tools_pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (p *ToolsPane) Draw() {
}

p.Renderer.DrawBox(box.X, box.Y, box.W, box.H, styling)
p.Renderer.DrawText(box.X+1, box.Y+textHeightOffset, textLen, 1, styling, util.TruncateAt(cat.Name, textLen))
p.Renderer.DrawText(box.X+1, box.Y+textHeightOffset, textLen, 1, styling, util.TruncateAt(string(cat.Name), textLen))
}
p.lastBoxesDrawn = boxes
}
Expand Down

0 comments on commit 3b6cd04

Please sign in to comment.