Skip to content

Commit 0cc5f52

Browse files
committed
Add Raylib integration for GUI and enhance Lua input handling
1 parent 826d83d commit 0cc5f52

File tree

6 files changed

+160
-5
lines changed

6 files changed

+160
-5
lines changed

go.mod

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@ module glos
22

33
go 1.23.5
44

5-
require github.com/yuin/gopher-lua v1.1.1
5+
require (
6+
github.com/gen2brain/raylib-go/raylib v0.0.0-20250109172833-6dbba4f81a9b
7+
github.com/yuin/gopher-lua v1.1.1
8+
)
9+
10+
require (
11+
github.com/ebitengine/purego v0.7.1 // indirect
12+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
13+
golang.org/x/sys v0.20.0 // indirect
14+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
2+
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
3+
github.com/gen2brain/raylib-go/raylib v0.0.0-20250109172833-6dbba4f81a9b h1:JJfspevP3YOXcSKVABizYOv++yMpTJIdPUtoDzF/RWw=
4+
github.com/gen2brain/raylib-go/raylib v0.0.0-20250109172833-6dbba4f81a9b/go.mod h1:BaY76bZk7nw1/kVOSQObPY1v1iwVE1KHAGMfvI6oK1Q=
15
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
26
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
7+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM=
8+
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
9+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
10+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

luaexec/lua_runner.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"glos/glosfs"
7+
"glos/ui"
78
"os"
89
"strings"
910

@@ -20,10 +21,12 @@ func Execute(content string, args []string) error {
2021
L.SetGlobal("list_files", L.NewFunction(luaListFiles))
2122
L.SetGlobal("write_file", L.NewFunction(luaWriteFile))
2223
L.SetGlobal("read_multiline_input", L.NewFunction(luaReadMultilineInput))
24+
L.SetGlobal("read_multiline_input_raylib", L.NewFunction(luaReadMultilineInputRaylib))
2325
L.SetGlobal("delete_file", L.NewFunction(luaDeleteFile))
2426
L.SetGlobal("set_env", L.NewFunction(luaSetEnv))
2527
L.SetGlobal("get_env", L.NewFunction(luaGetEnv))
2628
L.SetGlobal("clear_screen", L.NewFunction(luaClearScreen))
29+
L.SetGlobal("print", L.NewFunction(luaPrint))
2730

2831
luaTable := L.NewTable()
2932
for i, arg := range args {
@@ -96,6 +99,23 @@ func luaReadMultilineInput(L *lua.LState) int {
9699
return 1
97100
}
98101

102+
func luaReadMultilineInputRaylib(L *lua.LState) int {
103+
input := ""
104+
maxInputLen := 256
105+
var content strings.Builder
106+
107+
for input != ":exit" {
108+
ui.DrawUI(input)
109+
110+
if ui.HandleInput(&input, maxInputLen) {
111+
content.WriteString(input + "\n")
112+
input = ""
113+
}
114+
}
115+
L.Push(lua.LString(content.String()))
116+
return 1
117+
}
118+
99119
// Example usage in Lua, including error handling:
100120
// success, error_message = delete_file("filename")
101121
// if not success then
@@ -146,6 +166,15 @@ func luaGetEnv(L *lua.LState) int {
146166
// Example usage in Lua:
147167
// clear_screen()
148168
func luaClearScreen(L *lua.LState) int {
149-
fmt.Print("\033[2J\033[H") // ANSI code: Clear screen and move cursor to home
169+
ui.Output = ""
170+
return 0
171+
}
172+
173+
func luaPrint(L *lua.LState) int {
174+
n := L.GetTop()
175+
for i := 1; i <= n; i++ {
176+
ui.Output += L.ToString(i)
177+
}
178+
ui.Output += "\n"
150179
return 0
151180
}

main.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,80 @@
11
package main
22

3-
import "glos/repl"
3+
import (
4+
"fmt"
5+
"glos/glosfs"
6+
"glos/luaexec"
7+
"glos/ui"
8+
"strings"
9+
10+
rl "github.com/gen2brain/raylib-go/raylib"
11+
)
12+
13+
const (
14+
windowWidth = 800
15+
windowHeight = 600
16+
maxInputLen = 256
17+
)
418

519
func main() {
6-
repl.StartREPL()
20+
glosfs.LoadMemoryFS() // Load memoryFS on start
21+
22+
rl.InitWindow(windowWidth, windowHeight, "GLOS GUI")
23+
defer rl.CloseWindow()
24+
rl.SetTargetFPS(60)
25+
26+
input := ""
27+
28+
for !rl.WindowShouldClose() {
29+
ui.DrawUI(input)
30+
31+
if ui.HandleInput(&input, maxInputLen) {
32+
runLuaCommand(strings.ToLower(input))
33+
input = ""
34+
}
35+
}
36+
37+
}
38+
39+
func runLuaCommand(input string) {
40+
args := strings.SplitN(input, " ", 2)
41+
command := args[0]
42+
var param string
43+
if len(args) > 1 {
44+
param = args[1]
45+
}
46+
47+
switch command {
48+
case "exit":
49+
glosfs.SaveMemoryFS() // Save memoryFS before exit
50+
case "run":
51+
runLua(param)
52+
default:
53+
if _, exists := glosfs.MemoryFS[command]; exists {
54+
runLua(command + " " + param) // Execute exact match
55+
} else if _, exists := glosfs.MemoryFS[command+".lua"]; exists {
56+
runLua(command + ".lua" + " " + param) // Try with .lua extension
57+
} else {
58+
ui.Output = "Unknown command"
59+
}
60+
}
61+
}
62+
63+
func runLua(input string) {
64+
args := strings.SplitN(input, " ", 2)
65+
filename := args[0]
66+
var luaArgs []string
67+
68+
if len(args) > 1 {
69+
luaArgs = strings.Fields(args[1])
70+
}
71+
72+
content, exists := glosfs.MemoryFS[filename]
73+
if !exists {
74+
ui.Output = fmt.Sprintf("File '%s' not found.\n", filename)
75+
}
76+
77+
if err := luaexec.Execute(content, luaArgs); err != nil {
78+
ui.Output = fmt.Sprintln("Error executing Lua:", err)
79+
}
780
}

ui/ui.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ui
2+
3+
import rl "github.com/gen2brain/raylib-go/raylib"
4+
5+
var Output = "Welcome to GLOS GUI!\n"
6+
7+
func DrawUI(input string) {
8+
rl.BeginDrawing()
9+
rl.ClearBackground(rl.RayWhite)
10+
11+
rl.DrawText("GLOS GUI Terminal", 20, 20, 20, rl.Black)
12+
rl.DrawRectangleLines(20, 50, 760, 40, rl.Gray)
13+
rl.DrawText(input, 30, 60, 20, rl.Black)
14+
rl.DrawRectangle(20, 100, 760, 400, rl.LightGray)
15+
rl.DrawText(Output, 30, 110, 20, rl.Black)
16+
17+
rl.EndDrawing()
18+
}
19+
20+
func HandleInput(input *string, maxInputLen int) bool {
21+
if rl.IsKeyPressed(rl.KeyEnter) {
22+
Output += "\n> " + *input
23+
return true // Indicates that Enter was pressed
24+
}
25+
26+
if rl.IsKeyPressed(rl.KeyBackspace) && len(*input) > 0 {
27+
*input = (*input)[:len(*input)-1]
28+
}
29+
30+
for char := rl.GetCharPressed(); char > 0; char = rl.GetCharPressed() {
31+
if len(*input) < maxInputLen {
32+
*input += string(char)
33+
}
34+
}
35+
return false
36+
}

utils/write.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ local filename = args[1]
88
local content = ""
99

1010
print("Enter content line by line. Type ':exit' to finish.")
11-
content = read_multiline_input()
11+
content = read_multiline_input_raylib()
1212

1313
write_file(filename, content)

0 commit comments

Comments
 (0)