2017-01-13 23:07:43 -07:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-04-08 22:21:36 -06:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-04-08 10:06:47 -06:00
|
|
|
package termui
|
|
|
|
|
2015-08-19 13:22:53 -06:00
|
|
|
import (
|
2019-01-24 06:23:30 -07:00
|
|
|
"fmt"
|
2015-04-08 10:06:47 -06:00
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
tb "github.com/nsf/termbox-go"
|
2015-04-08 10:06:47 -06:00
|
|
|
)
|
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
/*
|
2018-11-28 19:19:34 -07:00
|
|
|
List of events:
|
2018-09-06 17:36:14 -06:00
|
|
|
mouse events:
|
|
|
|
<MouseLeft> <MouseRight> <MouseMiddle>
|
|
|
|
<MouseWheelUp> <MouseWheelDown>
|
|
|
|
keyboard events:
|
2019-01-23 21:12:10 -07:00
|
|
|
any uppercase or lowercase letter like j or J
|
2018-09-06 17:36:14 -06:00
|
|
|
<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
|
|
|
|
)
|
|
|
|
|
2015-04-08 10:06:47 -06:00
|
|
|
type Event struct {
|
2018-09-06 17:36:14 -06:00
|
|
|
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 16:15:57 -07:00
|
|
|
// PollEvents gets events from termbox, converts them, then sends them to each of its channels.
|
|
|
|
func PollEvents() <-chan Event {
|
2018-11-28 19:19:34 -07:00
|
|
|
ch := make(chan Event)
|
2018-09-06 17:36:14 -06:00
|
|
|
go func() {
|
2018-11-29 16:15:57 -07:00
|
|
|
for {
|
|
|
|
ch <- convertTermboxEvent(tb.PollEvent())
|
|
|
|
}
|
2018-09-06 17:36:14 -06:00
|
|
|
}()
|
2018-11-28 19:19:34 -07:00
|
|
|
return ch
|
2018-09-06 17:36:14 -06:00
|
|
|
}
|
2015-09-18 09:41:44 -06:00
|
|
|
|
2018-09-06 17:36:14 -06: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 {
|
2019-01-24 06:23:30 -07:00
|
|
|
ID := "%s"
|
2018-09-06 17:36:14 -06:00
|
|
|
if e.Mod == tb.ModAlt {
|
2019-01-24 06:23:30 -07:00
|
|
|
ID = "<M-%s>"
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
|
|
|
|
2019-01-24 06:23:30 -07:00
|
|
|
if e.Ch != 0 {
|
|
|
|
ID = fmt.Sprintf(ID, string(e.Ch))
|
|
|
|
} else {
|
|
|
|
switchExpression := func() string {
|
|
|
|
switch e.Key {
|
|
|
|
case tb.KeyF1:
|
|
|
|
return "<F1>"
|
|
|
|
case tb.KeyF2:
|
|
|
|
return "<F2>"
|
|
|
|
case tb.KeyF3:
|
|
|
|
return "<F3>"
|
|
|
|
case tb.KeyF4:
|
|
|
|
return "<F4>"
|
|
|
|
case tb.KeyF5:
|
|
|
|
return "<F5>"
|
|
|
|
case tb.KeyF6:
|
|
|
|
return "<F6>"
|
|
|
|
case tb.KeyF7:
|
|
|
|
return "<F7>"
|
|
|
|
case tb.KeyF8:
|
|
|
|
return "<F8>"
|
|
|
|
case tb.KeyF9:
|
|
|
|
return "<F9>"
|
|
|
|
case tb.KeyF10:
|
|
|
|
return "<F10>"
|
|
|
|
case tb.KeyF11:
|
|
|
|
return "<F11>"
|
|
|
|
case tb.KeyF12:
|
|
|
|
return "<F12>"
|
|
|
|
case tb.KeyInsert:
|
|
|
|
return "<Insert>"
|
|
|
|
case tb.KeyDelete:
|
|
|
|
return "<Delete>"
|
|
|
|
case tb.KeyHome:
|
|
|
|
return "<Home>"
|
|
|
|
case tb.KeyEnd:
|
|
|
|
return "<End>"
|
|
|
|
case tb.KeyPgup:
|
|
|
|
return "<PageUp>"
|
|
|
|
case tb.KeyPgdn:
|
|
|
|
return "<PageDown>"
|
|
|
|
case tb.KeyArrowUp:
|
|
|
|
return "<Up>"
|
|
|
|
case tb.KeyArrowDown:
|
|
|
|
return "<Down>"
|
|
|
|
case tb.KeyArrowLeft:
|
|
|
|
return "<Left>"
|
|
|
|
case tb.KeyArrowRight:
|
|
|
|
return "<Right>"
|
|
|
|
|
|
|
|
case tb.KeyCtrlTilde: // tb.KeyCtrl2 tb.KeyCtrlSpace
|
|
|
|
// <C-~> doesn't work
|
|
|
|
// <C-2> doesn't work
|
|
|
|
return "<C-<Space>>"
|
|
|
|
case tb.KeyCtrlA:
|
|
|
|
return "<C-a>"
|
|
|
|
case tb.KeyCtrlB:
|
|
|
|
return "<C-b>"
|
|
|
|
case tb.KeyCtrlC:
|
|
|
|
return "<C-c>"
|
|
|
|
case tb.KeyCtrlD:
|
|
|
|
return "<C-d>"
|
|
|
|
case tb.KeyCtrlE:
|
|
|
|
return "<C-e>"
|
|
|
|
case tb.KeyCtrlF:
|
|
|
|
return "<C-f>"
|
|
|
|
case tb.KeyCtrlG:
|
|
|
|
return "<C-g>"
|
|
|
|
case tb.KeyBackspace: // tb.KeyCtrlH
|
|
|
|
// <C-h> doesn't work
|
|
|
|
return "<C-<Backspace>>"
|
|
|
|
case tb.KeyTab: // tb.KeyCtrlI
|
|
|
|
// <C-i> doesn't work
|
|
|
|
return "<Tab>"
|
|
|
|
case tb.KeyCtrlJ:
|
|
|
|
return "<C-j>"
|
|
|
|
case tb.KeyCtrlK:
|
|
|
|
return "<C-k>"
|
|
|
|
case tb.KeyCtrlL:
|
|
|
|
return "<C-l>"
|
|
|
|
case tb.KeyEnter: // tb.KeyCtrlM
|
|
|
|
// <C-m> doesn't work
|
|
|
|
return "<Enter>"
|
|
|
|
case tb.KeyCtrlN:
|
|
|
|
return "<C-n>"
|
|
|
|
case tb.KeyCtrlO:
|
|
|
|
return "<C-o>"
|
|
|
|
case tb.KeyCtrlP:
|
|
|
|
return "<C-p>"
|
|
|
|
case tb.KeyCtrlQ:
|
|
|
|
return "<C-q>"
|
|
|
|
case tb.KeyCtrlR:
|
|
|
|
return "<C-r>"
|
|
|
|
case tb.KeyCtrlS:
|
|
|
|
return "<C-s>"
|
|
|
|
case tb.KeyCtrlT:
|
|
|
|
return "<C-t>"
|
|
|
|
case tb.KeyCtrlU:
|
|
|
|
return "<C-u>"
|
|
|
|
case tb.KeyCtrlV:
|
|
|
|
return "<C-v>"
|
|
|
|
case tb.KeyCtrlW:
|
|
|
|
return "<C-w>"
|
|
|
|
case tb.KeyCtrlX:
|
|
|
|
return "<C-x>"
|
|
|
|
case tb.KeyCtrlY:
|
|
|
|
return "<C-y>"
|
|
|
|
case tb.KeyCtrlZ:
|
|
|
|
return "<C-z>"
|
|
|
|
case tb.KeyEsc: // tb.KeyCtrlLsqBracket tb.KeyCtrl3
|
|
|
|
// <C-[> doesn't work
|
|
|
|
// <C-3> doesn't work
|
|
|
|
return "<Escape>"
|
|
|
|
case tb.KeyCtrl4: // tb.KeyCtrlBackslash
|
|
|
|
// <C-\\> doesn't work
|
|
|
|
return "<C-4>"
|
|
|
|
case tb.KeyCtrl5: // tb.KeyCtrlRsqBracket
|
|
|
|
// <C-]> doesn't work
|
|
|
|
return "<C-5>"
|
|
|
|
case tb.KeyCtrl6:
|
|
|
|
return "<C-6>"
|
|
|
|
case tb.KeyCtrl7: // tb.KeyCtrlSlash tb.KeyCtrlUnderscore
|
|
|
|
// <C-/> doesn't work
|
|
|
|
// <C-_> doesn't work
|
|
|
|
return "<C-7>"
|
|
|
|
case tb.KeySpace:
|
|
|
|
return "<Space>"
|
|
|
|
case tb.KeyBackspace2: // tb.KeyCtrl8:
|
|
|
|
// <C-8> doesn't work
|
|
|
|
return "<Backspace>"
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
2019-01-24 06:23:30 -07:00
|
|
|
// <C--> doesn't work
|
|
|
|
return ""
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
2019-01-24 06:23:30 -07:00
|
|
|
ID = fmt.Sprintf(ID, switchExpression())
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
return Event{
|
|
|
|
Type: KeyboardEvent,
|
2019-01-24 06:23:30 -07:00
|
|
|
ID: ID,
|
2018-01-11 11:01:45 -07:00
|
|
|
}
|
2017-05-27 04:11:46 -06:00
|
|
|
}
|
|
|
|
|
2018-09-06 17:36:14 -06: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-30 21:03:47 -06:00
|
|
|
}
|
2015-09-18 09:41:44 -06:00
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
converted, ok := mouseButtonMap[e.Key]
|
|
|
|
if !ok {
|
|
|
|
converted = "Unknown_Mouse_Button"
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
Drag := false
|
|
|
|
if e.Mod == tb.ModMotion {
|
|
|
|
Drag = true
|
2015-08-19 13:22:53 -06:00
|
|
|
}
|
2015-09-18 19:07:57 -06:00
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
return Event{
|
|
|
|
Type: MouseEvent,
|
|
|
|
ID: converted,
|
|
|
|
Payload: Mouse{
|
|
|
|
X: e.MouseX,
|
|
|
|
Y: e.MouseY,
|
|
|
|
Drag: Drag,
|
|
|
|
},
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
2015-10-07 12:25:59 -06:00
|
|
|
}
|
2017-01-17 01:11:16 -07:00
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
// convertTermboxEvent turns a termbox event into a termui event.
|
|
|
|
func convertTermboxEvent(e tb.Event) Event {
|
|
|
|
if e.Type == tb.EventError {
|
|
|
|
panic(e.Err)
|
2016-08-13 05:43:04 -06:00
|
|
|
}
|
2015-08-08 17:07:32 -06:00
|
|
|
|
2018-11-28 19:19:34 -07:00
|
|
|
var event Event
|
|
|
|
|
2018-09-06 17:36:14 -06:00
|
|
|
switch e.Type {
|
|
|
|
case tb.EventKey:
|
2018-11-28 19:19:34 -07:00
|
|
|
event = convertTermboxKeyboardEvent(e)
|
2018-09-06 17:36:14 -06:00
|
|
|
case tb.EventMouse:
|
2018-11-28 19:19:34 -07:00
|
|
|
event = convertTermboxMouseEvent(e)
|
2018-09-06 17:36:14 -06:00
|
|
|
case tb.EventResize:
|
2018-11-28 19:19:34 -07:00
|
|
|
event = Event{
|
2018-09-06 17:36:14 -06:00
|
|
|
Type: ResizeEvent,
|
|
|
|
ID: "<Resize>",
|
|
|
|
Payload: Resize{
|
|
|
|
Width: e.Width,
|
|
|
|
Height: e.Height,
|
|
|
|
},
|
2015-10-07 12:25:59 -06:00
|
|
|
}
|
2015-09-18 09:41:44 -06:00
|
|
|
}
|
|
|
|
|
2018-11-28 19:19:34 -07:00
|
|
|
return event
|
2015-10-13 10:45:03 -06:00
|
|
|
}
|