termui/events.go

176 lines
3.5 KiB
Go
Raw Normal View History

2017-01-14 06:07:43 +00:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package termui
2015-08-19 19:22:53 +00:00
import (
2015-09-18 15:41:44 +00:00
"strconv"
tb "github.com/nsf/termbox-go"
)
/*
2018-11-29 02:19:34 +00:00
List of events:
mouse events:
<MouseLeft> <MouseRight> <MouseMiddle>
<MouseWheelUp> <MouseWheelDown>
keyboard events:
2019-01-24 04:12:10 +00:00
any uppercase or lowercase letter like j or J
<C-d> etc
<M-d> etc
<Up> <Down> <Left> <Right>
<Insert> <Delete> <Home> <End> <Previous> <Next>
<Backspace> <Tab> <Enter> <Escape> <Space>
<C-<Space>> etc
terminal events:
<Resize>
*/
type EventType int
const (
KeyboardEvent EventType = iota
MouseEvent
ResizeEvent
)
type Event struct {
Type EventType
ID string
Payload interface{}
}
// Mouse payload.
type Mouse struct {
Drag bool
X int
Y int
}
// Resize payload.
type Resize struct {
Width int
Height int
}
2018-11-29 23:15:57 +00:00
// PollEvents gets events from termbox, converts them, then sends them to each of its channels.
func PollEvents() <-chan Event {
2018-11-29 02:19:34 +00:00
ch := make(chan Event)
go func() {
2018-11-29 23:15:57 +00:00
for {
ch <- convertTermboxEvent(tb.PollEvent())
}
}()
2018-11-29 02:19:34 +00:00
return ch
}
2015-09-18 15:41:44 +00:00
// convertTermboxKeyboardEvent converts a termbox keyboard event to a more friendly string format.
// Combines modifiers into the string instead of having them as additional fields in an event.
func convertTermboxKeyboardEvent(e tb.Event) Event {
2015-09-18 15:41:44 +00:00
k := string(e.Ch)
pre := ""
mod := ""
if e.Mod == tb.ModAlt {
mod = "<M-"
2015-09-18 15:41:44 +00:00
}
if e.Ch == 0 {
if e.Key > 0xFFFF-12 {
k = "<f" + strconv.Itoa(0xFFFF-int(e.Key)+1) + ">"
} else if e.Key > 0xFFFF-25 {
ks := []string{"<Insert>", "<Delete>", "<Home>", "<End>", "<Previous>", "<Next>", "<Up>", "<Down>", "<Left>", "<Right>"}
2015-09-18 15:41:44 +00:00
k = ks[0xFFFF-int(e.Key)-12]
}
if e.Key <= 0x7F {
pre = "<C-"
2015-09-18 15:41:44 +00:00
k = string('a' - 1 + int(e.Key))
kmap := map[tb.Key][2]string{
tb.KeyCtrlSpace: {"C-", "<Space>"},
tb.KeyBackspace: {"", "<Backspace>"},
tb.KeyTab: {"", "<Tab>"},
tb.KeyEnter: {"", "<Enter>"},
tb.KeyEsc: {"", "<Escape>"},
tb.KeyCtrlBackslash: {"C-", "\\"},
tb.KeyCtrlSlash: {"C-", "/"},
tb.KeySpace: {"", "<Space>"},
tb.KeyCtrl8: {"C-", "8"},
2015-09-18 15:41:44 +00:00
}
if sk, ok := kmap[e.Key]; ok {
pre = sk[0]
k = sk[1]
}
}
}
if pre != "" {
k += ">"
2015-09-18 15:41:44 +00:00
}
id := pre + mod + k
2017-05-27 10:11:46 +00:00
return Event{
Type: KeyboardEvent,
ID: id,
2018-01-11 18:01:45 +00:00
}
2017-05-27 10:11:46 +00:00
}
func convertTermboxMouseEvent(e tb.Event) Event {
mouseButtonMap := map[tb.Key]string{
tb.MouseLeft: "<MouseLeft>",
tb.MouseMiddle: "<MouseMiddle>",
tb.MouseRight: "<MouseRight>",
tb.MouseRelease: "<MouseRelease>",
tb.MouseWheelUp: "<MouseWheelUp>",
tb.MouseWheelDown: "<MouseWheelDown>",
2015-08-31 03:03:47 +00:00
}
2015-09-18 15:41:44 +00:00
converted, ok := mouseButtonMap[e.Key]
if !ok {
converted = "Unknown_Mouse_Button"
2015-09-18 15:41:44 +00:00
}
Drag := false
if e.Mod == tb.ModMotion {
Drag = true
2015-08-19 19:22:53 +00:00
}
return Event{
Type: MouseEvent,
ID: converted,
Payload: Mouse{
X: e.MouseX,
Y: e.MouseY,
Drag: Drag,
},
2015-09-18 15:41:44 +00:00
}
2015-10-07 18:25:59 +00:00
}
// convertTermboxEvent turns a termbox event into a termui event.
func convertTermboxEvent(e tb.Event) Event {
if e.Type == tb.EventError {
panic(e.Err)
}
2015-08-08 23:07:32 +00:00
2018-11-29 02:19:34 +00:00
var event Event
switch e.Type {
case tb.EventKey:
2018-11-29 02:19:34 +00:00
event = convertTermboxKeyboardEvent(e)
case tb.EventMouse:
2018-11-29 02:19:34 +00:00
event = convertTermboxMouseEvent(e)
case tb.EventResize:
2018-11-29 02:19:34 +00:00
event = Event{
Type: ResizeEvent,
ID: "<Resize>",
Payload: Resize{
Width: e.Width,
Height: e.Height,
},
2015-10-07 18:25:59 +00:00
}
2015-09-18 15:41:44 +00:00
}
2018-11-29 02:19:34 +00:00
return event
2015-10-13 16:45:03 +00:00
}