-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (130 loc) · 3.67 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
package main
import (
_ "embed"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
rl "github.com/gen2brain/raylib-go/raylib"
)
//go:embed JetBrainsMonoNerdFont-Medium.ttf
var fontData []byte
func main() {
flag.Parse()
homeDir, err := os.UserHomeDir()
if err != nil {
logError("Could not find the home directory: %s", err.Error())
os.Exit(1)
}
configPath := filepath.Join(homeDir, ".config", "links.txt")
bytes, err := os.ReadFile(configPath)
if err != nil {
logError("Failed to read %s: %s", configPath, err.Error())
os.Exit(1)
}
links := strings.Split(string(bytes), "\n")
links = links[0 : len(links)-1]
filteredLinks := []string{}
query := ""
selectedLink := 0
queryUpdated := true
blueBg := rl.NewColor(91, 206, 250, 100)
pinkBg := rl.NewColor(245, 169, 184, 100)
black := rl.NewColor(0, 0, 0, 255)
resultsY := 50
width := 1000
height := 300
rectX := 0.075 * float32(width)
rectWidth := int32(0.85 * float32(width))
rl.SetTargetFPS(60)
rl.SetConfigFlags(rl.FlagWindowUndecorated)
rl.InitWindow(int32(width), int32(height), "Links")
windowPos := rl.GetWindowPosition()
rl.SetWindowPosition(int(windowPos.X), 220)
fontSize := 20
font := rl.LoadFontFromMemory(".ttf", fontData, 2*int32(fontSize), nil)
defer rl.CloseWindow()
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawRectangle(int32(rectX), 20, rectWidth, 20, blueBg)
// TODO: Handle holding keys down
key := rl.GetKeyPressed()
if key == rl.KeyBackspace {
if len(query) > 0 {
query = query[0 : len(query)-1]
queryUpdated = true
}
} else if key == rl.KeyEnter {
if len(filteredLinks) > 0 {
url, _, _ := strings.Cut(filteredLinks[selectedLink], " ")
rl.OpenURL(url)
os.Exit(0)
}
} else if key == rl.KeyDown {
selectedLink = min(len(filteredLinks)-1, selectedLink+1)
} else if key == rl.KeyUp {
selectedLink = max(0, selectedLink-1)
} else if rl.IsKeyDown(rl.KeyLeftControl) || rl.IsKeyDown(rl.KeyRightControl) {
switch key {
case rl.KeyU:
query = ""
queryUpdated = true
case rl.KeyLeftBracket:
os.Exit(0)
case rl.KeyJ, rl.KeyN:
selectedLink = min(len(filteredLinks)-1, selectedLink+1)
case rl.KeyK, rl.KeyP:
selectedLink = max(0, selectedLink-1)
case rl.KeyM:
if len(filteredLinks) > 0 {
url, _, _ := strings.Cut(filteredLinks[selectedLink], " ")
rl.OpenURL(url)
os.Exit(0)
}
}
} else if rl.IsKeyDown(rl.KeyLeftAlt) || rl.IsKeyDown(rl.KeyRightAlt) {
switch key {
case rl.KeyJ, rl.KeyN:
selectedLink = min(len(filteredLinks)-1, selectedLink+5)
case rl.KeyK, rl.KeyP:
selectedLink = max(0, selectedLink-5)
}
} else if key >= 39 && key <= 90 {
if rl.IsKeyDown(rl.KeyLeftShift) || rl.IsKeyDown(rl.KeyRightShift) {
query += string(key)
queryUpdated = true
} else {
query += strings.ToLower(string(key))
queryUpdated = true
}
}
rl.DrawTextEx(font, query, rl.NewVector2(rectX, 20), float32(fontSize), 0, black)
if queryUpdated {
selectedLink = 0
filteredLinks = []string{}
for _, l := range links {
if strings.Contains(l, query) {
filteredLinks = append(filteredLinks, l)
}
}
}
linksToShow := filteredLinks[selectedLink:]
for i, l := range linksToShow {
if i == 0 {
rl.DrawRectangle(int32(rectX), int32(resultsY+i*20), rectWidth, 20, pinkBg)
}
rl.DrawTextEx(font, l[:min(94, len(l))], rl.NewVector2(rectX, float32(resultsY+i*20)), float32(fontSize), 0, rl.Black)
}
queryUpdated = false
rl.EndDrawing()
}
}
func logError(format string, args ...any) {
if !strings.HasSuffix(format, "\n") {
format = format + "\n"
}
format = "ERROR: " + format
fmt.Fprintf(os.Stderr, format, args...)
}