Updated TimerList struct

This commit is contained in:
Brian Buller 2023-01-12 06:04:17 -06:00
parent c98a4dea0c
commit c412f54294
10 changed files with 260 additions and 27 deletions

View File

@ -33,13 +33,13 @@ func (p *Program) GetTimerFilePath() string {
func (p *Program) LoadTimerList() error { func (p *Program) LoadTimerList() error {
var err error var err error
var tl timertxt.TimerList var tl *timertxt.TimerList
tl, err = timertxt.LoadFromFilename(p.timerPath) tl, err = timertxt.LoadFromFilename(p.timerPath)
if err != nil { if err != nil {
return err return err
} }
tl.Sort(timertxt.SORT_UNFINISHED_START) tl.Sort(timertxt.SORT_UNFINISHED_START)
p.TimerList = &tl p.TimerList = tl
return nil return nil
} }
@ -53,12 +53,12 @@ func (p *Program) GetDoneFilePath() string {
func (p *Program) LoadDoneList() error { func (p *Program) LoadDoneList() error {
var err error var err error
var tl timertxt.TimerList var tl *timertxt.TimerList
tl, err = timertxt.LoadFromFilename(p.donePath) tl, err = timertxt.LoadFromFilename(p.donePath)
if err != nil { if err != nil {
return err return err
} }
p.DoneList = &tl p.DoneList = tl
return nil return nil
} }
@ -110,14 +110,14 @@ func (p *Program) GetFilteredTimerList(args []string) *timertxt.TimerList {
} }
} }
list := p.TimerList.GetTimersInRange(start, end) list := p.TimerList.GetTimersInRange(start, end)
if includeArchive { if includeArchive {
if err = p.LoadDoneList(); err != nil { if err = p.LoadDoneList(); err != nil {
fmt.Println("Error loading done.txt entries") fmt.Println("Error loading done.txt entries")
fmt.Println(err.Error()) fmt.Println(err.Error())
return nil return nil
} }
*list = append(*list, (*p.DoneList.GetTimersInRange(start, end))...) doneList := p.DoneList.GetTimersInRange(start, end)
list.Combine(doneList)
} }
if len(contextFilters) > 0 { if len(contextFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool { allFilters = append(allFilters, func(t timertxt.Timer) bool {

View File

@ -91,7 +91,7 @@ func opI3Status(cmd *cobra.Command, args []string) error {
getListTotal := func(list *timertxt.TimerList) string { getListTotal := func(list *timertxt.TimerList) string {
var isActive bool var isActive bool
var total time.Duration var total time.Duration
for _, v := range *list { for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate) dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() { if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate) dur = time.Now().Sub(v.StartDate)

View File

@ -76,7 +76,7 @@ func opListTimers(cmd *cobra.Command, args []string) error {
list = list.Filter(filter) list = list.Filter(filter)
dayTotals := make(map[string]time.Duration) dayTotals := make(map[string]time.Duration)
for _, v := range *list { for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate) dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() { if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate) dur = time.Now().Sub(v.StartDate)
@ -84,7 +84,7 @@ func opListTimers(cmd *cobra.Command, args []string) error {
dayTotals[v.StartDate.Format("2006/01/02")] += dur dayTotals[v.StartDate.Format("2006/01/02")] += dur
} }
var oldDayStr, dayStr string var oldDayStr, dayStr string
for _, v := range *list { for _, v := range list.GetTimerSlice() {
oldDayStr = dayStr oldDayStr = dayStr
dayStr = v.StartDate.Format("2006/01/02") dayStr = v.StartDate.Format("2006/01/02")
if dayStr != oldDayStr { if dayStr != oldDayStr {

View File

@ -42,8 +42,9 @@ func opMod(cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0]) id, err := strconv.Atoi(args[0])
if err != nil { if err != nil {
// We didn't have a timer id, so try to modify the first active timer // We didn't have a timer id, so try to modify the first active timer
if len(*p.TimerList.GetActiveTimers()) > 0 { active := p.TimerList.GetActiveTimers().GetTimerSlice()
timer = (*p.TimerList.GetActiveTimers())[0] if len(active) > 0 {
timer = active[0]
} else { } else {
// And we don't have any active timers // And we don't have any active timers
return fmt.Errorf("No active timers, 'id' must be provided: %w", err) return fmt.Errorf("No active timers, 'id' must be provided: %w", err)

View File

@ -34,12 +34,13 @@ func opStatus(cmd *cobra.Command, args []string) error {
if err := p.LoadTimerList(); err != nil { if err := p.LoadTimerList(); err != nil {
return fmt.Errorf("Error loading timer list: %w", err) return fmt.Errorf("Error loading timer list: %w", err)
} }
if len(*p.TimerList.GetActiveTimers()) == 0 { active := p.TimerList.GetActiveTimers().GetTimerSlice()
if len(active) == 0 {
fmt.Println("No timers running") fmt.Println("No timers running")
return nil return nil
} }
var currDur time.Duration var currDur time.Duration
for _, v := range *p.TimerList { for _, v := range active {
if v.ActiveToday() { if v.ActiveToday() {
currDur += v.Duration() currDur += v.Duration()
} }
@ -47,7 +48,7 @@ func opStatus(cmd *cobra.Command, args []string) error {
d := util.Round(currDur) d := util.Round(currDur)
fmt.Printf("%s ( %.2f hrs )\n", time.Now().Format(time.Stamp), util.DurationToDecimal(d)) fmt.Printf("%s ( %.2f hrs )\n", time.Now().Format(time.Stamp), util.DurationToDecimal(d))
for _, v := range *p.TimerList.GetActiveTimers() { for _, v := range active {
fmt.Println(util.TimerToFriendlyString(v)) fmt.Println(util.TimerToFriendlyString(v))
} }
return nil return nil

View File

@ -51,7 +51,7 @@ func opStop(cmd *cobra.Command, args []string) error {
fmt.Println("Stopping at : " + end.Format(time.RFC3339)) fmt.Println("Stopping at : " + end.Format(time.RFC3339))
var timerIds []int var timerIds []int
if id == -1 { if id == -1 {
for _, v := range *p.TimerList.GetActiveTimers() { for _, v := range p.TimerList.GetActiveTimers().GetTimerSlice() {
timerIds = append(timerIds, v.Id) timerIds = append(timerIds, v.Id)
} }
} else { } else {

View File

@ -38,7 +38,7 @@ func opSwitch(cmd *cobra.Command, args []string) error {
var timerIds []int var timerIds []int
end := time.Now() end := time.Now()
// Stop all running timers and start a new one with the given args // Stop all running timers and start a new one with the given args
for _, v := range *p.TimerList.GetActiveTimers() { for _, v := range p.TimerList.GetActiveTimers().GetTimerSlice() {
timerIds = append(timerIds, v.Id) timerIds = append(timerIds, v.Id)
} }
fmt.Print("Stopping ", timerIds, "\n") fmt.Print("Stopping ", timerIds, "\n")

View File

@ -37,7 +37,7 @@ func opShowTimers(cmd *cobra.Command, args []string) error {
list := p.GetFilteredTimerList(args) list := p.GetFilteredTimerList(args)
var isActive bool var isActive bool
var total time.Duration var total time.Duration
for _, v := range *list { for _, v := range list.GetTimerSlice() {
dur := v.FinishDate.Sub(v.StartDate) dur := v.FinishDate.Sub(v.StartDate)
if v.FinishDate.IsZero() { if v.FinishDate.IsZero() {
dur = time.Now().Sub(v.StartDate) dur = time.Now().Sub(v.StartDate)

View File

@ -21,13 +21,19 @@ type listTimersScreen struct {
cursor int cursor int
fullList *timertxt.TimerList
timerList *timertxt.TimerList timerList *timertxt.TimerList
doneList *timertxt.TimerList doneList *timertxt.TimerList
timerFilterList *timertxt.TimerList timerFilterList *timertxt.TimerList
doneFilterList *timertxt.TimerList doneFilterList *timertxt.TimerList
filter string selected map[int]bool
inputDialog *widdles.InputDialog
filter string
partManager *PartManager
msg string msg string
err error err error
@ -38,9 +44,12 @@ type ListTimersMsg ScreenMsg
func NewListTimersScreen(u *Ui) *listTimersScreen { func NewListTimersScreen(u *Ui) *listTimersScreen {
w, h := termbox.Size() w, h := termbox.Size()
s := listTimersScreen{ s := listTimersScreen{
ui: u, ui: u,
menu: widdles.NewTopMenu(0, 0, 0), menu: widdles.NewTopMenu(0, 0, 0),
scrollbar: widdles.NewScrollbar(w-2, 2, 1, h-2), scrollbar: widdles.NewScrollbar(w-2, 2, 1, h-2),
selected: make(map[int]bool),
inputDialog: widdles.NewInputDialog("", ""),
partManager: NewPartManager("", 0, 0, 0, 0),
} }
return &s return &s
} }
@ -62,9 +71,17 @@ func (s *listTimersScreen) Init() wandle.Cmd {
s.menu.Measure() s.menu.Measure()
// Timer Lists // Timer Lists
s.timerList, s.doneList = s.ui.program.TimerList, s.ui.program.DoneList s.timerList, s.doneList = s.ui.program.TimerList, s.ui.program.DoneList
s.fullList = timertxt.NewTimerList()
s.fullList.AddTimers(s.timerList.GetTimerSlice())
s.fullList.AddTimers(s.doneList.GetTimerSlice())
s.timerFilterList, s.doneFilterList = s.timerList, s.doneList s.timerFilterList, s.doneFilterList = s.timerList, s.doneList
s.timerFilterList.Sort(timertxt.SORT_START_DATE_DESC) s.timerFilterList.Sort(timertxt.SORT_START_DATE_DESC)
s.doneFilterList.Sort(timertxt.SORT_START_DATE_DESC) s.doneFilterList.Sort(timertxt.SORT_START_DATE_DESC)
w, h := termbox.Size()
s.partManager.SetX(w / 4)
s.partManager.SetY(h / 4)
s.partManager.SetWidth(w / 2)
s.partManager.SetHeight(h / 2)
return nil return nil
} }
@ -80,9 +97,6 @@ func (s *listTimersScreen) Update(msg wandle.Msg) wandle.Cmd {
func (s *listTimersScreen) View(style wandle.Style) { func (s *listTimersScreen) View(style wandle.Style) {
_, h := termbox.Size() _, h := termbox.Size()
if s.menu.IsActive() {
s.menu.View(style)
}
y := 2 y := 2
wandle.Print(1, y, style.Bold(true), "Active Timers") wandle.Print(1, y, style.Bold(true), "Active Timers")
y++ y++
@ -94,7 +108,11 @@ func (s *listTimersScreen) View(style wandle.Style) {
if s.cursor == idx { if s.cursor == idx {
st = st.Invert() st = st.Invert()
} }
wandle.Print(1, y, st, "[ ]") if s.selected[idx] {
wandle.Print(1, y, st, "[✔] ")
} else {
wandle.Print(1, y, st, "[ ] ")
}
s.ViewTimer(5, y, st, tmr) s.ViewTimer(5, y, st, tmr)
y++ y++
} }
@ -102,19 +120,31 @@ func (s *listTimersScreen) View(style wandle.Style) {
wandle.Print(1, y, style.Bold(true), "Done Timers") wandle.Print(1, y, style.Bold(true), "Done Timers")
y++ y++
for idx, tmr := range s.doneFilterList.GetTimerSlice() { for idx, tmr := range s.doneFilterList.GetTimerSlice() {
if y > h-2 { if y > h-3 {
break break
} }
st := style st := style
if s.cursor == s.timerFilterList.Size()+idx { if s.cursor == s.timerFilterList.Size()+idx {
st = st.Invert() st = st.Invert()
} }
wandle.Print(1, y, st, "[ ]") if s.selected[s.timerFilterList.Size()+idx] {
wandle.Print(1, y, st, "[✔] ")
} else {
wandle.Print(1, y, st, "[ ] ")
}
s.ViewTimer(5, y, st, tmr) s.ViewTimer(5, y, st, tmr)
y++ y++
} }
wandle.Print(1, h-1, style, "[p]roject(+), [c]ontext(@), [t]ags(:)")
if len(s.selected) > 0 {
wandle.Print(39, h-1, style, fmt.Sprintf("(%d selected)", len(s.selected)))
}
s.scrollbar.View(style) s.scrollbar.View(style)
if s.menu.IsActive() {
s.menu.View(style)
}
s.partManager.View(style)
} }
func (s *listTimersScreen) ViewTimer(x, y int, style wandle.Style, tmr *timertxt.Timer) { func (s *listTimersScreen) ViewTimer(x, y int, style wandle.Style, tmr *timertxt.Timer) {
@ -127,18 +157,38 @@ func (s *listTimersScreen) ViewTimer(x, y int, style wandle.Style, tmr *timertxt
} }
func (s *listTimersScreen) handleTermboxEvent(msg termbox.Event) wandle.Cmd { func (s *listTimersScreen) handleTermboxEvent(msg termbox.Event) wandle.Cmd {
if pmRes := s.partManager.Update(msg); pmRes != nil {
return pmRes
}
if (msg.Type == termbox.EventKey && msg.Key == termbox.KeyEsc) || s.menu.IsActive() { if (msg.Type == termbox.EventKey && msg.Key == termbox.KeyEsc) || s.menu.IsActive() {
return s.menu.Update(msg) return s.menu.Update(msg)
} }
switch msg.Type { switch msg.Type {
case termbox.EventKey: case termbox.EventKey:
top := s.timerFilterList.Size() + s.doneFilterList.Size() - 2 top := s.timerFilterList.Size() + s.doneFilterList.Size() - 2
selected := len(s.selected)
var selTimer *timertxt.Timer
if selected == 0 {
if s.cursor < s.timerFilterList.Size() {
selTimer, s.err = s.timerFilterList.GetTimer(s.cursor)
} else {
selTimer, s.err = s.doneFilterList.GetTimer(s.cursor)
}
}
if msg.Key == termbox.KeyEnter { if msg.Key == termbox.KeyEnter {
// Edit the entry
/* /*
if s.cursor >= 0 && s.cursor < s.timerFilterList.Size()+s.doneFilterList.Size() { if s.cursor >= 0 && s.cursor < s.timerFilterList.Size()+s.doneFilterList.Size() {
} else { } else {
} }
*/ */
} else if msg.Key == termbox.KeySpace {
// (un)Select the entry
if v := s.selected[s.cursor]; v {
delete(s.selected, s.cursor)
} else {
s.selected[s.cursor] = true
}
} else if msg.Key == termbox.KeyArrowUp || msg.Ch == 'k' { } else if msg.Key == termbox.KeyArrowUp || msg.Ch == 'k' {
if s.cursor > 0 { if s.cursor > 0 {
s.cursor-- s.cursor--
@ -155,6 +205,44 @@ func (s *listTimersScreen) handleTermboxEvent(msg termbox.Event) wandle.Cmd {
return nil return nil
} else if msg.Ch == 'G' { } else if msg.Ch == 'G' {
s.cursor = top s.cursor = top
} else if msg.Ch == 't' {
// Edit tag(s)
tags := s.fullList.GetTagKeys()
s.partManager.SetOptions(tags)
if selected > 0 {
// TODO
//s.partManager.SetLabel(fmt.Sprintf("Manage Tags on %d Timers", selected))
} else {
s.partManager.SetLabel("Manage Tags for Timer")
s.partManager.SetType("Tag")
}
s.partManager.Show()
} else if msg.Ch == 'p' {
// Edit project(s)
projs := s.fullList.GetProjects()
s.partManager.SetOptions(projs)
if selected > 0 {
// TODO
//s.partManager.SetLabel(fmt.Sprintf("Manage Projects on %d Timers", selected))
} else {
s.partManager.SetLabel("Manage Projects for Timer")
s.partManager.SetType("Project")
s.partManager.SetValue(strings.Join(selTimer.Projects, " "))
}
s.partManager.Show()
} else if msg.Ch == 'c' {
// Edit context(s)
ctxts := s.fullList.GetContexts()
s.partManager.SetOptions(ctxts)
if selected > 0 {
// TODO
//s.partManager.SetLabel(fmt.Sprintf("Manage Contexts on %d Timers", selected))
} else {
s.partManager.SetLabel("Manage Contexts for Timer")
s.partManager.SetType("Context")
s.partManager.SetValue(strings.Join(selTimer.Contexts, " "))
}
s.partManager.Show()
} }
} }
return nil return nil

143
ui/widdle_manageparts.go Normal file
View File

@ -0,0 +1,143 @@
package ui
import (
"fmt"
"git.bullercodeworks.com/brian/wandle"
"git.bullercodeworks.com/brian/widdles"
"github.com/nsf/termbox-go"
)
/*
* PartManager is for adding/editing/removing groups of strings
*/
type PartManager struct {
active bool
visible bool
x, y, w, h int
label string
typeString string
options []string
selOptions map[int]bool
input *widdles.ToggleField
cursor int
}
var _ widdles.Widdle = (*PartManager)(nil)
func NewPartManager(label string, x, y, w, h int) *PartManager {
return &PartManager{
label: label,
selOptions: make(map[int]bool),
input: widdles.NewToggleField("Value:", "", x+1, y+2, w-2, 1),
}
}
func (w *PartManager) Init() wandle.Cmd { return nil }
func (w *PartManager) Update(msg wandle.Msg) wandle.Cmd {
if w.active {
if msg, ok := msg.(termbox.Event); ok {
return w.handleTermboxEvent(msg)
}
}
return nil
}
func (w *PartManager) handleTermboxEvent(msg termbox.Event) wandle.Cmd {
if w.cursor == 0 {
if opt := w.input.Update(msg); opt != nil {
return opt
} else {
//return w.handleKeyPress(msg)
}
}
return w.handleKeyPress(msg)
}
func (w *PartManager) handleKeyPress(msg termbox.Event) wandle.Cmd {
if msg.Key == termbox.KeyEsc {
return func() wandle.Msg {
w.Hide()
return nil
}
} else if msg.Ch == 'j' || msg.Key == termbox.KeyArrowDown {
if w.cursor < len(w.options) {
w.cursor = w.cursor + 1
}
} else if msg.Ch == 'k' || msg.Key == termbox.KeyArrowUp {
if w.cursor > 0 {
w.cursor = w.cursor - 1
}
}
if w.IsActive() {
return func() wandle.Msg { return nil }
}
return nil
}
func (w *PartManager) View(style wandle.Style) {
if w.visible {
wandle.TitledBorderFilled(w.label, w.x, w.y, w.x+w.w, w.y+w.h, style, wandle.BRD_SIMPLE)
st := style
if w.cursor == 0 {
st = st.Invert()
}
w.input.View(st)
y := w.input.GetY() + 1
wandle.Print(w.x+1, y, style, fmt.Sprintf("Add %s:", w.typeString))
y++
for i := range w.options {
st := style
if w.cursor-1 == i {
st = st.Invert()
}
wandle.Print(w.x+3, y, st, w.options[i])
y++
}
}
}
func (w *PartManager) IsActive() bool { return w.active }
func (w *PartManager) SetActive(b bool) { w.active = b }
func (w *PartManager) Focusable() bool { return true }
func (w *PartManager) SetX(x int) {
w.x = x
w.Measure()
}
func (w *PartManager) GetX() int { return w.x }
func (w *PartManager) SetY(y int) {
w.y = y
w.Measure()
}
func (w *PartManager) GetY() int { return w.y }
func (w *PartManager) SetHeight(h int) {
w.h = h
w.Measure()
}
func (w *PartManager) GetHeight() int { return w.h }
func (w *PartManager) SetWidth(v int) {
w.w = v
w.Measure()
}
func (w *PartManager) GetWidth() int { return w.w }
func (w *PartManager) Measure() {
w.input.SetX(w.x + 1)
w.input.SetY(w.y + 1)
w.input.SetWidth(w.w - 2)
w.input.SetHeight(1)
}
func (w *PartManager) SetLabel(lbl string) { w.label = lbl }
func (w *PartManager) SetType(tp string) { w.typeString = tp }
func (w *PartManager) SetValue(val string) { w.input.SetValue(val) }
func (w *PartManager) GetValue() string { return w.input.GetValue() }
func (w *PartManager) SetOptions(opts []string) { w.options = opts }
func (w *PartManager) SetVisible(is bool) { w.visible = is }
func (w *PartManager) IsVisible() bool { return w.visible }
func (w *PartManager) Show() {
w.SetVisible(true)
w.SetActive(true)
}
func (w *PartManager) Hide() {
w.SetVisible(false)
w.SetActive(false)
}