-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
182 lines (173 loc) · 4.37 KB
/
handlers.go
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
package squirssi
import (
"strings"
"code.dopame.me/veonik/squircy3/event"
"github.com/sirupsen/logrus"
"code.dopame.me/veonik/squirssi/widget"
)
func bindUIHandlers(srv *Server, events *event.Dispatcher) {
events.Bind("ui.DIRTY", HandleUIEvent(srv, onUIDirty))
}
type UIHandler func(*Server, *event.Event)
func HandleUIEvent(srv *Server, fn UIHandler) event.Handler {
return event.HandlerFunc(func(ev *event.Event) {
fn(srv, ev)
})
}
func onUIDirty(srv *Server, _ *event.Event) {
srv.Update()
srv.Render()
}
// onUIKeyPress handles keyboard input from termui.
// Not a regular event handler but instead called before the actual
// ui.KEYPRESS event is emitted. This is done to avoid extra lag between
// pressing a key and seeing the UI react.
func onUIKeyPress(srv *Server, key string) {
if key != "<Tab>" {
srv.tabber.Clear()
}
switch key {
case "<C-c>":
srv.inputTextBox.Append(string(rune(0x03)))
srv.RenderOnly(InputTextBox)
case "<C-u>":
srv.inputTextBox.Append(string(rune(0x1F)))
srv.RenderOnly(InputTextBox)
case "<C-b>":
srv.inputTextBox.Append(string(rune(0x02)))
srv.RenderOnly(InputTextBox)
case "<M-b>":
srv.inputTextBox.CursorPrevWord()
srv.RenderOnly(InputTextBox)
case "<M-f>":
srv.inputTextBox.CursorNextWord()
srv.RenderOnly(InputTextBox)
case "<Home>":
srv.inputTextBox.CursorStartLine()
srv.RenderOnly(InputTextBox)
case "<End>":
srv.inputTextBox.CursorEndLine()
srv.RenderOnly(InputTextBox)
case "<PageUp>":
srv.mu.RLock()
h := srv.pageSize - 2
srv.mu.RUnlock()
srv.windows.ScrollOffset(-h)
case "<PageDown>":
srv.mu.RLock()
h := srv.pageSize - 2
srv.mu.RUnlock()
srv.windows.ScrollOffset(h)
case "<M-<PageUp>>":
srv.mu.Lock()
h := srv.pageSize - 2
srv.userListPane.SelectedRow -= h
srv.mu.Unlock()
srv.events.Emit("ui.DIRTY", nil)
case "<M-<PageDown>>":
srv.mu.Lock()
h := srv.pageSize - 2
if srv.userListPane.SelectedRow == 0 {
srv.userListPane.SelectedRow = h
}
srv.userListPane.SelectedRow += h
srv.mu.Unlock()
srv.events.Emit("ui.DIRTY", nil)
case "<Space>":
srv.inputTextBox.Append(" ")
srv.RenderOnly(InputTextBox)
case "<Backspace>":
srv.inputTextBox.Backspace()
srv.RenderOnly(InputTextBox)
case "<Delete>":
srv.inputTextBox.DeleteNext()
srv.RenderOnly(InputTextBox)
case "<C-5>":
srv.windows.SelectNext()
case "<Escape>":
srv.windows.SelectPrev()
case "<Up>":
win := srv.windows.Active()
if win == nil {
return
}
cur := srv.inputTextBox.Consume()
if cur.Text != "" {
srv.history.Insert(win, cur)
}
msg := srv.history.Previous(win)
srv.inputTextBox.Set(msg)
srv.RenderOnly(InputTextBox)
case "<Down>":
win := srv.windows.Active()
if win == nil {
return
}
cur := srv.inputTextBox.Consume()
if cur.Text != "" {
srv.history.Insert(win, cur)
}
msg := srv.history.Next(win)
srv.inputTextBox.Set(msg)
srv.RenderOnly(InputTextBox)
case "<Left>":
srv.inputTextBox.CursorPrev()
srv.RenderOnly(InputTextBox)
case "<Right>":
srv.inputTextBox.CursorNext()
srv.RenderOnly(InputTextBox)
case "<Tab>":
win := srv.windows.Active()
if ch, ok := win.(*Channel); ok {
var tabbed string
if srv.tabber.Active() {
tabbed = srv.tabber.Tab()
} else {
tabbed = srv.tabber.Reset(srv.inputTextBox.Peek(), ch)
}
srv.inputTextBox.Set(widget.ModedText{Kind: srv.inputTextBox.Mode(), Text: tabbed})
}
srv.RenderOnly(InputTextBox)
case "<Enter>":
in := srv.inputTextBox.Consume()
active := srv.windows.ActiveIndex()
channel := srv.windows.Active()
if channel == nil {
return
}
if len(in.Text) == 0 {
// render anyway incase the textbox mode was changed
srv.RenderOnly(MainWindow, InputTextBox)
return
}
defer srv.RenderOnly(InputTextBox)
defer srv.history.Append(channel, in)
switch in.Kind {
case widget.ModeCommand:
args := strings.Split(in.Text, " ")
c := args[0]
if cmd, ok := builtIns[c]; ok {
cmd(srv, args)
} else {
logrus.Warnln("no command named:", c)
}
case widget.ModeMessage:
if active == 0 {
// status window doesn't accept messages
return
}
msgTarget(srv, []string{"msg", channel.Title(), in.Text})
}
default:
if len(key) != 1 {
logrus.Debugln("received unhandled keypress:", key)
return
}
if key == "/" && srv.inputTextBox.Pos() == 0 {
srv.inputTextBox.ToggleMode()
} else {
srv.inputTextBox.Append(key)
}
srv.RenderOnly(InputTextBox)
}
}