Some updates
This commit is contained in:
91
timemap.go
Normal file
91
timemap.go
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
Copyright © Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// TODO: Support recurring jobs
|
||||
type TimeMap struct {
|
||||
Events map[time.Time]func(*tcell.EventTime) bool
|
||||
Jobs map[time.Duration]func(*tcell.EventTime) bool
|
||||
}
|
||||
|
||||
func BlankTimeMap() *TimeMap {
|
||||
return &TimeMap{
|
||||
Events: make(map[time.Time]func(*tcell.EventTime) bool),
|
||||
Jobs: make(map[time.Duration]func(*tcell.EventTime) bool),
|
||||
}
|
||||
}
|
||||
|
||||
func NewEventMap(m map[time.Time]func(*tcell.EventTime) bool) TimeMap {
|
||||
return TimeMap{
|
||||
Events: m,
|
||||
Jobs: make(map[time.Duration]func(*tcell.EventTime) bool),
|
||||
}
|
||||
}
|
||||
|
||||
func NewJobMap(m map[time.Duration]func(*tcell.EventTime) bool) TimeMap {
|
||||
return TimeMap{
|
||||
Events: make(map[time.Time]func(*tcell.EventTime) bool),
|
||||
Jobs: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TimeMap) Merge(tm TimeMap) {
|
||||
for t, v := range tm.Events {
|
||||
m.Events[t] = v
|
||||
}
|
||||
for j, v := range tm.Jobs {
|
||||
m.Jobs[j] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TimeMap) Add(e time.Time, do func(*tcell.EventTime) bool) { m.Events[e] = do }
|
||||
func (m *TimeMap) Remove(e time.Time) { delete(m.Events, e) }
|
||||
|
||||
func (m *TimeMap) AddAll(all map[time.Time]func(*tcell.EventTime) bool) {
|
||||
for t, v := range all {
|
||||
m.Add(t, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TimeMap) AddJob(d time.Duration, do func(*tcell.EventTime) bool) { m.Jobs[d] = do }
|
||||
func (m *TimeMap) RemoveJob(d time.Duration) { delete(m.Jobs, d) }
|
||||
func (m *TimeMap) AddJobs(all map[time.Duration]func(*tcell.EventTime) bool) {
|
||||
for k, v := range all {
|
||||
m.AddJob(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *TimeMap) Handle(ev *tcell.EventTime) bool {
|
||||
var ranSomething bool
|
||||
for t, do := range m.Events {
|
||||
if ev.When() == t {
|
||||
ranSomething = ranSomething || do(ev)
|
||||
}
|
||||
}
|
||||
return ranSomething
|
||||
}
|
||||
Reference in New Issue
Block a user