From 5347f1c59c99d18addbc81b5323471a2040b6cd9 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Sat, 29 Oct 2022 09:24:30 +0300 Subject: [PATCH] Allow loading ~/.yalrc in the REPL This allows a cute startup file: ``` ;; Get our hostname (set! hostname (fn* () (trim (slurp "/etc/hostname")))) (set! trim (fn* (str) "Trim all leading/trailing whitespace from the given string." (let* (res (match "^[ \t\r\n]*([^ \t\r\n]+)[ \t\r\n]*" str)) (if (list? res) (car (cdr res)) str)))) ;; Show ourselves (print "This is ~/.yalrc on %s - %s %s" (hostname) (os) (arch) ) ``` --- main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.go b/main.go index c041534..5befb5b 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "fmt" "math/rand" "os" + "path" "regexp" "sort" "strings" @@ -257,10 +258,34 @@ func main() { fmt.Printf("YAL version %s\n", version) reader := bufio.NewReader(os.Stdin) + // + // Get the home directory, and load ~/.yalrc if present + // + home := os.Getenv("HOME") + if home != "" { + + // Build the path + file := path.Join(home, ".yalrc") + + // Read the content + content, err := os.ReadFile(file) + if err == nil { + + // Execute the contents + out := LISP.Execute(ENV, string(content)) + if _, ok := out.(primitive.Error); ok { + fmt.Printf("Error executing ~/.yalrc %v\n", out) + } + } + } + src := "" for { if src == "" { fmt.Printf("\n> ") + } else { + fmt.Printf(" ") + } line, _ := reader.ReadString('\n')