Several changes
Switch timerlist to a slice of pointers to timers Go mod
This commit is contained in:
parent
d35b67037e
commit
d0c9ea8f27
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module git.bullercodeworks.com/brian/go-timertxt
|
||||||
|
|
||||||
|
go 1.17
|
2
sort.go
2
sort.go
@ -45,7 +45,7 @@ func (ts *timerlistSort) Swap(l, r int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts *timerlistSort) Less(l, r int) bool {
|
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 {
|
func (timerlist *TimerList) sortBy(by func(t1, t2 *Timer) bool) *TimerList {
|
||||||
|
13
timer.go
13
timer.go
@ -88,7 +88,9 @@ func NewTimer() *Timer {
|
|||||||
// ParseTimer parses the input text string into a Timer struct
|
// ParseTimer parses the input text string into a Timer struct
|
||||||
func ParseTimer(text string) (*Timer, error) {
|
func ParseTimer(text string) (*Timer, error) {
|
||||||
var err error
|
var err error
|
||||||
timer := Timer{}
|
timer := Timer{
|
||||||
|
AdditionalTags: make(map[string]string),
|
||||||
|
}
|
||||||
timer.Original = strings.Trim(text, "\t\n\r ")
|
timer.Original = strings.Trim(text, "\t\n\r ")
|
||||||
originalParts := strings.Fields(timer.Original)
|
originalParts := strings.Fields(timer.Original)
|
||||||
|
|
||||||
@ -209,3 +211,12 @@ func (timer *Timer) HasProject(project string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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]
|
||||||
|
}
|
||||||
|
32
timerlist.go
32
timerlist.go
@ -12,18 +12,26 @@ import (
|
|||||||
|
|
||||||
// TimerList represents a list of timer.txt timer entries.
|
// TimerList represents a list of timer.txt timer entries.
|
||||||
// It is usually loasded from a whole timer.txt file.
|
// It is usually loasded from a whole timer.txt file.
|
||||||
type TimerList []Timer
|
type TimerList []*Timer
|
||||||
|
|
||||||
// NewTimerList creates a new empty TimerList.
|
// NewTimerList creates a new empty TimerList.
|
||||||
func NewTimerList() *TimerList {
|
func NewTimerList() *TimerList {
|
||||||
return &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) {
|
func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) {
|
||||||
var found *Timer
|
var found *Timer
|
||||||
var latest time.Time
|
var latest time.Time
|
||||||
for i := range *timerlist {
|
for i := range *timerlist {
|
||||||
t := &([]Timer(*timerlist))[i]
|
t := ([]*Timer(*timerlist))[i]
|
||||||
if t.FinishDate.IsZero() {
|
if t.FinishDate.IsZero() {
|
||||||
if t.StartDate.After(latest) {
|
if t.StartDate.After(latest) {
|
||||||
found = t
|
found = t
|
||||||
@ -43,7 +51,7 @@ func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (timerlist *TimerList) GetTimersInRange(start, end time.Time) *TimerList {
|
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) {
|
if t.StartDate.Before(end) && t.StartDate.After(start) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -56,13 +64,13 @@ func (timerlist *TimerList) GetTimersInRange(start, end time.Time) *TimerList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (timerlist *TimerList) GetTimersWithContext(context string) *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)
|
return t.HasContext(context)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (timerlist *TimerList) GetTimersWithProject(project string) *TimerList {
|
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)
|
return t.HasProject(project)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -95,17 +103,17 @@ func (timerlist *TimerList) AddTimer(timer *Timer) {
|
|||||||
t.Id++
|
t.Id++
|
||||||
}
|
}
|
||||||
// Now prepend the timer to the slice
|
// Now prepend the timer to the slice
|
||||||
*timerlist = append(*timerlist, Timer{})
|
*timerlist = append(*timerlist, &Timer{})
|
||||||
copy((*timerlist)[1:], (*timerlist)[0:])
|
copy((*timerlist)[1:], (*timerlist)[0:])
|
||||||
(*timerlist)[0] = *timer
|
(*timerlist)[0] = timer
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTimer returns the Timer with the given timer 'id' from the TimerList.
|
// GetTimer returns the Timer with the given timer 'id' from the TimerList.
|
||||||
// Returns an error if Timer could not be found.
|
// Returns an error if Timer could not be found.
|
||||||
func (timerlist *TimerList) GetTimer(id int) (*Timer, error) {
|
func (timerlist *TimerList) GetTimer(id int) (*Timer, error) {
|
||||||
for i := range *timerlist {
|
for i := range *timerlist {
|
||||||
if ([]Timer(*timerlist))[i].Id == id {
|
if ([]*Timer(*timerlist))[i].Id == id {
|
||||||
return &([]Timer(*timerlist))[i], nil
|
return ([]*Timer(*timerlist))[i], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, errors.New("timer not found")
|
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
|
// 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.
|
// 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
|
var newList TimerList
|
||||||
for _, t := range *timerlist {
|
for _, t := range *timerlist {
|
||||||
if predicate(t) {
|
if predicate(t) {
|
||||||
@ -180,7 +188,7 @@ func (timerlist *TimerList) Filter(predicate func(Timer) bool) *TimerList {
|
|||||||
// LoadFromFile loads a TimerList from *os.File.
|
// 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.
|
// 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 {
|
func (timerlist *TimerList) LoadFromFile(file *os.File) error {
|
||||||
*timerlist = []Timer{} // Empty timerlist
|
*timerlist = []*Timer{} // Empty timerlist
|
||||||
timerId := 1
|
timerId := 1
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@ -194,7 +202,7 @@ func (timerlist *TimerList) LoadFromFile(file *os.File) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
timer.Id = timerId
|
timer.Id = timerId
|
||||||
*timerlist = append(*timerlist, *timer)
|
*timerlist = append(*timerlist, timer)
|
||||||
timerId++
|
timerId++
|
||||||
}
|
}
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user