gime/old/model.go

234 lines
5.4 KiB
Go
Raw Permalink Normal View History

2019-02-21 02:44:07 +00:00
package main
import (
"fmt"
2019-02-22 23:56:15 +00:00
"strings"
2019-02-21 02:44:07 +00:00
"time"
2021-03-02 17:10:29 +00:00
timertxt "git.bullercodeworks.com/brian/go-timertxt"
2019-02-21 02:44:07 +00:00
)
// If we have an active timer in timer.txt, return the most recent one
// Otherwise, check if we have a more recent completed timer in done.txt
// Return the most recent done from timer.txt or dont.txt
2021-03-02 17:05:44 +00:00
func (a *AppState) getMostRecentTimer() (*timertxt.Timer, error) {
work, wErr := a.TimerList.GetMostRecentTimer()
2021-03-09 15:10:34 +00:00
if wErr == nil && work.FinishDate.IsZero() {
return work, nil
}
if err := a.LoadDoneList(); err != nil {
return nil, err
}
done, dErr := a.DoneList.GetMostRecentTimer()
if dErr != nil {
return nil, dErr
}
if !done.FinishDate.IsZero() && work == nil || done.FinishDate.After(work.FinishDate) {
return done, nil
}
return work, nil
}
func (a *AppState) getFilteredTimerList(args []string) *timertxt.TimerList {
var includeArchive bool
var err error
start := time.Time{}
end := time.Now()
var contextFilters []string
var projectFilters []string
var allFilters []func(timertxt.Timer) bool
if len(args) > 0 {
contextFilters, args = getContextsFromSlice(args)
projectFilters, args = getProjectsFromSlice(args)
}
if len(args) > 0 {
if args[0] == "--a" {
includeArchive = true
args = args[1:]
}
}
if len(args) > 0 {
if start, err = parseFuzzyTime(args[0]); err != nil {
y, m, d := time.Now().Date()
start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location())
} else {
args = args[1:]
}
if len(args) > 0 {
if end, err = parseFuzzyTime(args[0]); err != nil {
y, m, d := time.Now().Date()
end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location())
} else {
args = args[1:]
}
2021-03-02 17:05:44 +00:00
}
}
list := a.TimerList.GetTimersInRange(start, end)
2021-03-02 17:05:44 +00:00
if includeArchive {
if err = a.LoadDoneList(); err != nil {
fmt.Println("Error loading done.txt entries")
fmt.Println(err.Error())
return nil
}
*list = append(*list, (*a.DoneList.GetTimersInRange(start, end))...)
}
if len(contextFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range contextFilters {
v = strings.TrimPrefix(v, "@")
if !t.HasContext(v) {
return false
}
}
return true
})
}
if len(projectFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range projectFilters {
v = strings.TrimPrefix(v, "+")
if !t.HasProject(v) {
return false
}
}
return true
})
}
2022-01-19 13:52:10 +00:00
doFilters := func(t *timertxt.Timer) bool {
for _, v := range allFilters {
2022-01-19 13:52:10 +00:00
if !v(*t) {
return false
}
2021-03-02 17:05:44 +00:00
}
// If we made it all the way down here, it matches
return true
2021-03-02 17:05:44 +00:00
}
return list.Filter(doFilters)
2021-03-02 17:05:44 +00:00
}
2019-02-21 02:44:07 +00:00
func (a *AppState) SetTimerFinished(id int, end time.Time) error {
var t *timertxt.Timer
var err error
if t, err = a.TimerList.GetTimer(id); err != nil {
return err
}
t.FinishDate = end
t.Finished = true
return nil
}
2019-02-22 23:56:15 +00:00
func (a *AppState) addTimer(timerString string) error {
t, err := timertxt.ParseTimer(timerString)
if err != nil {
return err
}
if t.StartDate.IsZero() {
t.StartDate = time.Now()
}
a.TimerList.AddTimer(t)
return a.WriteList()
}
func (a *AppState) archiveTimer(id int) error {
var err error
var timer *timertxt.Timer
if timer, err = a.TimerList.GetTimer(id); err != nil {
return err
}
if err := a.TimerList.ArchiveTimerToFile(*timer, app.getDoneFile()); err != nil {
return err
}
a.TimerList.RemoveTimer(*timer)
return a.WriteList()
}
func (a *AppState) unarchiveTimer(id int) error {
var err error
var timer *timertxt.Timer
if timer, err = a.DoneList.GetTimer(id); err != nil {
return err
}
a.TimerList.AddTimer(timer)
if err = a.WriteList(); err != nil {
return err
}
a.DoneList.RemoveTimer(*timer)
return a.WriteDoneList()
}
2019-02-21 02:44:07 +00:00
func (a *AppState) LoadTimerList() error {
var err error
var tl timertxt.TimerList
tl, err = timertxt.LoadFromFilename(a.getTimerFile())
tl.Sort(timertxt.SORT_UNFINISHED_START)
a.TimerList = &tl
return err
}
func (a *AppState) WriteList() error {
return a.TimerList.WriteToFilename(a.getTimerFile())
}
func (a *AppState) LoadDoneList() error {
var err error
var tl timertxt.TimerList
tl, err = timertxt.LoadFromFilename(a.getDoneFile())
a.DoneList = &tl
return err
}
func (a *AppState) WriteDoneList() error {
return a.DoneList.WriteToFilename(a.getDoneFile())
}
2019-02-22 23:56:15 +00:00
2022-01-19 13:52:10 +00:00
func (a *AppState) getFilterPredicate(filter string) func(*timertxt.Timer) bool {
var predicates []func(*timertxt.Timer) bool
2019-02-22 23:56:15 +00:00
// If none of the 'filter' is in upper-case, do a case-insensitive filter
checkCase := true
if strings.ToLower(filter) == filter {
checkCase = false
}
filterParts := strings.Split(filter, " ")
for _, part := range filterParts {
if strings.HasPrefix(part, "@") {
2022-01-19 13:52:10 +00:00
predicates = append(predicates, func(t *timertxt.Timer) bool {
2019-02-22 23:56:15 +00:00
for _, v := range t.Contexts {
if "@"+v == part {
return true
}
}
return false
})
} else if strings.HasPrefix(part, "+") {
2022-01-19 13:52:10 +00:00
predicates = append(predicates, func(t *timertxt.Timer) bool {
2019-02-22 23:56:15 +00:00
for _, v := range t.Projects {
if "+"+v == part {
return true
}
}
return false
})
} else {
2022-01-19 13:52:10 +00:00
predicates = append(predicates, func(t *timertxt.Timer) bool {
2019-02-22 23:56:15 +00:00
val := t.Original
if !checkCase {
val = strings.ToLower(t.Original)
}
return strings.Contains(val, part)
})
}
}
2022-01-19 13:52:10 +00:00
return func(t *timertxt.Timer) bool {
2019-02-22 23:56:15 +00:00
for _, v := range predicates {
if v(t) {
return true
}
}
return false
}
}