170 lines
5.4 KiB
Go
170 lines
5.4 KiB
Go
package js
|
|
|
|
import (
|
|
"apigo.cc/ai/ai/goja"
|
|
"fmt"
|
|
"github.com/ssgo/u"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
func RequireConsole() map[string]any {
|
|
return map[string]any{
|
|
"print": func(args goja.FunctionCall) goja.Value {
|
|
consolePrint(args, "print", nil)
|
|
return nil
|
|
},
|
|
"println": func(args goja.FunctionCall) goja.Value {
|
|
consolePrint(args, "log", nil)
|
|
return nil
|
|
},
|
|
"log": func(args goja.FunctionCall) goja.Value {
|
|
consolePrint(args, "log", nil)
|
|
return nil
|
|
},
|
|
"info": func(args goja.FunctionCall) goja.Value {
|
|
consolePrint(args, "info", nil)
|
|
return nil
|
|
},
|
|
"warn": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
consolePrint(args, "warn", vm)
|
|
return nil
|
|
},
|
|
"error": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
consolePrint(args, "error", vm)
|
|
return nil
|
|
},
|
|
"debug": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
consolePrint(args, "debug", vm)
|
|
return nil
|
|
},
|
|
"input": func(args goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
consolePrint(args, "print", nil)
|
|
line := ""
|
|
_, _ = fmt.Scanln(&line)
|
|
return vm.ToValue(line)
|
|
},
|
|
"black": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Black(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"red": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Red(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"green": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Green(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"yellow": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Yellow(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"blue": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Blue(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"magenta": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Magenta(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"cyan": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Cyan(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"white": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.White(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"dim": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Dim(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"italic": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.Italic(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bBlack": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BBlack(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bRed": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BRed(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bGreen": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BGreen(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bYellow": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BYellow(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bBlue": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BBlue(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bMagenta": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BMagenta(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bCyan": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BCyan(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
"bWhite": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
|
|
args := MakeArgs(&argsIn, vm)
|
|
return vm.ToValue(u.BWhite(fmt.Sprint(args.Arr(0)...)))
|
|
},
|
|
}
|
|
}
|
|
|
|
func consolePrint(args goja.FunctionCall, typ string, vm *goja.Runtime) {
|
|
arr := make([]any, len(args.Arguments))
|
|
textColor := u.TextNone
|
|
bgColor := u.BgNone
|
|
switch typ {
|
|
case "info":
|
|
textColor = u.TextCyan
|
|
bgColor = u.BgCyan
|
|
case "warn":
|
|
textColor = u.TextYellow
|
|
bgColor = u.BgYellow
|
|
case "error":
|
|
textColor = u.TextRed
|
|
bgColor = u.BgRed
|
|
}
|
|
|
|
if len(args.Arguments) == 1 && args.Argument(0).ExportType().Kind() == reflect.Map {
|
|
ex := args.Argument(0).ToObject(vm)
|
|
message := ex.Get("message")
|
|
stack := ex.Get("stack")
|
|
if message != nil && stack != nil {
|
|
fmt.Println(u.Color(u.String(message.Export()), textColor, u.BgNone))
|
|
fmt.Println(u.Color(u.String(stack.Export()), u.TextWhite, bgColor))
|
|
return
|
|
}
|
|
}
|
|
|
|
for i, arg := range args.Arguments {
|
|
if textColor != u.TextNone {
|
|
arr[i] = u.Color(u.StringP(arg.Export()), textColor, u.BgNone)
|
|
} else {
|
|
arr[i] = u.StringP(arg.Export())
|
|
}
|
|
}
|
|
|
|
if (typ == "warn" || typ == "error" || typ == "debug") && vm != nil {
|
|
callStacks := make([]string, 0)
|
|
for _, stack := range vm.CaptureCallStack(0, nil) {
|
|
callStacks = append(callStacks, u.Color(" "+stack.Position().String(), u.TextWhite, bgColor))
|
|
}
|
|
fmt.Println(arr...)
|
|
fmt.Println(strings.Join(callStacks, "\n"))
|
|
} else if typ == "print" {
|
|
fmt.Print(arr...)
|
|
} else {
|
|
fmt.Println(arr...)
|
|
}
|
|
}
|