2019-02-15 18:46:50 +00:00
|
|
|
package timertxt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-02-22 23:31:44 +00:00
|
|
|
"errors"
|
2019-02-15 18:46:50 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2023-01-12 12:04:53 +00:00
|
|
|
"sort"
|
2019-02-15 18:46:50 +00:00
|
|
|
"strings"
|
2019-02-25 22:04:10 +00:00
|
|
|
"time"
|
2019-02-15 18:46:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TimerList represents a list of timer.txt timer entries.
|
|
|
|
// It is usually loasded from a whole timer.txt file.
|
2023-01-12 12:04:53 +00:00
|
|
|
type TimerList struct {
|
|
|
|
timers []*Timer
|
|
|
|
sortFlag int
|
2019-02-22 23:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:04:53 +00:00
|
|
|
// NewTimerList creates a new empty TimerList.
|
|
|
|
func NewTimerList() *TimerList { return &TimerList{} }
|
|
|
|
func (timerlist *TimerList) Size() int { return len(timerlist.timers) }
|
|
|
|
func (timerlist *TimerList) GetTimerSlice() []*Timer { return timerlist.timers }
|
2022-01-17 15:55:44 +00:00
|
|
|
|
2023-08-03 21:02:43 +00:00
|
|
|
func (timerlist *TimerList) Contains(t *Timer) bool {
|
|
|
|
for _, tmr := range timerlist.timers {
|
|
|
|
if tmr == t {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (timerlist *TimerList) GetActiveOrMostRecent() (*Timer, error) {
|
|
|
|
var found *Timer
|
|
|
|
var latest time.Time
|
|
|
|
activeTimers := timerlist.Filter(func(t *Timer) bool { return !t.Finished })
|
|
|
|
if len(activeTimers.timers) > 0 {
|
|
|
|
return activeTimers.GetMostRecentTimer()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range timerlist.timers {
|
|
|
|
if t.FinishDate.IsZero() {
|
|
|
|
if t.StartDate.After(latest) {
|
|
|
|
found = t
|
|
|
|
latest = t.StartDate
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if t.FinishDate.After(latest) {
|
|
|
|
latest = t.FinishDate
|
|
|
|
found = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found == nil {
|
|
|
|
return nil, errors.New("No timer found")
|
|
|
|
}
|
|
|
|
return found, nil
|
|
|
|
}
|
2021-03-02 17:06:37 +00:00
|
|
|
func (timerlist *TimerList) GetMostRecentTimer() (*Timer, error) {
|
|
|
|
var found *Timer
|
|
|
|
var latest time.Time
|
2023-01-12 12:04:53 +00:00
|
|
|
for _, t := range timerlist.timers {
|
2021-03-02 17:06:37 +00:00
|
|
|
if t.FinishDate.IsZero() {
|
|
|
|
if t.StartDate.After(latest) {
|
|
|
|
found = t
|
|
|
|
latest = t.StartDate
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if t.FinishDate.After(latest) {
|
|
|
|
latest = t.FinishDate
|
|
|
|
found = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found == nil {
|
|
|
|
return nil, errors.New("No timer found")
|
|
|
|
}
|
|
|
|
return found, nil
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:31:44 +00:00
|
|
|
func (timerlist *TimerList) GetTimersInRange(start, end time.Time) *TimerList {
|
2022-01-17 15:55:44 +00:00
|
|
|
fltr := func(t *Timer) bool {
|
2019-02-25 22:04:10 +00:00
|
|
|
if t.StartDate.Before(end) && t.StartDate.After(start) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if t.FinishDate.Before(end) && t.FinishDate.After(start) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-02-22 23:31:44 +00:00
|
|
|
}
|
2019-02-25 22:04:10 +00:00
|
|
|
return timerlist.Filter(fltr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (timerlist *TimerList) GetTimersWithContext(context string) *TimerList {
|
2022-01-17 15:55:44 +00:00
|
|
|
return timerlist.Filter(func(t *Timer) bool {
|
2019-02-25 22:04:10 +00:00
|
|
|
return t.HasContext(context)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (timerlist *TimerList) GetTimersWithProject(project string) *TimerList {
|
2022-01-17 15:55:44 +00:00
|
|
|
return timerlist.Filter(func(t *Timer) bool {
|
2019-02-25 22:04:10 +00:00
|
|
|
return t.HasProject(project)
|
|
|
|
})
|
2019-02-22 23:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:04:53 +00:00
|
|
|
func (timerlist *TimerList) GetContexts() []string {
|
|
|
|
var ret []string
|
|
|
|
added := make(map[string]bool)
|
|
|
|
for _, tmr := range timerlist.timers {
|
|
|
|
for _, c := range tmr.Contexts {
|
|
|
|
if !added[c] {
|
|
|
|
ret = append(ret, c)
|
|
|
|
added[c] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
func (timerlist *TimerList) GetProjects() []string {
|
|
|
|
var ret []string
|
|
|
|
added := make(map[string]bool)
|
|
|
|
for _, tmr := range timerlist.timers {
|
|
|
|
for _, p := range tmr.Projects {
|
|
|
|
if !added[p] {
|
|
|
|
ret = append(ret, p)
|
|
|
|
added[p] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
func (timerlist *TimerList) GetTagKVList() []string {
|
|
|
|
var ret []string
|
|
|
|
added := make(map[string]bool)
|
|
|
|
for _, tmr := range timerlist.timers {
|
|
|
|
for k, v := range tmr.AdditionalTags {
|
|
|
|
tag := fmt.Sprintf("%s:%s", k, v)
|
|
|
|
if !added[tag] {
|
|
|
|
ret = append(ret, tag)
|
|
|
|
added[tag] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
func (timerlist *TimerList) GetTagKeys() []string {
|
|
|
|
var ret []string
|
|
|
|
added := make(map[string]bool)
|
|
|
|
for _, tmr := range timerlist.timers {
|
|
|
|
for k := range tmr.AdditionalTags {
|
|
|
|
if !added[k] {
|
|
|
|
ret = append(ret, k)
|
|
|
|
added[k] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
func (timerlist *TimerList) GetTagValuesForKey(key string) []string {
|
|
|
|
var ret []string
|
|
|
|
added := make(map[string]bool)
|
|
|
|
for _, tmr := range timerlist.timers {
|
|
|
|
if v, ok := tmr.AdditionalTags[key]; ok && !added[v] {
|
|
|
|
ret = append(ret, v)
|
|
|
|
added[v] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:31:44 +00:00
|
|
|
func (timerlist *TimerList) GetActiveTimers() *TimerList {
|
|
|
|
t := *NewTimerList()
|
2023-01-12 12:04:53 +00:00
|
|
|
for _, v := range timerlist.timers {
|
2019-02-22 23:31:44 +00:00
|
|
|
if v.FinishDate.IsZero() {
|
2023-01-12 12:04:53 +00:00
|
|
|
t.timers = append(t.timers, v)
|
2019-02-22 23:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &t
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a complete list of timers in timer.txt format.
|
2019-02-22 23:31:44 +00:00
|
|
|
func (timerlist *TimerList) String() string {
|
2019-02-15 18:46:50 +00:00
|
|
|
var ret string
|
2023-01-12 12:04:53 +00:00
|
|
|
for _, timer := range timerlist.timers {
|
2019-02-15 18:46:50 +00:00
|
|
|
ret += fmt.Sprintf("%s\n", timer.String())
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:10:17 +00:00
|
|
|
// AddTimer prepends a Timer to the current TimerList and takes care to set the Timer.Id correctly
|
2019-02-15 18:46:50 +00:00
|
|
|
func (timerlist *TimerList) AddTimer(timer *Timer) {
|
2023-01-12 12:04:53 +00:00
|
|
|
timerlist.timers = append(timerlist.timers, timer)
|
|
|
|
timerlist.refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTimers adds all passed in timers to the list, sorts the list, then updates the Timer.Id values.
|
|
|
|
func (timerlist *TimerList) AddTimers(timers []*Timer) {
|
|
|
|
timerlist.timers = append(timerlist.timers, timers...)
|
|
|
|
timerlist.Sort(SORT_START_DATE_ASC)
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
func (timerlist *TimerList) Combine(other *TimerList) { timerlist.AddTimers(other.timers) }
|
2019-02-15 18:46:50 +00:00
|
|
|
|
|
|
|
// 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) {
|
2023-01-12 12:04:53 +00:00
|
|
|
for i := range timerlist.timers {
|
|
|
|
if timerlist.timers[i].Id == id {
|
|
|
|
return timerlist.timers[i], nil
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 23:31:44 +00:00
|
|
|
return nil, errors.New("timer not found")
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTimerById removes any Timer with given Timer 'id' from the TimerList.
|
|
|
|
// Returns an error if no Timer was removed.
|
|
|
|
func (timerlist *TimerList) RemoveTimerById(id int) error {
|
|
|
|
found := false
|
2023-01-12 12:04:53 +00:00
|
|
|
var remIdx int
|
|
|
|
var t *Timer
|
|
|
|
for remIdx, t = range timerlist.timers {
|
|
|
|
if t.Id == id {
|
2019-02-15 18:46:50 +00:00
|
|
|
found = true
|
2023-01-12 12:04:53 +00:00
|
|
|
break
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2019-02-22 23:31:44 +00:00
|
|
|
return errors.New("timer not found")
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
timerlist.timers = append(timerlist.timers[:remIdx], timerlist.timers[remIdx+1:]...)
|
|
|
|
timerlist.refresh()
|
2019-02-15 18:46:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTimer removes any Timer from the TimerList with the same String representation as the given Timer.
|
|
|
|
// Returns an error if no Timer was removed.
|
|
|
|
func (timerlist *TimerList) RemoveTimer(timer Timer) error {
|
|
|
|
found := false
|
2023-01-12 12:04:53 +00:00
|
|
|
var remIdx int
|
|
|
|
var t *Timer
|
|
|
|
for remIdx, t = range timerlist.timers {
|
|
|
|
if t.String() == timer.String() {
|
2019-02-15 18:46:50 +00:00
|
|
|
found = true
|
2023-01-12 12:04:53 +00:00
|
|
|
break
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return errors.New("timer not found")
|
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
timerlist.timers = append(timerlist.timers[:remIdx], timerlist.timers[remIdx+1:]...)
|
|
|
|
timerlist.refresh()
|
2019-02-15 18:46:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:04:10 +00:00
|
|
|
// ArchiveTimerToFile removes the timer from the active list and concatenates it to
|
|
|
|
// the passed in filename
|
|
|
|
// Return an err if any part of that fails
|
|
|
|
func (timerlist *TimerList) ArchiveTimerToFile(timer Timer, filename string) error {
|
|
|
|
if err := timerlist.RemoveTimer(timer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(timer.String() + "\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:46:50 +00:00
|
|
|
// 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.
|
2022-01-17 15:55:44 +00:00
|
|
|
func (timerlist *TimerList) Filter(predicate func(*Timer) bool) *TimerList {
|
2019-02-15 18:46:50 +00:00
|
|
|
var newList TimerList
|
2023-01-12 12:04:53 +00:00
|
|
|
for _, t := range timerlist.timers {
|
2019-02-15 18:46:50 +00:00
|
|
|
if predicate(t) {
|
2023-01-12 12:04:53 +00:00
|
|
|
newList.timers = append(newList.timers, t)
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &newList
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-01-12 12:04:53 +00:00
|
|
|
timerlist.timers = []*Timer{} // Empty timerlist
|
2019-02-15 18:46:50 +00:00
|
|
|
timerId := 1
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
text := strings.Trim(scanner.Text(), "\t\n\r") // Read Line
|
|
|
|
// Ignore blank lines
|
|
|
|
if text == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
timer, err := ParseTimer(text)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timer.Id = timerId
|
|
|
|
timerId++
|
2023-01-12 12:04:53 +00:00
|
|
|
timerlist.timers = append(timerlist.timers, timer)
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
timerlist.refresh()
|
2019-02-15 18:46:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-22 23:31:44 +00:00
|
|
|
// WriteToFile writes a TimerList to *os.File
|
|
|
|
func (timerlist *TimerList) WriteToFile(file *os.File) error {
|
|
|
|
writer := bufio.NewWriter(file)
|
|
|
|
_, err := writer.WriteString(timerlist.String())
|
|
|
|
writer.Flush()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-18 19:57:43 +00:00
|
|
|
// LoadFromFilename loads a TimerList from the filename.
|
2019-02-15 18:46:50 +00:00
|
|
|
func (timerlist *TimerList) LoadFromFilename(filename string) error {
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
return timerlist.LoadFromFile(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToFilename writes a TimerList to the specified file (most likely called "timer.txt").
|
|
|
|
func (timerlist *TimerList) WriteToFilename(filename string) error {
|
|
|
|
return ioutil.WriteFile(filename, []byte(timerlist.String()), 0640)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFile loads and returns a TimerList from *os.File.
|
2023-01-12 12:04:53 +00:00
|
|
|
func LoadFromFile(file *os.File) (*TimerList, error) {
|
2019-02-15 18:46:50 +00:00
|
|
|
timerlist := TimerList{}
|
|
|
|
if err := timerlist.LoadFromFile(file); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
return &timerlist, nil
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToFile writes a TimerList to *os.File.
|
|
|
|
func WriteToFile(timerlist *TimerList, file *os.File) error {
|
|
|
|
return timerlist.WriteToFile(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFilename loads and returns a TimerList from a file (most likely called "timer.txt")
|
2023-01-12 12:04:53 +00:00
|
|
|
func LoadFromFilename(filename string) (*TimerList, error) {
|
2019-02-15 18:46:50 +00:00
|
|
|
timerlist := TimerList{}
|
|
|
|
if err := timerlist.LoadFromFilename(filename); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-12 12:04:53 +00:00
|
|
|
return &timerlist, nil
|
2019-02-15 18:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToFilename write a TimerList to the specified file (most likely called "timer.txt")
|
|
|
|
func WriteToFilename(timerlist *TimerList, filename string) error {
|
|
|
|
return timerlist.WriteToFilename(filename)
|
|
|
|
}
|