Matrix/main.go
2024-04-15 01:25:33 +02:00

65 lines
1.5 KiB
Go

package main
import (
"fmt"
"log"
"github.com/buger/goterm"
"github.com/go-resty/resty/v2"
"github.com/joho/godotenv"
)
func output(client *resty.Client, url string, width, height int, arr [][]Cell) {
goterm.MoveCursor(0, 0)
// Prepare instructions for matrix
instructions := make([]interface{}, 0)
// Append all live cells as pixel instructions
for i := 0; i < width; i++ {
for j := 0; j < height; j++ {
if arr[i][j].live {
instructions = append(instructions, Pixel{X: i, Y: j, Endpoint: PIXEL})
goterm.Print("X")
continue
}
goterm.Print(" ")
}
goterm.Println()
}
// Append update instruction
instructions = append(instructions, Update{Endpoint: UPDATE})
// Send to matrix
sendRequest(client, url, instructions)
// Update terminal UI
goterm.Flush()
}
func main() {
// Load .env file if it exists
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
// Load env server data
url, width, height := loadMatrixData()
log.Printf("At '%s': %d x %d\n", url, width, height)
// Initialize terminal UI
goterm.Clear()
// Initialize resty client
client := resty.New()
// Initialize pixel color
sendRequest(client, url, []interface{}{Color{R: 255, G: 255, B: 255, Endpoint: COLOR}})
// Run Game of Life
done := setup(func(c [][]Cell) {
output(client, url, width, height, c)
}, width, height, 10)
// Wait for user input to quit
fmt.Scanln()
// Stop Game of Life, wait for routine
done <- true
}