-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
208 lines (179 loc) · 4.92 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//go:generate esc -o static.go -pkg main static
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/atotto/clipboard"
ordering "github.com/bcongdon/emoji-ordering"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"golang.org/x/crypto/ssh/terminal"
)
var (
outputFlag = flag.String("output", "clipboard", "The output of ep. Choices: clipboard, stdout")
noninteractiveFlag = flag.Bool("noninteractive", false, "If set, doesn't display emoji picker -- instead just outputting the first selection for the provided query.")
)
var usageFunc = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "%s [QUERY]\n", os.Args[0])
flag.PrintDefaults()
}
type Emoji struct {
Keywords []string `json:"keywords"`
Char string `json:"char"`
}
func getEmojis() (map[string][]Emoji, error) {
raw := FSMustByte(false, "/static/emojis.json")
nameMap := make(map[string]Emoji)
err := json.Unmarshal(raw, &nameMap)
if err != nil {
return nil, err
}
keywordMap := make(map[string][]Emoji)
for name, emoji := range nameMap {
nameKey := strings.ReplaceAll(name, "_", " ")
keywordMap[nameKey] = append(keywordMap[nameKey], emoji)
for _, keyword := range emoji.Keywords {
keywordMap[keyword] = append(keywordMap[keyword], emoji)
}
}
return keywordMap, nil
}
func filterEmojis(emojis map[string][]Emoji, query string) []string {
justEmojis := []string{}
for key, e := range emojis {
if !strings.Contains(key, query) {
continue
}
for _, emoji := range e {
justEmojis = append(justEmojis, emoji.Char)
}
}
sort.Sort(ordering.EmojiSlice(justEmojis))
return justEmojis
}
func drawEmojis(table *tview.Table, emojis map[string][]Emoji, query string, numCols int) {
filteredEmojis := filterEmojis(emojis, query)
used := make(map[string]bool)
gridIdx := 0
for idx := 0; idx < len(filteredEmojis); idx++ {
r, c := gridIdx/numCols, gridIdx%numCols
emoji := filteredEmojis[idx]
if _, alreadyUsed := used[emoji]; !alreadyUsed {
table.SetCell(r, c, tview.NewTableCell(emoji))
used[emoji] = true
gridIdx++
}
}
table.ScrollToBeginning()
table.Select(0, 0)
}
func validateFlags() {
if *outputFlag != "clipboard" && *outputFlag != "stdout" {
log.Panicf("Invalid output method: %s\n", *outputFlag)
}
}
func outputEmoji(emoji string) {
switch *outputFlag {
case "clipboard":
clipboard.WriteAll(emoji)
case "stdout":
fmt.Println(emoji)
default:
log.Panicf("Unknown output method: %s", *outputFlag)
}
}
func runNoninterativeMode(emojis map[string][]Emoji, query string) {
if len(query) == 0 {
log.Panicln("A query must be specified in noninteractive mode.")
}
filteredEmojis := filterEmojis(emojis, query)
if len(filteredEmojis) > 0 {
outputEmoji(filteredEmojis[0])
}
}
func main() {
flag.Usage = usageFunc
flag.Parse()
validateFlags()
app := tview.NewApplication()
table := tview.NewTable().
SetBorders(false).
SetSelectable(true, true).
SetFixed(0, 0)
initialQuery := strings.Join(flag.Args(), " ")
inputField := tview.NewInputField().
SetDoneFunc(func(key tcell.Key) {
app.SetFocus(table)
table.SetSelectable(true, true)
}).SetText(initialQuery)
inputField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyDown {
app.SetFocus(table)
table.SetSelectable(true, true)
}
return event
})
width, height, err := terminal.GetSize(0)
if err != nil {
log.Fatalf("Unable to get terminal size: %v", err)
}
height = clamp(height, 20, 40)
numCols := (width - 2) / 3
grid := tview.NewGrid().
SetRows(1, 1).
SetColumns(1, 1).
AddItem(inputField, 0, 0, 1, 3, 0, 0, true).
AddItem(table, 2, 0, 1, 3, 0, 0, false)
grid.SetBorder(true).SetTitle("Emoji Picker").SetRect(0, 0, width, height)
emojis, _ := getEmojis()
if *noninteractiveFlag {
runNoninterativeMode(emojis, initialQuery)
return
}
inputField.SetChangedFunc(func(text string) {
table.Clear()
drawEmojis(table, emojis, text, numCols)
})
table.SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
table.SetSelectable(true, true)
app.Stop()
}
}).SetSelectedFunc(func(row int, column int) {
cell := table.GetCell(row, column)
app.Stop()
outputEmoji(cell.Text)
}).SetSelectable(false, false)
table.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
row, _ := table.GetSelection()
if event.Key() == tcell.KeyUp && row == 0 {
app.SetFocus(inputField)
table.SetSelectable(false, false)
} else if event.Key() == tcell.KeyRune {
inputField.SetText(inputField.GetText() + string(event.Rune()))
app.SetFocus(inputField)
table.SetSelectable(false, false)
}
return event
})
drawEmojis(table, emojis, initialQuery, numCols)
if err := app.SetRoot(grid, false).Run(); err != nil {
panic(err)
}
}
func clamp(val, low, high int) int {
switch {
case val < low:
return low
case val > high:
return high
default:
return val
}
}