UI Mode Work & Archiving

This commit is contained in:
Brian Buller 2019-02-22 17:56:15 -06:00
parent cf80d56e20
commit f4af61386e
3 changed files with 102 additions and 141 deletions

View File

@ -21,6 +21,8 @@ type AppState struct {
OpFuncs map[string]func([]string) int
TimerList *timertxt.TimerList
DoneList *timertxt.TimerList
screens []Screen
}
func NewApp() *AppState {
@ -35,10 +37,14 @@ func NewApp() *AppState {
return app
}
func (a *AppState) filterList(list *timertxt.TimerList, filter string) *timertxt.TimerList {
return list.Filter(a.getFilterPredicate(filter))
}
func (a *AppState) run(parms []string) int {
if len(parms) == 0 || parms[0] == "ui" {
// UI Mode
//return uiLoop()
return uiLoop()
}
if fn, ok := a.OpFuncs[parms[0]]; ok {
return fn(parms[1:])
@ -64,6 +70,14 @@ func (a *AppState) addOperation(name string, desc []string, fn func([]string) in
a.OpFuncs[name] = fn
}
func (a *AppState) getTimerString(timer timertxt.Timer) string {
return fmt.Sprintf("%d. %s", timer.Id, strings.TrimPrefix(timer.String(), "x "))
}
func (a *AppState) getDoneTimerString(timer timertxt.Timer) string {
return fmt.Sprintf("0. %s", timer.Id, timer.String())
}
func (a *AppState) doVersionCheck() {
confVer, _ := a.config.GetInt("version")
for confVer < a.Version {

View File

@ -1,6 +1,7 @@
package main
import (
"strings"
"time"
timertxt "git.bullercodeworks.com/brian/go-timertxt"
@ -17,6 +18,45 @@ func (a *AppState) SetTimerFinished(id int, end time.Time) error {
return nil
}
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()
}
func (a *AppState) LoadTimerList() error {
var err error
var tl timertxt.TimerList
@ -30,7 +70,6 @@ func (a *AppState) WriteList() error {
return a.TimerList.WriteToFilename(a.getTimerFile())
}
/*
func (a *AppState) LoadDoneList() error {
var err error
var tl timertxt.TimerList
@ -42,4 +81,50 @@ func (a *AppState) LoadDoneList() error {
func (a *AppState) WriteDoneList() error {
return a.DoneList.WriteToFilename(a.getDoneFile())
}
*/
func (a *AppState) getFilterPredicate(filter string) func(timertxt.Timer) bool {
var predicates []func(timertxt.Timer) bool
// 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, "@") {
predicates = append(predicates, func(t timertxt.Timer) bool {
for _, v := range t.Contexts {
if "@"+v == part {
return true
}
}
return false
})
} else if strings.HasPrefix(part, "+") {
predicates = append(predicates, func(t timertxt.Timer) bool {
for _, v := range t.Projects {
if "+"+v == part {
return true
}
}
return false
})
} else {
predicates = append(predicates, func(t timertxt.Timer) bool {
val := t.Original
if !checkCase {
val = strings.ToLower(t.Original)
}
return strings.Contains(val, part)
})
}
}
return func(t timertxt.Timer) bool {
for _, v := range predicates {
if v(t) {
return true
}
}
return false
}
}

View File

@ -1,138 +0,0 @@
package main
import (
"fmt"
"strconv"
"time"
timertxt "git.bullercodeworks.com/brian/go-timertxt"
)
func (a *AppState) opStatus(args []string) int {
fmt.Println("Current Time: " + time.Now().Format(time.Stamp))
if len(*a.TimerList.GetActiveTimers()) == 0 {
fmt.Println("No timers running")
return 0
}
for _, v := range *a.TimerList.GetActiveTimers() {
fmt.Println(timerToFriendlyString(&v))
}
return 0
}
func (a *AppState) opListTimers(args []string) int {
// By default only list today
// Format:
// 2019/02/20 (9.25)
// start - end [ contexts ] [ projects ]
// ...
dayTotals := make(map[string]time.Duration)
for _, v := range *a.TimerList {
dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate)
}
dayTotals[v.StartDate.Format("2006/01/02")] += dur
}
var oldDayStr, dayStr string
for _, v := range *a.TimerList {
oldDayStr = dayStr
dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr {
wrkDur := dayTotals[dayStr].Round(GetRoundToDuration())
fmtStr := dayStr + " ( %.2f )\n"
fmt.Printf(fmtStr, DurationToDecimal(wrkDur))
}
fmt.Println(" " + timerToFriendlyString(&v))
}
return 0
}
func (a *AppState) opStartTimer(args []string) int {
var contexts, projects []string
t := timertxt.NewTimer()
contexts, args = getContextsFromSlice(args)
projects, args = getProjectsFromSlice(args)
if len(args) > 0 {
if start, err := parseFuzzyTime(args[0]); err == nil {
t.StartDate = start
args = args[1:]
}
}
t.Contexts = contexts
t.Projects = projects
a.TimerList.AddTimer(t)
if err := a.WriteList(); err != nil {
fmt.Println(err.Error())
return 1
}
return 0
}
func (a *AppState) opStopTimer(args []string) int {
var err error
var wrk time.Time
end := time.Now()
id := -1
if len(args) > 0 {
if wrk, err = parseFuzzyTime(args[0]); err != nil {
id, err = strconv.Atoi(args[0])
} else {
end = wrk
args = args[1:]
}
}
fmt.Println("Stopping at : " + end.Format(time.RFC3339))
var timerIds []int
if id == -1 {
for _, v := range *a.TimerList.GetActiveTimers() {
timerIds = append(timerIds, v.Id)
}
} else {
timerIds = append(timerIds, id)
}
for _, v := range timerIds {
var stopped *timertxt.Timer
if stopped, err = a.TimerList.GetTimer(v); err != nil {
fmt.Println(err.Error())
}
if err = a.SetTimerFinished(v, end); err != nil {
fmt.Println(err.Error())
continue
}
fmt.Println("Stopped Timer:", timerToFriendlyString(stopped))
}
if err = a.WriteList(); err != nil {
fmt.Println(err.Error())
return 1
}
return 0
}
func (a *AppState) opState(args []string) int {
return 0
}
func (a *AppState) opFuzzyParse(args []string) int {
if len(args) > 0 {
if start, err := parseFuzzyTime(args[0]); err == nil {
fmt.Println(start.Format(time.RFC3339))
} else {
fmt.Println(err.Error())
}
}
return 0
}
func (a *AppState) opPrintUsage(args []string) int {
for _, v := range a.ValidOperations {
for _, vv := range v {
fmt.Println(" " + vv)
fmt.Println("")
}
}
return 0
}