console/console.go

183 lines
5.8 KiB
Go
Raw Normal View History

2024-10-11 10:14:56 +08:00
package console
import (
_ "embed"
"fmt"
"reflect"
"strings"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/ssgo/u"
)
//go:embed console.ts
var consoleTS string
func init() {
obj := 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 := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Black(fmt.Sprint(args.Array(0)...)))
},
"red": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Red(fmt.Sprint(args.Array(0)...)))
},
"green": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Green(fmt.Sprint(args.Array(0)...)))
},
"yellow": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Yellow(fmt.Sprint(args.Array(0)...)))
},
"blue": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Blue(fmt.Sprint(args.Array(0)...)))
},
"magenta": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Magenta(fmt.Sprint(args.Array(0)...)))
},
"cyan": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Cyan(fmt.Sprint(args.Array(0)...)))
},
"white": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.White(fmt.Sprint(args.Array(0)...)))
},
"dim": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Dim(fmt.Sprint(args.Array(0)...)))
},
"italic": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.Italic(fmt.Sprint(args.Array(0)...)))
},
"bBlack": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BBlack(fmt.Sprint(args.Array(0)...)))
},
"bRed": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BRed(fmt.Sprint(args.Array(0)...)))
},
"bGreen": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BGreen(fmt.Sprint(args.Array(0)...)))
},
"bYellow": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BYellow(fmt.Sprint(args.Array(0)...)))
},
"bBlue": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BBlue(fmt.Sprint(args.Array(0)...)))
},
"bMagenta": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BMagenta(fmt.Sprint(args.Array(0)...)))
},
"bCyan": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BCyan(fmt.Sprint(args.Array(0)...)))
},
"bWhite": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm)
return vm.ToValue(u.BWhite(fmt.Sprint(args.Array(0)...)))
},
}
gojs.Register("apigo.cc/gojs/console", gojs.Module{
Object: obj,
Desc: "console api referer by nodejs console",
TsCode: consoleTS,
Example: "",
})
}
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
}
2024-11-08 16:14:34 +08:00
if len(args.Arguments) == 1 && args.Argument(0).ExportType() != nil && args.Argument(0).ExportType().Kind() == reflect.Map {
2024-10-11 10:14:56 +08:00
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...)
}
}