diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6c17c92 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.bullercodeworks.com/brian/go-timertxt + +go 1.17 diff --git a/sort.go b/sort.go index b8a86a1..c483964 100644 --- a/sort.go +++ b/sort.go @@ -45,7 +45,7 @@ func (ts *timerlistSort) Swap(l, r int) { } func (ts *timerlistSort) Less(l, r int) bool { - return ts.by(&ts.timerlists[l], &ts.timerlists[r]) + return ts.by(ts.timerlists[l], ts.timerlists[r]) } func (timerlist *TimerList) sortBy(by func(t1, t2 *Timer) bool) *TimerList { diff --git a/timer.go b/timer.go index f428d78..da823a5 100644 --- a/timer.go +++ b/timer.go @@ -88,7 +88,9 @@ func NewTimer() *Timer { // ParseTimer parses the input text string into a Timer struct func ParseTimer(text string) (*Timer, error) { var err error - timer := Timer{} + timer := Timer{ + AdditionalTags: make(map[string]string), + } timer.Original = strings.Trim(text, "\t\n\r ") originalParts := strings.Fields(timer.Original) @@ -209,3 +211,12 @@ func (timer *Timer) HasProject(project string) bool { } return false } + +func (timer *Timer) HasTag(name string) bool { + _, ok := timer.AdditionalTags[name] + return ok +} + +func (timer *Timer) GetTag(name string) string { + return timer.AdditionalTags[name] +} diff --git a/timerlist.go b/timerlist.go index 7765dca..255af50 100644 --- a/timerlist.go +++ b/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 {