refactor: deduplicate engine internals, replace bubble sorts, remove dead code
Extract shared code that was duplicated across functions: - taskJSON struct (MarshalJSON/UnmarshalJSON) to package-level type - scanTask(scanner) helper for GetTask/GetTasks (~70 identical lines) - monthNames map for parseMonthName/parseDayAndMonth - applyNonDateAttribute helper for Apply/ApplyToNew - resolveDisplayID calls replace inline loops in FormatTaskListWithFormat Replace O(n²) bubble sorts with sort.Slice in all four report sort functions (sortByUrgency, NewestReport, NextReport, OldestReport). Remove dead code: formatTimeWithColor (unused, also used time.Now() instead of timeNow()), getCurrentTimestamp (unnecessary wrapper). Remove ~20 comments that restated the next line of code. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -92,7 +92,6 @@ func (m *Modifier) Apply(task *Task) error {
|
||||
for _, key := range m.AttributeOrder {
|
||||
valuePtr := m.SetAttributes[key]
|
||||
|
||||
// Handle date attributes with relative expression support
|
||||
if dateKeys[key] {
|
||||
if err := applyDateAttribute(key, valuePtr, task, resolvedDates); err != nil {
|
||||
return err
|
||||
@@ -100,30 +99,11 @@ func (m *Modifier) Apply(task *Task) error {
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle non-date attributes
|
||||
switch key {
|
||||
case "priority":
|
||||
if valuePtr == nil {
|
||||
task.Priority = PriorityDefault
|
||||
} else {
|
||||
task.Priority = Priority(priorityStringToInt(*valuePtr))
|
||||
}
|
||||
case "project":
|
||||
task.Project = valuePtr
|
||||
case "recur":
|
||||
if valuePtr == nil {
|
||||
task.RecurrenceDuration = nil
|
||||
} else {
|
||||
duration, err := ParseRecurrencePattern(*valuePtr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid recurrence: %w", err)
|
||||
}
|
||||
task.RecurrenceDuration = &duration
|
||||
}
|
||||
if err := applyNonDateAttribute(key, valuePtr, task); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Apply tag changes
|
||||
for _, tag := range m.AddTags {
|
||||
if err := task.AddTag(tag); err != nil {
|
||||
return err
|
||||
@@ -160,7 +140,6 @@ func (m *Modifier) ApplyToNew(task *Task) error {
|
||||
for _, key := range m.AttributeOrder {
|
||||
valuePtr := m.SetAttributes[key]
|
||||
|
||||
// Handle date attributes with relative expression support
|
||||
if dateKeys[key] {
|
||||
if err := applyDateAttribute(key, valuePtr, task, resolvedDates); err != nil {
|
||||
return err
|
||||
@@ -168,26 +147,8 @@ func (m *Modifier) ApplyToNew(task *Task) error {
|
||||
continue
|
||||
}
|
||||
|
||||
// Handle non-date attributes
|
||||
switch key {
|
||||
case "priority":
|
||||
if valuePtr == nil {
|
||||
task.Priority = PriorityDefault
|
||||
} else {
|
||||
task.Priority = Priority(priorityStringToInt(*valuePtr))
|
||||
}
|
||||
case "project":
|
||||
task.Project = valuePtr
|
||||
case "recur":
|
||||
if valuePtr == nil {
|
||||
task.RecurrenceDuration = nil
|
||||
} else {
|
||||
duration, err := ParseRecurrencePattern(*valuePtr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid recurrence: %w", err)
|
||||
}
|
||||
task.RecurrenceDuration = &duration
|
||||
}
|
||||
if err := applyNonDateAttribute(key, valuePtr, task); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,6 +156,31 @@ func (m *Modifier) ApplyToNew(task *Task) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// applyNonDateAttribute applies a non-date attribute (priority, project, recur) to a task.
|
||||
func applyNonDateAttribute(key string, valuePtr *string, task *Task) error {
|
||||
switch key {
|
||||
case "priority":
|
||||
if valuePtr == nil {
|
||||
task.Priority = PriorityDefault
|
||||
} else {
|
||||
task.Priority = Priority(priorityStringToInt(*valuePtr))
|
||||
}
|
||||
case "project":
|
||||
task.Project = valuePtr
|
||||
case "recur":
|
||||
if valuePtr == nil {
|
||||
task.RecurrenceDuration = nil
|
||||
} else {
|
||||
duration, err := ParseRecurrencePattern(*valuePtr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid recurrence: %w", err)
|
||||
}
|
||||
task.RecurrenceDuration = &duration
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseRelativeExpression checks if a string is a relative date expression
|
||||
// Returns: baseAttr, operator, offset, isRelative
|
||||
// Example: "due-1d" -> "due", "-", "1d", true
|
||||
|
||||
Reference in New Issue
Block a user