refactoring the core app logic a bit
parent
55ddb818d9
commit
2819cfffc8
@ -0,0 +1,69 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/EngoEngine/ecs"
|
||||||
|
)
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
W *ecs.World
|
||||||
|
L *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(level string) *App {
|
||||||
|
var logLevel slog.Level
|
||||||
|
switch level {
|
||||||
|
case "INFO", "info":
|
||||||
|
logLevel = slog.LevelInfo
|
||||||
|
case "DEBUG", "debug":
|
||||||
|
logLevel = slog.LevelDebug
|
||||||
|
case "ERROR", "error":
|
||||||
|
logLevel = slog.LevelError
|
||||||
|
}
|
||||||
|
|
||||||
|
l := slog.New(slog.NewTextHandler(
|
||||||
|
os.Stderr, &slog.HandlerOptions{
|
||||||
|
Level: logLevel,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
world := &ecs.World{}
|
||||||
|
|
||||||
|
return &App{
|
||||||
|
L: l,
|
||||||
|
W: world,
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run(ctx context.Context) {
|
||||||
|
go func() {
|
||||||
|
t := time.NewTicker(time.Millisecond * 30)
|
||||||
|
for range t.C {
|
||||||
|
a.W.Update(1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
var line string
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
a.L.Error("cancelled")
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
if scanner.Scan() {
|
||||||
|
line = scanner.Text()
|
||||||
|
}
|
||||||
|
a.L.Info("scanning input")
|
||||||
|
a.L.With(slog.String("input", line)).Info("received command")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue