Very naive REPL

This commit is contained in:
2025-10-21 21:52:16 +01:00
parent d52f8bcfa5
commit ba3dcebb27
4 changed files with 131 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
// Package repl Basic UI for the user
package repl
import (
"bufio"
"fmt"
"os"
)
var DefaultCfg = REPLCfg{
PS1: "\r> ",
}
type REPLCfg struct {
PS1 string
}
type REPL struct {
Cfg REPLCfg
Commands map[string]Command
}
func NewREPL(cfg REPLCfg) *REPL {
return &REPL{
Cfg: cfg,
Commands: map[string]Command{
helpCmd.Name: helpCmd,
clearCmd.Name: clearCmd,
exitCmd.Name: exitCmd,
},
}
}
func (r *REPL) printPrompt() {
_, _ = os.Stdout.Write([]byte(r.Cfg.PS1))
}
func (r *REPL) Start() {
reader := bufio.NewScanner(os.Stdin)
r.printPrompt()
for reader.Scan() {
input := reader.Text()
args := parseInput(input)
command, exists := r.Commands[args[0]]
if exists {
_ = command.Action(r, args[1:])
} else {
fmt.Printf("No such command `%s`\n", args[0])
}
r.printPrompt()
}
}