gime/ui/list_timers.go

169 lines
3.8 KiB
Go
Raw Normal View History

2023-01-05 17:18:08 +00:00
package ui
import (
"fmt"
"strings"
"time"
"git.bullercodeworks.com/brian/gime/util"
"git.bullercodeworks.com/brian/go-timertxt"
"git.bullercodeworks.com/brian/wandle"
"git.bullercodeworks.com/brian/widdles"
"github.com/nsf/termbox-go"
)
type listTimersScreen struct {
ui *Ui
initialized bool
menu *widdles.TopMenu
scrollbar *widdles.Scrollbar
cursor int
timerList *timertxt.TimerList
doneList *timertxt.TimerList
timerFilterList *timertxt.TimerList
doneFilterList *timertxt.TimerList
filter string
msg string
err error
}
type ListTimersMsg ScreenMsg
func NewListTimersScreen(u *Ui) *listTimersScreen {
w, h := termbox.Size()
s := listTimersScreen{
ui: u,
menu: widdles.NewTopMenu(0, 0, 0),
scrollbar: widdles.NewScrollbar(w-2, 2, 1, h-2),
}
return &s
}
func (s *listTimersScreen) Init() wandle.Cmd {
if s.initialized {
return nil
}
s.initialized = true
// Set up the top menu
fileMenu := s.menu.NewSubMenu("File")
settingsOption := widdles.NewMenuItem("Settings")
settingsOption.SetCommand(s.ui.GotoScreen(SettingsId))
fileMenu.AddOption(settingsOption)
quitOption := widdles.NewMenuItem("Quit")
quitOption.Hotkey = termbox.KeyCtrlC
quitOption.SetCommand(func() wandle.Msg { return wandle.Quit() })
fileMenu.AddOption(quitOption)
s.menu.Measure()
// Timer Lists
s.timerList, s.doneList = s.ui.program.TimerList, s.ui.program.DoneList
s.timerFilterList, s.doneFilterList = s.timerList, s.doneList
s.timerFilterList.Sort(timertxt.SORT_START_DATE_DESC)
s.doneFilterList.Sort(timertxt.SORT_START_DATE_DESC)
return nil
}
func (s *listTimersScreen) Update(msg wandle.Msg) wandle.Cmd {
switch msg := msg.(type) {
case ScreenMsg:
case termbox.Event:
return s.handleTermboxEvent(msg)
}
return nil
}
func (s *listTimersScreen) View(style wandle.Style) {
_, h := termbox.Size()
if s.menu.IsActive() {
s.menu.View(style)
}
y := 2
wandle.Print(1, y, style.Bold(true), "Active Timers")
y++
for idx, tmr := range s.timerFilterList.GetTimerSlice() {
if y > h-2 {
break
}
st := style
if s.cursor == idx {
st = st.Invert()
}
wandle.Print(1, y, st, "[ ]")
s.ViewTimer(5, y, st, tmr)
y++
}
y++
wandle.Print(1, y, style.Bold(true), "Done Timers")
y++
for idx, tmr := range s.doneFilterList.GetTimerSlice() {
if y > h-2 {
break
}
st := style
if s.cursor == s.timerFilterList.Size()+idx {
st = st.Invert()
}
wandle.Print(1, y, st, "[ ]")
s.ViewTimer(5, y, st, tmr)
y++
}
s.scrollbar.View(style)
}
func (s *listTimersScreen) ViewTimer(x, y int, style wandle.Style, tmr *timertxt.Timer) {
var tags []string
for _, k := range util.SortedTagKeyList(tmr.AdditionalTags) {
tags = append(tags, fmt.Sprintf("%s:%s", k, tmr.AdditionalTags[k]))
}
wandle.Print(x, y, style, fmt.Sprintf("%s %s %s %s %s", tmr.StartDate.Format(time.Stamp), tmr.Duration(), tmr.Contexts, tmr.Projects, strings.Join(tags, "; ")))
}
func (s *listTimersScreen) handleTermboxEvent(msg termbox.Event) wandle.Cmd {
if (msg.Type == termbox.EventKey && msg.Key == termbox.KeyEsc) || s.menu.IsActive() {
return s.menu.Update(msg)
}
switch msg.Type {
case termbox.EventKey:
top := s.timerFilterList.Size() + s.doneFilterList.Size() - 2
if msg.Key == termbox.KeyEnter {
/*
if s.cursor >= 0 && s.cursor < s.timerFilterList.Size()+s.doneFilterList.Size() {
} else {
}
*/
} else if msg.Key == termbox.KeyArrowUp || msg.Ch == 'k' {
if s.cursor > 0 {
s.cursor--
} else {
s.cursor = 0
}
return nil
} else if msg.Key == termbox.KeyArrowDown || msg.Ch == 'j' {
if s.cursor < top {
s.cursor++
} else {
s.cursor = top
}
return nil
} else if msg.Ch == 'G' {
s.cursor = top
}
}
return nil
}
func (s *listTimersScreen) gotoSettingsScreen() wandle.Msg {
return ScreenMsg{
source: ListTimersId,
command: CmdGotoSettings,
}
}