When adding a new timer, prepend it to the list

This commit is contained in:
Brian Buller 2019-03-06 08:10:17 -06:00
parent 99fbdb003a
commit b9dd8302dc
2 changed files with 37 additions and 17 deletions

View File

@ -38,17 +38,19 @@ type Timer struct {
// //
// For example: // For example:
// "2019-02-15T11:43:00-0600 Working on Go Library @home @personal +timertxt customTag1:Important! due:Today" // "2019-02-15T11:43:00-0600 Working on Go Library @home @personal +timertxt customTag1:Important! due:Today"
// "x 2019-02-15T10:00:00-0600 2019-02-15T06:00:00-0600 Creating Go Library Repo @home @personal +timertxt customTag1:Important! due:Today" // "x 2019-02-15T06:00:00-0600 2019-02-15T10:00:00-0600 Creating Go Library Repo @home @personal +timertxt customTag1:Important! due:Today"
func (timer Timer) String() string { func (timer Timer) String() string {
var text string var text string
if timer.Finished { if timer.Finished {
text += "x " text += "x "
if !timer.FinishDate.IsZero() {
text += fmt.Sprintf("%s ", timer.FinishDate.Format(DateLayout))
}
} }
text += fmt.Sprintf("%s ", timer.StartDate.Format(DateLayout)) text += fmt.Sprintf("%s ", timer.StartDate.Format(DateLayout))
text += timer.Notes if !timer.FinishDate.IsZero() {
text += fmt.Sprintf("%s", timer.FinishDate.Format(DateLayout))
}
if len(timer.Notes) > 0 {
text += " " + timer.Notes
}
if len(timer.Contexts) > 0 { if len(timer.Contexts) > 0 {
sort.Strings(timer.Contexts) sort.Strings(timer.Contexts)
for _, context := range timer.Contexts { for _, context := range timer.Contexts {
@ -79,6 +81,7 @@ func (timer Timer) String() string {
func NewTimer() *Timer { func NewTimer() *Timer {
timer := Timer{} timer := Timer{}
timer.StartDate = time.Now() timer.StartDate = time.Now()
timer.AdditionalTags = make(map[string]string)
return &timer return &timer
} }
@ -92,16 +95,19 @@ func ParseTimer(text string) (*Timer, error) {
// Check for finished // Check for finished
if originalParts[0] == "x" { if originalParts[0] == "x" {
timer.Finished = true timer.Finished = true
// If it's finished, there _must_ be a finished date originalParts = originalParts[1:]
if timer.FinishDate, err = time.Parse(DateLayout, originalParts[1]); err != nil {
return nil, errors.New("Timer marked finished, but failed to parse FinishDate: " + err.Error())
}
originalParts = originalParts[2:]
} }
if timer.StartDate, err = time.Parse(DateLayout, originalParts[0]); err != nil { if timer.StartDate, err = time.Parse(DateLayout, originalParts[0]); err != nil {
return nil, errors.New("Unable to parse StartDate: " + err.Error()) return nil, errors.New("Unable to parse StartDate: " + err.Error())
} }
originalParts = originalParts[1:] originalParts = originalParts[1:]
if timer.Finished {
// If it's finished, there _must_ be a finished date
if timer.FinishDate, err = time.Parse(DateLayout, originalParts[0]); err != nil {
return nil, errors.New("Timer marked finished, but failed to parse FinishDate: " + err.Error())
}
originalParts = originalParts[1:]
}
var notes []string var notes []string
for _, v := range originalParts { for _, v := range originalParts {
if strings.HasPrefix(v, "@") { if strings.HasPrefix(v, "@") {
@ -159,6 +165,18 @@ func (timer *Timer) Duration() time.Duration {
return end.Sub(timer.StartDate) return end.Sub(timer.StartDate)
} }
func (timer *Timer) StartsToday() bool {
currTime := time.Now()
dur := int64(currTime.Hour())*int64(time.Hour) + int64(currTime.Minute())*int64(time.Minute)
return int64(time.Since(timer.StartDate)) < dur
}
func (timer *Timer) EndsToday() bool {
currTime := time.Now()
dur := int64(currTime.Hour())*int64(time.Hour) + int64(currTime.Minute())*int64(time.Minute)
return int64(time.Since(timer.FinishDate)) < dur
}
func (timer *Timer) ActiveToday() bool { func (timer *Timer) ActiveToday() bool {
return timer.ActiveOnDay(time.Now()) return timer.ActiveOnDay(time.Now())
} }

View File

@ -63,16 +63,18 @@ func (timerlist *TimerList) String() string {
return ret return ret
} }
// AddTimer appends a Timer to the current TimerList and takes care to set the Timer.Id correctly // AddTimer prepends a Timer to the current TimerList and takes care to set the Timer.Id correctly
func (timerlist *TimerList) AddTimer(timer *Timer) { func (timerlist *TimerList) AddTimer(timer *Timer) {
timer.Id = 0 // The new timer is going to be id 1
timer.Id = 1
for _, t := range *timerlist { for _, t := range *timerlist {
if t.Id > timer.Id { // Everything else gets incremented
timer.Id = t.Id t.Id++
} }
} // Now prepend the timer to the slice
timer.Id += 1 *timerlist = append(*timerlist, Timer{})
*timerlist = append(*timerlist, *timer) copy((*timerlist)[1:], (*timerlist)[0:])
(*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.