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:
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// DisplayFormat defines how tasks should be displayed
|
||||
@@ -131,16 +132,11 @@ func NewestReport() *Report {
|
||||
BaseFilter: filter,
|
||||
DisplayFormat: DisplayFormatTable,
|
||||
SortFunc: func(tasks []*Task) []*Task {
|
||||
// Sort by created descending
|
||||
sorted := make([]*Task, len(tasks))
|
||||
copy(sorted, tasks)
|
||||
for i := 0; i < len(sorted)-1; i++ {
|
||||
for j := i + 1; j < len(sorted); j++ {
|
||||
if sorted[i].Created.Before(sorted[j].Created) {
|
||||
sorted[i], sorted[j] = sorted[j], sorted[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
return sorted[i].Created.After(sorted[j].Created)
|
||||
})
|
||||
return sorted
|
||||
},
|
||||
LimitFunc: func(tasks []*Task) []*Task {
|
||||
@@ -164,23 +160,14 @@ func NextReport() *Report {
|
||||
BaseFilter: filter,
|
||||
DisplayFormat: DisplayFormatTable,
|
||||
SortFunc: func(tasks []*Task) []*Task {
|
||||
// Sort by urgency descending
|
||||
cfg, _ := GetConfig()
|
||||
coeffs := BuildUrgencyCoefficients(cfg)
|
||||
|
||||
sorted := make([]*Task, len(tasks))
|
||||
copy(sorted, tasks)
|
||||
|
||||
for i := 0; i < len(sorted)-1; i++ {
|
||||
for j := i + 1; j < len(sorted); j++ {
|
||||
urgI := sorted[i].CalculateUrgency(coeffs)
|
||||
urgJ := sorted[j].CalculateUrgency(coeffs)
|
||||
if urgI < urgJ {
|
||||
sorted[i], sorted[j] = sorted[j], sorted[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
return sorted[i].CalculateUrgency(coeffs) > sorted[j].CalculateUrgency(coeffs)
|
||||
})
|
||||
return sorted
|
||||
},
|
||||
LimitFunc: func(tasks []*Task) []*Task {
|
||||
@@ -208,16 +195,11 @@ func OldestReport() *Report {
|
||||
BaseFilter: filter,
|
||||
DisplayFormat: DisplayFormatTable,
|
||||
SortFunc: func(tasks []*Task) []*Task {
|
||||
// Sort by created ascending (already default, but explicit)
|
||||
sorted := make([]*Task, len(tasks))
|
||||
copy(sorted, tasks)
|
||||
for i := 0; i < len(sorted)-1; i++ {
|
||||
for j := i + 1; j < len(sorted); j++ {
|
||||
if sorted[i].Created.After(sorted[j].Created) {
|
||||
sorted[i], sorted[j] = sorted[j], sorted[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
return sorted[i].Created.Before(sorted[j].Created)
|
||||
})
|
||||
return sorted
|
||||
},
|
||||
}
|
||||
@@ -429,18 +411,13 @@ func sortByUrgency(tasks []*Task) []*Task {
|
||||
sorted := make([]*Task, len(tasks))
|
||||
copy(sorted, tasks)
|
||||
|
||||
// Calculate and store urgency on each task
|
||||
for _, t := range sorted {
|
||||
t.Urgency = t.CalculateUrgency(coeffs)
|
||||
}
|
||||
|
||||
for i := 0; i < len(sorted)-1; i++ {
|
||||
for j := i + 1; j < len(sorted); j++ {
|
||||
if sorted[i].Urgency < sorted[j].Urgency {
|
||||
sorted[i], sorted[j] = sorted[j], sorted[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
return sorted[i].Urgency > sorted[j].Urgency
|
||||
})
|
||||
|
||||
return sorted
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user