2019-02-15 18:46:50 +00:00
|
|
|
package timertxt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flags for defining sort element and order.
|
|
|
|
const (
|
2023-08-23 14:58:04 +00:00
|
|
|
SortUnfinishedStart = iota
|
|
|
|
SortStartDateAsc
|
|
|
|
SortStartDateDesc
|
|
|
|
SortFinishDateAsc
|
|
|
|
SortFinishDateDesc
|
|
|
|
SortError
|
2019-02-15 18:46:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sort allows a TimerList to be sorted by certain predefined fields.
|
2023-08-23 14:58:04 +00:00
|
|
|
// See constants Sort* for fields and sort order.
|
2019-02-15 18:46:50 +00:00
|
|
|
func (timerlist *TimerList) Sort(sortFlag int) error {
|
|
|
|
switch sortFlag {
|
2023-08-23 14:58:04 +00:00
|
|
|
case SortUnfinishedStart:
|
2019-02-22 23:31:44 +00:00
|
|
|
timerlist.sortByUnfinishedThenStart()
|
2023-08-23 14:58:04 +00:00
|
|
|
case SortStartDateAsc, SortStartDateDesc:
|
2019-02-15 18:46:50 +00:00
|
|
|
timerlist.sortByStartDate(sortFlag)
|
2023-08-23 14:58:04 +00:00
|
|
|
case SortFinishDateAsc, SortFinishDateDesc:
|
2019-02-15 18:46:50 +00:00
|
|
|
timerlist.sortByFinishDate(sortFlag)
|
|
|
|
default:
|
|
|
|
return errors.New("Unrecognized sort option")
|
|
|
|
}
|
2023-08-23 16:09:33 +00:00
|
|
|
timerlist.SortFlag = sortFlag
|
2019-02-15 18:46:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
func (timerlist *TimerList) refresh() {
|
2023-08-23 16:09:33 +00:00
|
|
|
timerlist.Sort(timerlist.SortFlag)
|
2023-01-12 12:04:53 +00:00
|
|
|
}
|
2019-02-15 18:46:50 +00:00
|
|
|
|
|
|
|
type timerlistSort struct {
|
|
|
|
timerlists TimerList
|
|
|
|
by func(t1, t2 *Timer) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *timerlistSort) Len() int {
|
2023-08-23 16:09:33 +00:00
|
|
|
return len(ts.timerlists.Timers)
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *timerlistSort) Swap(l, r int) {
|
2023-08-23 16:09:33 +00:00
|
|
|
ts.timerlists.Timers[l], ts.timerlists.Timers[r] = ts.timerlists.Timers[r], ts.timerlists.Timers[l]
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *timerlistSort) Less(l, r int) bool {
|
2023-08-23 16:09:33 +00:00
|
|
|
return ts.by(ts.timerlists.Timers[l], ts.timerlists.Timers[r])
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (timerlist *TimerList) sortBy(by func(t1, t2 *Timer) bool) *TimerList {
|
|
|
|
ts := &timerlistSort{
|
|
|
|
timerlists: *timerlist,
|
|
|
|
by: by,
|
|
|
|
}
|
|
|
|
sort.Sort(ts)
|
|
|
|
return timerlist
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortByDate(asc bool, date1, date2 time.Time) bool {
|
|
|
|
if asc { // ASC
|
|
|
|
if !date1.IsZero() && !date2.IsZero() {
|
|
|
|
return date1.Before(date2)
|
|
|
|
}
|
|
|
|
return !date2.IsZero()
|
|
|
|
}
|
|
|
|
// DESC
|
|
|
|
if !date1.IsZero() && !date2.IsZero() {
|
|
|
|
return date1.After(date2)
|
|
|
|
}
|
|
|
|
return date2.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (timerlist *TimerList) sortByStartDate(order int) *TimerList {
|
|
|
|
timerlist.sortBy(func(t1, t2 *Timer) bool {
|
2023-08-23 14:58:04 +00:00
|
|
|
return sortByDate(order == SortStartDateAsc, t1.StartDate, t2.StartDate)
|
2019-02-15 18:46:50 +00:00
|
|
|
})
|
|
|
|
return timerlist
|
|
|
|
}
|
|
|
|
|
|
|
|
func (timerlist *TimerList) sortByFinishDate(order int) *TimerList {
|
|
|
|
timerlist.sortBy(func(t1, t2 *Timer) bool {
|
2023-08-23 14:58:04 +00:00
|
|
|
return sortByDate(order == SortFinishDateAsc, t1.FinishDate, t2.FinishDate)
|
2019-02-15 18:46:50 +00:00
|
|
|
})
|
|
|
|
return timerlist
|
|
|
|
}
|
2019-02-22 23:31:44 +00:00
|
|
|
|
|
|
|
func (timerlist *TimerList) sortByUnfinishedThenStart() *TimerList {
|
|
|
|
timerlist.sortBy(func(t1, t2 *Timer) bool {
|
|
|
|
if t1.FinishDate.IsZero() && !t2.FinishDate.IsZero() {
|
|
|
|
return true
|
|
|
|
} else if t2.FinishDate.IsZero() && !t1.FinishDate.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return sortByDate(false, t1.StartDate, t2.StartDate)
|
|
|
|
})
|
|
|
|
return timerlist
|
|
|
|
}
|