2015-04-09 12:21:36 +08:00
|
|
|
// 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)
|
|
|
|
|
2015-04-09 00:06:47 +08:00
|
|
|
package termui
|
|
|
|
|
2015-08-20 03:22:53 +08:00
|
|
|
import (
|
|
|
|
"strings"
|
2015-04-09 00:06:47 +08:00
|
|
|
|
2015-08-20 03:22:53 +08:00
|
|
|
"github.com/nsf/termbox-go"
|
2015-04-09 00:06:47 +08:00
|
|
|
)
|
|
|
|
|
2015-08-20 03:22:53 +08:00
|
|
|
//import "github.com/nsf/termbox-go"
|
2015-04-09 00:06:47 +08:00
|
|
|
|
|
|
|
var evtChs = make([]chan Event, 0)
|
|
|
|
|
|
|
|
// EventCh returns an output-only event channel.
|
|
|
|
// This function can be called many times (multiplexer).
|
|
|
|
func EventCh() <-chan Event {
|
|
|
|
out := make(chan Event)
|
|
|
|
evtChs = append(evtChs, out)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn on event listener
|
|
|
|
func evtListen() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
e := termbox.PollEvent()
|
|
|
|
// dispatch
|
|
|
|
for _, c := range evtChs {
|
|
|
|
go func(ch chan Event) {
|
|
|
|
ch <- uiEvt(e)
|
|
|
|
}(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2015-08-20 03:22:53 +08:00
|
|
|
|
2015-08-09 07:07:32 +08:00
|
|
|
type Event struct {
|
2015-08-20 03:22:53 +08:00
|
|
|
Type string
|
|
|
|
Uri string
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
Data interface{}
|
|
|
|
Time int
|
2015-08-09 07:07:32 +08:00
|
|
|
}
|
2015-04-09 00:06: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-04-09 00:06:47 +08:00
|
|
|
|
2015-08-09 07:07:32 +08:00
|
|
|
//
|
|
|
|
type EvtStream struct {
|
2015-08-20 03:22:53 +08:00
|
|
|
srcMap map[string]Event
|
|
|
|
stream chan Event
|
|
|
|
cache map[string][]func(Event)
|
|
|
|
Handlers map[string]func(Event)
|
2015-04-09 00:06:47 +08:00
|
|
|
}
|
|
|
|
|
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-04-09 00:06:47 +08:00
|
|
|
}
|
|
|
|
|
2015-08-09 07:07:32 +08:00
|
|
|
func NewEvtStream() EvtStream {
|
|
|
|
return EvtStream{
|
2015-08-20 03:22:53 +08:00
|
|
|
srcMap: make(map[string]Event),
|
2015-08-09 07:07:32 +08:00
|
|
|
stream: make(chan Event),
|
|
|
|
}
|
2015-04-09 00:06:47 +08:00
|
|
|
}
|
|
|
|
|
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-09 07:07:32 +08:00
|
|
|
/*
|
|
|
|
func (es *EvtStream) hookup() {
|
2015-04-09 00:06:47 +08:00
|
|
|
|
2015-08-09 07:07:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es EvtStream) Subscribe(uri string) chan Event {
|
2015-04-09 00:06:47 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
*/
|