ai_old/js/console.go

170 lines
5.4 KiB
Go
Raw Permalink Normal View History

2024-09-17 18:44:21 +08:00
package js
import (
"apigo.cc/ai/ai/goja"
2024-09-17 18:44:21 +08:00
"fmt"
"github.com/ssgo/u"
"reflect"
2024-09-17 18:44:21 +08:00
"strings"
)
2024-09-18 18:29:21 +08:00
func RequireConsole() map[string]any {
2024-09-17 18:44:21 +08:00
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)...)))
},
2024-09-17 18:44:21 +08:00
}
}
func consolePrint(args goja.FunctionCall, typ string, vm *goja.Runtime) {
arr := make([]any, len(args.Arguments))
textColor := u.TextNone
bgColor := u.BgNone
2024-09-17 18:44:21 +08:00
switch typ {
case "info":
textColor = u.TextCyan
bgColor = u.BgCyan
2024-09-17 18:44:21 +08:00
case "warn":
textColor = u.TextYellow
bgColor = u.BgYellow
2024-09-17 18:44:21 +08:00
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
}
2024-09-17 18:44:21 +08:00
}
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))
2024-09-17 18:44:21 +08:00
}
fmt.Println(arr...)
fmt.Println(strings.Join(callStacks, "\n"))
} else if typ == "print" {
fmt.Print(arr...)
} else {
fmt.Println(arr...)
}
}