add color and better format output
Jes Olson j3s@c3f.net
Sat, 01 Oct 2022 17:01:40 -0500
5 files changed,
51 insertions(+),
14 deletions(-)
A
color/color.go
@@ -0,0 +1,28 @@
+package color + +import ( + "fmt" +) + +const ( + red = 31 + green = 32 + blue = 34 +) + +func colorPrintln(s string, color int) { + colorString := fmt.Sprintf("\x1b[%dm%s\x1b[0m", color, s) + fmt.Println(colorString) +} + +func BluePrintln(s string) { + colorPrintln(s, blue) +} + +func RedPrintln(s string) { + colorPrintln(s, red) +} + +func GreenPrintln(s string) { + colorPrintln(s, green) +}
M
git/git.go
→
git/git.go
@@ -4,7 +4,7 @@ import (
"os" "strings" - "j3s.sh/zoa/utils" + "j3s.sh/zoa/color" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing"@@ -49,7 +49,7 @@ if branch != "" {
msg += "@" + branch cloneopts.ReferenceName = plumbing.ReferenceName(branch) } - utils.BluePrintln(msg) + color.BluePrintln(msg) _, err := git.PlainClone(clonePath, false, cloneopts) return err
M
shell/shell.go
→
shell/shell.go
@@ -9,6 +9,7 @@ "os/exec"
"path/filepath" "strings" + "j3s.sh/zoa/color" "j3s.sh/zoa/env" "j3s.sh/zoa/utils"@@ -44,11 +45,13 @@
func CallHandler(ctx context.Context, args []string) ([]string, error) { // hc := interp.HandlerCtx(ctx) + fmt.Println() if args[0] == "zoa-script" { subScript := filepath.Join(utils.ZoaRoot, "scripts", args[1]) // TODO: figure out how to get scripts to echo their names // when we resume their execution - utils.BluePrintln("-> " + subScript) + // fmt.Printf("$ %s\n", strings.Join(args, " ")) + color.BluePrintln("$ zoa-script " + args[1]) runScriptInSubshell(ctx, subScript) // args has to have something in it - true is a pretty safe bet@@ -99,22 +102,29 @@ f, err := parseFile(script)
if err != nil { return err } - r.Run(ctx, f) + for _, stmt := range f.Stmts { + err = r.Run(context.TODO(), stmt) + if err != nil { + color.RedPrintln(err.Error()) + } + } return nil } func RunScript(s string) { - utils.BluePrintln("(✿◠‿◠) zoa") + color.BluePrintln("(✿◠‿◠) zoa") script, err := parseFile(s) if err != nil { fmt.Printf("error in %s: %s\n", s, err) os.Exit(1) } - err = r.Run(context.TODO(), script) - if err != nil { - fmt.Println(err) - os.Exit(1) + for _, stmt := range script.Stmts { + err = r.Run(context.TODO(), stmt) + if err != nil { + color.RedPrintln(err.Error()) + } } + // err = r.Run(context.TODO(), script) } func parseFile(filename string) (*syntax.File, error) {
M
utils/utils.go
→
utils/utils.go
@@ -49,8 +49,3 @@
checksum := fmt.Sprintf("%x", h.Sum(nil)) return checksum, nil } - -func BluePrintln(s string) { - colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", 34, s) - fmt.Println(colored) -}