termui/events.go

148 lines
2.4 KiB
Go
Raw Normal View History

// Copyright 2015 Zack Guo <gizak@icloud.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
//
// Portions of this file uses [termbox-go](https://github.com/nsf/termbox-go/blob/54b74d087b7c397c402d0e3b66d2ccb6eaf5c2b4/api_common.go)
// by [authors](https://github.com/nsf/termbox-go/blob/master/AUTHORS)
// under [license](https://github.com/nsf/termbox-go/blob/master/LICENSE)
package termui
2015-08-20 03:22:53 +08:00
import (
"strings"
2015-08-31 11:03:47 +08:00
"sync"
"time"
2015-08-20 03:22:53 +08:00
"github.com/nsf/termbox-go"
)
2015-08-31 11:03:47 +08:00
type Event struct {
Type string
Path string
From string
To string
Data interface{}
Time int64
}
2015-08-31 11:03:47 +08:00
var sysevt struct {
chs []chan Event
}
2015-08-31 11:03:47 +08:00
func newSysEvtFromTb(e termbox.Event) Event {
ne := Event{From: "/sys", Time: time.Now().Unix()}
return ne
}
2015-08-31 11:03:47 +08:00
func hookSysEvt() {
sysevt.chs = make([]chan Event, 0)
for {
e := termbox.PollEvent()
for _, c := range sysevt.chs {
// shorten?
go func(ch chan Event, ev Event) { ch <- ev }(c, newSysEvtFromTb(e))
}
2015-08-31 11:03:47 +08:00
}
}
2015-08-20 03:22:53 +08:00
2015-08-31 11:03:47 +08:00
func NewSysEvtCh() chan Event {
ec := make(chan Event)
sysevt.chs = append(sysevt.chs, ec)
return ec
2015-08-09 07:07:32 +08:00
}
2015-08-31 11:03:47 +08:00
/*
2015-08-09 07:07:32 +08:00
type evtCtl struct {
in chan Event
out chan Event
suspend chan int
recover chan int
close chan int
}
2015-08-09 07:07:32 +08:00
func newEvtCtl() evtCtl {
ec := evtCtl{}
ec.in = make(chan Event)
ec.suspend = make(chan int)
ec.recover = make(chan int)
ec.close = make(chan int)
ec.out = make(chan Event)
return ec
}
2015-08-31 11:03:47 +08:00
*/
//
type EvtStream struct {
srcMap map[string]chan Event
stream chan Event
cache map[string][]func(Event)
wg sync.WaitGroup
Handlers map[string]func(Event)
}
func NewEvtStream() *EvtStream {
return &EvtStream{
srcMap: make(map[string]chan Event),
2015-08-09 07:07:32 +08:00
stream: make(chan Event),
}
}
2015-08-31 11:03:47 +08:00
func (es *EvtStream) Init() {
go func() {
es.wg.Wait()
close(es.stream)
}()
}
2015-08-20 03:22:53 +08:00
// a: /sys/bell
// b: /sys
// score: 1
//
// a: /sys
// b: /usr
// score: -1
//
// a: /sys
// b: /
// score: 0
func MatchScore(a, b string) int {
sa := strings.Split(a, "/")
sb := strings.Split(b, "/")
score := -1
for i, s := range sa {
if i >= len(sb) {
break
}
if s != sb[i] {
return -1
}
score++
}
return score
}
2015-08-31 11:03:47 +08:00
func (es *EvtStream) Merge(ec chan Event) {
es.wg.Add(1)
go func(a chan Event) {
for n := range ec {
es.stream <- n
}
wg.Done()
}(ec)
}
2015-08-09 07:07:32 +08:00
/*
func (es *EvtStream) hookup() {
2015-08-09 07:07:32 +08:00
}
func (es EvtStream) Subscribe(uri string) chan Event {
}
*/