This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
/
autocomplete.go
61 lines (50 loc) · 1.7 KB
/
autocomplete.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
// mauIRC - The original mauIRC web frontend
// Copyright (C) 2016 Tulir Asokan
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package ui contains UI-related functions
package ui
import (
"fmt"
"maunium.net/go/mauirc/data"
"strings"
)
var commands = []string{"/me", "/topic", "/nick", "/msg", "/query", "/join", "/part", "/exit"}
// Autocomplete things
func Autocomplete() {
msgbox := jq("#message-text")
text := msgbox.Val()
caretPos := msgbox.Underlying().Index(0).Get("selectionStart").Int()
spIndex := strings.LastIndex(text[:caretPos], " ")
if spIndex == caretPos-1 {
return
}
word := text[spIndex+1 : caretPos]
var acList []string
fmtStart := "%s %s"
fmtMid := "%s %s%s"
if word[0] == '/' {
acList = commands
} else {
fmtStart = "%s: %s"
acList = data.MustGetChannel(GetActiveNetwork(), GetActiveChannel()).UserlistPlain
}
for _, ac := range acList {
if strings.HasPrefix(strings.ToLower(ac), word) {
if spIndex == -1 {
msgbox.SetVal(fmt.Sprintf(fmtStart, ac, text[caretPos:]))
} else {
msgbox.SetVal(fmt.Sprintf(fmtMid, text[:spIndex], ac, text[caretPos:]))
}
return
}
}
}