Several changes
Switch timerlist to a slice of pointers to timers Go mod
This commit is contained in:
32
timerlist.go
32
timerlist.go
@@ -12,18 +12,26 @@ import (
|
||||
|
||||
// TimerList represents a list of timer.txt timer entries.
|
||||
// It is usually loasded from a whole timer.txt file.
|
||||
type TimerList []Timer
|
||||
type TimerList []*Timer
|
||||
|
||||
// NewTimerList creates a new empty TimerList.
|
||||
func NewTimerList() *TimerList {
|
||||
return &TimerList{}
|
||||
}
|
||||
|
||||
func (timerlist *TimerList) Size() int {
|
||||
return len([]*Timer(*timerlist))
|
||||
}
|
||||
|
||||
func (timerlist *TimerList) GetTimerSlice() []*Timer {
|
||||
return []*Timer(*timerlist)
|
||||
}
|
||||
|
||||
func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) {
|
||||
var found *Timer
|
||||
var latest time.Time
|
||||
for i := range *timerlist {
|
||||
t := &([]Timer(*timerlist))[i]
|
||||
t := ([]*Timer(*timerlist))[i]
|
||||
if t.FinishDate.IsZero() {
|
||||
if t.StartDate.After(latest) {
|
||||
found = t
|
||||
@@ -43,7 +51,7 @@ func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) {
|
||||
}
|
||||
|
||||
func (timerlist *TimerList) GetTimersInRange(start, end time.Time) *TimerList {
|
||||
fltr := func(t Timer) bool {
|
||||
fltr := func(t *Timer) bool {
|
||||
if t.StartDate.Before(end) && t.StartDate.After(start) {
|
||||
return true
|
||||
}
|
||||
@@ -56,13 +64,13 @@ func (timerlist *TimerList) GetTimersInRange(start, end time.Time) *TimerList {
|
||||
}
|
||||
|
||||
func (timerlist *TimerList) GetTimersWithContext(context string) *TimerList {
|
||||
return timerlist.Filter(func(t Timer) bool {
|
||||
return timerlist.Filter(func(t *Timer) bool {
|
||||
return t.HasContext(context)
|
||||
})
|
||||
}
|
||||
|
||||
func (timerlist *TimerList) GetTimersWithProject(project string) *TimerList {
|
||||
return timerlist.Filter(func(t Timer) bool {
|
||||
return timerlist.Filter(func(t *Timer) bool {
|
||||
return t.HasProject(project)
|
||||
})
|
||||
}
|
||||
@@ -95,17 +103,17 @@ func (timerlist *TimerList) AddTimer(timer *Timer) {
|
||||
t.Id++
|
||||
}
|
||||
// Now prepend the timer to the slice
|
||||
*timerlist = append(*timerlist, Timer{})
|
||||
*timerlist = append(*timerlist, &Timer{})
|
||||
copy((*timerlist)[1:], (*timerlist)[0:])
|
||||
(*timerlist)[0] = *timer
|
||||
(*timerlist)[0] = timer
|
||||
}
|
||||
|
||||
// GetTimer returns the Timer with the given timer 'id' from the TimerList.
|
||||
// Returns an error if Timer could not be found.
|
||||
func (timerlist *TimerList) GetTimer(id int) (*Timer, error) {
|
||||
for i := range *timerlist {
|
||||
if ([]Timer(*timerlist))[i].Id == id {
|
||||
return &([]Timer(*timerlist))[i], nil
|
||||
if ([]*Timer(*timerlist))[i].Id == id {
|
||||
return ([]*Timer(*timerlist))[i], nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("timer not found")
|
||||
@@ -167,7 +175,7 @@ func (timerlist *TimerList) ArchiveTimerToFile(timer Timer, filename string) err
|
||||
|
||||
// Filter filters the current TimerList for the given predicate (a function that takes a timer as input and returns a
|
||||
// bool), and returns a new TimerList. The original TimerList is not modified.
|
||||
func (timerlist *TimerList) Filter(predicate func(Timer) bool) *TimerList {
|
||||
func (timerlist *TimerList) Filter(predicate func(*Timer) bool) *TimerList {
|
||||
var newList TimerList
|
||||
for _, t := range *timerlist {
|
||||
if predicate(t) {
|
||||
@@ -180,7 +188,7 @@ func (timerlist *TimerList) Filter(predicate func(Timer) bool) *TimerList {
|
||||
// LoadFromFile loads a TimerList from *os.File.
|
||||
// Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File.
|
||||
func (timerlist *TimerList) LoadFromFile(file *os.File) error {
|
||||
*timerlist = []Timer{} // Empty timerlist
|
||||
*timerlist = []*Timer{} // Empty timerlist
|
||||
timerId := 1
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
@@ -194,7 +202,7 @@ func (timerlist *TimerList) LoadFromFile(file *os.File) error {
|
||||
return err
|
||||
}
|
||||
timer.Id = timerId
|
||||
*timerlist = append(*timerlist, *timer)
|
||||
*timerlist = append(*timerlist, timer)
|
||||
timerId++
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user