use ok pattern instead of enum

This commit is contained in:
Ivar Fatland
2026-05-10 02:41:17 +02:00
parent 69a1e02b60
commit b8e18263b9
+8 -22
View File
@@ -32,37 +32,23 @@ func main() {
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
lines := strings.Split(string(input), "\n") choices := strings.Split(string(input), "\n")
lines = slices.DeleteFunc(lines, func(l string) bool { choices = slices.DeleteFunc(choices, func(l string) bool {
return l == "" return l == ""
}) })
choice, result := raymenu(lines) choice, ok := raymenu(choices)
switch (result) { if ok {
case RayMenuResult_NoChoice:
return
case RayMenuResult_Chosen:
fmt.Println(choice) fmt.Println(choice)
return
default:
log.Fatalln("Unhandled RayMenuResult", result)
} }
} }
type RayMenuResult int
const (
RayMenuResult_NoChoice RayMenuResult = iota
RayMenuResult_Chosen
Count_RayMenuResult int = iota
)
func isPressedRepeat(key int32) bool { func isPressedRepeat(key int32) bool {
return rl.IsKeyPressed(key) || rl.IsKeyPressedRepeat(key) return rl.IsKeyPressed(key) || rl.IsKeyPressedRepeat(key)
} }
func raymenu(options []string) (choice string, result RayMenuResult) { func raymenu(options []string) (choice string, ok bool) {
state := initialize(len(options)) state := initialize(len(options))
rg.SetFont(state.Font) rg.SetFont(state.Font)
rg.SetStyle(rg.DEFAULT, rg.TEXT_SIZE, fontSize) rg.SetStyle(rg.DEFAULT, rg.TEXT_SIZE, fontSize)
@@ -121,9 +107,9 @@ func raymenu(options []string) (choice string, result RayMenuResult) {
if enter { if enter {
rl.CloseWindow() rl.CloseWindow()
if cursor < len(matched) { if cursor < len(matched) {
return options[matched[cursor].Idx], RayMenuResult_Chosen return options[matched[cursor].Idx], true
} }
return "", RayMenuResult_NoChoice return "", false
} }
for i, match := range matched { for i, match := range matched {
@@ -153,7 +139,7 @@ func raymenu(options []string) (choice string, result RayMenuResult) {
rl.EndDrawing() rl.EndDrawing()
} }
return "", RayMenuResult_NoChoice return "", false
} }
var yOffset float32 = 0.0 var yOffset float32 = 0.0