summaryrefslogtreecommitdiffstats
path: root/termui/events.go
blob: 884910143e2153ac48bd8600b16b0fc22b7fd94d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package termui

import (
	"strconv"

	tb "github.com/nsf/termbox-go"
)

var eventStream = EventStream{
	make(map[string]func(Event)),
	"",
	make(chan bool, 1),
	make(chan tb.Event),
}

type EventStream struct {
	eventHandlers map[string]func(Event)
	prevKey       string // previous keypress
	stopLoop      chan bool
	eventQueue    chan tb.Event // list of events from termbox
}

// Event is a copy of termbox.Event that only contains the fields we need.
type Event struct {
	Key    string
	Width  int
	Height int
	MouseX int
	MouseY int
}

// handleEvent calls the approriate callback function if there is one.
func handleEvent(e tb.Event) {
	if e.Type == tb.EventError {
		panic(e.Err)
	}

	ne := convertTermboxEvent(e)

	if val, ok := eventStream.eventHandlers[ne.Key]; ok {
		val(ne)
		eventStream.prevKey = ""
	} else { // check if the last 2 keys form a key combo with a handler
		// if this is a keyboard event and the previous event was unhandled
		if e.Type == tb.EventKey && eventStream.prevKey != "" {
			combo := eventStream.prevKey + ne.Key
			if val, ok := eventStream.eventHandlers[combo]; ok {
				ne.Key = combo
				val(ne)
				eventStream.prevKey = ""
			} else {
				eventStream.prevKey = ne.Key
			}
		} else {
			eventStream.prevKey = ne.Key
		}
	}
}

// Loop gets events from termbox and passes them off to handleEvent.
// Stops when StopLoop is called.
func Loop() {
	go func() {
		for {
			eventStream.eventQueue <- tb.PollEvent()
		}
	}()

	for {
		select {
		case <-eventStream.stopLoop:
			return
		case e := <-eventStream.eventQueue:
			handleEvent(e)
		}
	}
}

// StopLoop stops the event loop.
func StopLoop() {
	eventStream.stopLoop <- true
}

// On assigns event names to their handlers. Takes a string, strings, or a slice of strings, and a function.
func On(things ...interface{}) {
	function := things[len(things)-1].(func(Event))
	for _, thing := range things {
		if value, ok := thing.(string); ok {
			eventStream.eventHandlers[value] = function
		}
		if value, ok := thing.([]string); ok {
			for _, name := range value {
				eventStream.eventHandlers[name] = function
			}
		}
	}
}

// convertTermboxKeyValue 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 convertTermboxKeyValue(e tb.Event) string {
	k := string(e.Ch)
	pre := ""
	mod := ""

	if e.Mod == tb.ModAlt {
		mod = "M-"
	}
	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>"}
			k = ks[0xFFFF-int(e.Key)-12]
		}

		if e.Key <= 0x7F {
			pre = "C-"
			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"},
			}
			if sk, ok := kmap[e.Key]; ok {
				pre = sk[0]
				k = sk[1]
			}
		}
	}

	return pre + mod + k
}

func convertTermboxMouseValue(e tb.Event) string {
	switch e.Key {
	case tb.MouseLeft:
		return "MouseLeft"
	case tb.MouseMiddle:
		return "MouseMiddle"
	case tb.MouseRight:
		return "MouseRight"
	case tb.MouseWheelUp:
		return "MouseWheelUp"
	case tb.MouseWheelDown:
		return "MouseWheelDown"
	case tb.MouseRelease:
		return "MouseRelease"
	}
	return ""
}

// convertTermboxEvent turns a termbox event into a termui event.
func convertTermboxEvent(e tb.Event) Event {
	var ne Event

	switch e.Type {
	case tb.EventKey:
		ne = Event{
			Key: convertTermboxKeyValue(e),
		}
	case tb.EventMouse:
		ne = Event{
			Key:    convertTermboxMouseValue(e),
			MouseX: e.MouseX,
			MouseY: e.MouseY,
		}
	case tb.EventResize:
		ne = Event{
			Key:    "resize",
			Width:  e.Width,
			Height: e.Height,
		}
	}

	return ne
}