mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
refactor deep blocks
This commit is contained in:
parent
f13a1fe9bc
commit
675bb8b7c0
2
ctx.go
2
ctx.go
|
|
@ -216,7 +216,7 @@ func (c *Ctx) NewFilter() *Filter {
|
|||
}
|
||||
|
||||
func (c *Ctx) NewInput() *Input {
|
||||
return &Input{c, &sync.Mutex{}}
|
||||
return &Input{c, &sync.Mutex{}, nil}
|
||||
}
|
||||
|
||||
func (c *Ctx) Stop() {
|
||||
|
|
|
|||
84
input.go
84
input.go
|
|
@ -10,6 +10,7 @@ import (
|
|||
type Input struct {
|
||||
*Ctx
|
||||
mutex *sync.Mutex // Currently only used for protecting Alt/Esc workaround
|
||||
mod *time.Timer
|
||||
}
|
||||
|
||||
func (i *Input) Loop() {
|
||||
|
|
@ -31,53 +32,56 @@ func (i *Input) Loop() {
|
|||
}
|
||||
}()
|
||||
|
||||
hasModifierMaps := i.config.Keymap.hasModifierMaps()
|
||||
var mod *time.Timer
|
||||
for {
|
||||
select {
|
||||
case <-i.LoopCh(): // can only fall here if we closed c.loopCh
|
||||
return
|
||||
case ev := <-evCh:
|
||||
switch ev.Type {
|
||||
case termbox.EventError:
|
||||
//update = false
|
||||
case termbox.EventResize:
|
||||
i.DrawMatches(nil)
|
||||
case termbox.EventKey:
|
||||
// ModAlt is a sequence of letters with a leading \x1b (=Esc).
|
||||
// It would be nice if termbox differentiated this for us, but
|
||||
// we workaround it by waiting (juuuuse a few milliseconds) for
|
||||
// extra key events. If no extra events arrive, it should be Esc
|
||||
if !hasModifierMaps {
|
||||
i.handleKeyEvent(ev)
|
||||
continue
|
||||
}
|
||||
i.handleInputEvent(ev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Smells like Esc or Alt. mod == nil checks for the presense
|
||||
// of a previous timer
|
||||
if ev.Ch == 0 && ev.Key == 27 && mod == nil {
|
||||
tmp := ev
|
||||
i.mutex.Lock()
|
||||
mod = time.AfterFunc(50*time.Millisecond, func() {
|
||||
i.mutex.Lock()
|
||||
mod = nil
|
||||
i.mutex.Unlock()
|
||||
i.handleKeyEvent(tmp)
|
||||
})
|
||||
i.mutex.Unlock()
|
||||
} else {
|
||||
// it doesn't look like this is Esc or Alt. If we have a previous
|
||||
// timer, stop it because this is probably Alt+ this new key
|
||||
i.mutex.Lock()
|
||||
if mod != nil {
|
||||
mod.Stop()
|
||||
mod = nil
|
||||
ev.Mod |= ModAlt
|
||||
}
|
||||
i.mutex.Unlock()
|
||||
i.handleKeyEvent(ev)
|
||||
}
|
||||
func (i *Input) handleInputEvent(ev termbox.Event) {
|
||||
hasModifierMaps := i.config.Keymap.hasModifierMaps()
|
||||
switch ev.Type {
|
||||
case termbox.EventError:
|
||||
//update = false
|
||||
case termbox.EventResize:
|
||||
i.DrawMatches(nil)
|
||||
case termbox.EventKey:
|
||||
// ModAlt is a sequence of letters with a leading \x1b (=Esc).
|
||||
// It would be nice if termbox differentiated this for us, but
|
||||
// we workaround it by waiting (juuuuse a few milliseconds) for
|
||||
// extra key events. If no extra events arrive, it should be Esc
|
||||
if !hasModifierMaps {
|
||||
i.handleKeyEvent(ev)
|
||||
return
|
||||
}
|
||||
|
||||
// Smells like Esc or Alt. mod == nil checks for the presense
|
||||
// of a previous timer
|
||||
if ev.Ch == 0 && ev.Key == 27 && i.mod == nil {
|
||||
tmp := ev
|
||||
i.mutex.Lock()
|
||||
i.mod = time.AfterFunc(50*time.Millisecond, func() {
|
||||
i.mutex.Lock()
|
||||
i.mod = nil
|
||||
i.mutex.Unlock()
|
||||
i.handleKeyEvent(tmp)
|
||||
})
|
||||
i.mutex.Unlock()
|
||||
} else {
|
||||
// it doesn't look like this is Esc or Alt. If we have a previous
|
||||
// timer, stop it because this is probably Alt+ this new key
|
||||
i.mutex.Lock()
|
||||
if i.mod != nil {
|
||||
i.mod.Stop()
|
||||
i.mod = nil
|
||||
ev.Mod |= ModAlt
|
||||
}
|
||||
i.mutex.Unlock()
|
||||
i.handleKeyEvent(ev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue