Adding log.gd
This commit is contained in:
parent
eb32d6614e
commit
4522259397
547 changed files with 46844 additions and 0 deletions
61
addons/gdUnit4/src/cmd/CmdArgumentParser.gd
Normal file
61
addons/gdUnit4/src/cmd/CmdArgumentParser.gd
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
class_name CmdArgumentParser
|
||||
extends RefCounted
|
||||
|
||||
var _options :CmdOptions
|
||||
var _tool_name :String
|
||||
var _parsed_commands :Dictionary = Dictionary()
|
||||
|
||||
|
||||
func _init(p_options :CmdOptions, p_tool_name :String):
|
||||
_options = p_options
|
||||
_tool_name = p_tool_name
|
||||
|
||||
|
||||
func parse(args :Array, ignore_unknown_cmd := false) -> GdUnitResult:
|
||||
_parsed_commands.clear()
|
||||
|
||||
# parse until first program argument
|
||||
while not args.is_empty():
|
||||
var arg :String = args.pop_front()
|
||||
if arg.find(_tool_name) != -1:
|
||||
break
|
||||
|
||||
if args.is_empty():
|
||||
return GdUnitResult.empty()
|
||||
|
||||
# now parse all arguments
|
||||
while not args.is_empty():
|
||||
var cmd :String = args.pop_front()
|
||||
var option := _options.get_option(cmd)
|
||||
|
||||
if option:
|
||||
if _parse_cmd_arguments(option, args) == -1:
|
||||
return GdUnitResult.error("The '%s' command requires an argument!" % option.short_command())
|
||||
elif not ignore_unknown_cmd:
|
||||
return GdUnitResult.error("Unknown '%s' command!" % cmd)
|
||||
return GdUnitResult.success(_parsed_commands.values())
|
||||
|
||||
|
||||
func options() -> CmdOptions:
|
||||
return _options
|
||||
|
||||
|
||||
func _parse_cmd_arguments(option :CmdOption, args :Array) -> int:
|
||||
var command_name := option.short_command()
|
||||
var command :CmdCommand = _parsed_commands.get(command_name, CmdCommand.new(command_name))
|
||||
|
||||
if option.has_argument():
|
||||
if not option.is_argument_optional() and args.is_empty():
|
||||
return -1
|
||||
if _is_next_value_argument(args):
|
||||
command.add_argument(args.pop_front())
|
||||
elif not option.is_argument_optional():
|
||||
return -1
|
||||
_parsed_commands[command_name] = command
|
||||
return 0
|
||||
|
||||
|
||||
func _is_next_value_argument(args :Array) -> bool:
|
||||
if args.is_empty():
|
||||
return false
|
||||
return _options.get_option(args[0]) == null
|
||||
26
addons/gdUnit4/src/cmd/CmdCommand.gd
Normal file
26
addons/gdUnit4/src/cmd/CmdCommand.gd
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
class_name CmdCommand
|
||||
extends RefCounted
|
||||
|
||||
var _name: String
|
||||
var _arguments: PackedStringArray
|
||||
|
||||
|
||||
func _init(p_name: String, p_arguments: = []):
|
||||
_name = p_name
|
||||
_arguments = PackedStringArray(p_arguments)
|
||||
|
||||
|
||||
func name() -> String:
|
||||
return _name
|
||||
|
||||
|
||||
func arguments() -> PackedStringArray:
|
||||
return _arguments
|
||||
|
||||
|
||||
func add_argument(arg: String) -> void:
|
||||
_arguments.append(arg)
|
||||
|
||||
|
||||
func _to_string():
|
||||
return "%s:%s" % [_name, ", ".join(_arguments)]
|
||||
105
addons/gdUnit4/src/cmd/CmdCommandHandler.gd
Normal file
105
addons/gdUnit4/src/cmd/CmdCommandHandler.gd
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
class_name CmdCommandHandler
|
||||
extends RefCounted
|
||||
|
||||
const CB_SINGLE_ARG = 0
|
||||
const CB_MULTI_ARGS = 1
|
||||
|
||||
var _cmd_options :CmdOptions
|
||||
# holds the command callbacks by key:<cmd_name>:String and value: [<cb single arg>, <cb multible args>]:Array
|
||||
var _command_cbs :Dictionary
|
||||
|
||||
const NO_CB := Callable()
|
||||
|
||||
# we only able to check cb function name since Godot 3.3.x
|
||||
var _enhanced_fr_test := false
|
||||
|
||||
|
||||
func _init(cmd_options: CmdOptions):
|
||||
_cmd_options = cmd_options
|
||||
var major: int = Engine.get_version_info()["major"]
|
||||
var minor: int = Engine.get_version_info()["minor"]
|
||||
if major == 3 and minor == 3:
|
||||
_enhanced_fr_test = true
|
||||
|
||||
|
||||
# register a callback function for given command
|
||||
# cmd_name short name of the command
|
||||
# fr_arg a funcref to a function with a single argument
|
||||
func register_cb(cmd_name: String, cb: Callable = NO_CB) -> CmdCommandHandler:
|
||||
var registered_cb: Array = _command_cbs.get(cmd_name, [NO_CB, NO_CB])
|
||||
if registered_cb[CB_SINGLE_ARG]:
|
||||
push_error("A function for command '%s' is already registered!" % cmd_name)
|
||||
return self
|
||||
registered_cb[CB_SINGLE_ARG] = cb
|
||||
_command_cbs[cmd_name] = registered_cb
|
||||
return self
|
||||
|
||||
|
||||
# register a callback function for given command
|
||||
# cb a funcref to a function with a variable number of arguments but expects all parameters to be passed via a single Array.
|
||||
func register_cbv(cmd_name: String, cb: Callable) -> CmdCommandHandler:
|
||||
var registered_cb: Array = _command_cbs.get(cmd_name, [NO_CB, NO_CB])
|
||||
if registered_cb[CB_MULTI_ARGS]:
|
||||
push_error("A function for command '%s' is already registered!" % cmd_name)
|
||||
return self
|
||||
registered_cb[CB_MULTI_ARGS] = cb
|
||||
_command_cbs[cmd_name] = registered_cb
|
||||
return self
|
||||
|
||||
|
||||
func _validate() -> GdUnitResult:
|
||||
var errors: = PackedStringArray()
|
||||
var registered_cbs: = Dictionary()
|
||||
|
||||
for cmd_name in _command_cbs.keys():
|
||||
var cb: Callable = _command_cbs[cmd_name][CB_SINGLE_ARG] if _command_cbs[cmd_name][CB_SINGLE_ARG] else _command_cbs[cmd_name][CB_MULTI_ARGS]
|
||||
if cb != NO_CB and not cb.is_valid():
|
||||
errors.append("Invalid function reference for command '%s', Check the function reference!" % cmd_name)
|
||||
if _cmd_options.get_option(cmd_name) == null:
|
||||
errors.append("The command '%s' is unknown, verify your CmdOptions!" % cmd_name)
|
||||
# verify for multiple registered command callbacks
|
||||
if _enhanced_fr_test and cb != NO_CB:
|
||||
var cb_method: = cb.get_method()
|
||||
if registered_cbs.has(cb_method):
|
||||
var already_registered_cmd = registered_cbs[cb_method]
|
||||
errors.append("The function reference '%s' already registerd for command '%s'!" % [cb_method, already_registered_cmd])
|
||||
else:
|
||||
registered_cbs[cb_method] = cmd_name
|
||||
if errors.is_empty():
|
||||
return GdUnitResult.success(true)
|
||||
else:
|
||||
return GdUnitResult.error("\n".join(errors))
|
||||
|
||||
|
||||
func execute(commands :Array) -> GdUnitResult:
|
||||
var result := _validate()
|
||||
if result.is_error():
|
||||
return result
|
||||
for index in commands.size():
|
||||
var cmd :CmdCommand = commands[index]
|
||||
assert(cmd is CmdCommand) #,"commands contains invalid command object '%s'" % cmd)
|
||||
var cmd_name := cmd.name()
|
||||
if _command_cbs.has(cmd_name):
|
||||
var cb_s :Callable = _command_cbs.get(cmd_name)[CB_SINGLE_ARG]
|
||||
var arguments := cmd.arguments()
|
||||
var cmd_option := _cmd_options.get_option(cmd_name)
|
||||
var argument = arguments[0] if arguments.size() > 0 else null
|
||||
match cmd_option.type():
|
||||
TYPE_BOOL:
|
||||
argument = true if argument == "true" else false
|
||||
if cb_s and arguments.size() == 0:
|
||||
cb_s.call()
|
||||
elif cb_s:
|
||||
cb_s.call(argument)
|
||||
else:
|
||||
var cb_m :Callable = _command_cbs.get(cmd_name)[CB_MULTI_ARGS]
|
||||
# we need to find the method and determin the arguments to call the right function
|
||||
for m in cb_m.get_object().get_method_list():
|
||||
if m["name"] == cb_m.get_method():
|
||||
if m["args"].size() > 1:
|
||||
cb_m.callv(arguments)
|
||||
break
|
||||
else:
|
||||
cb_m.call(arguments)
|
||||
break
|
||||
return GdUnitResult.success(true)
|
||||
147
addons/gdUnit4/src/cmd/CmdConsole.gd
Normal file
147
addons/gdUnit4/src/cmd/CmdConsole.gd
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
# prototype of console with CSI support
|
||||
# https://notes.burke.libbey.me/ansi-escape-codes/
|
||||
class_name CmdConsole
|
||||
extends RefCounted
|
||||
|
||||
const BOLD = 0x1
|
||||
const ITALIC = 0x2
|
||||
const UNDERLINE = 0x4
|
||||
|
||||
const __CSI_BOLD = "[1m"
|
||||
const __CSI_ITALIC = "[3m"
|
||||
const __CSI_UNDERLINE = "[4m"
|
||||
|
||||
enum {
|
||||
COLOR_TABLE,
|
||||
COLOR_RGB
|
||||
}
|
||||
|
||||
|
||||
# Control Sequence Introducer
|
||||
#var csi := PackedByteArray([0x1b]).get_string_from_ascii()
|
||||
var _debug_show_color_codes := false
|
||||
var _color_mode = COLOR_TABLE
|
||||
|
||||
|
||||
func color(p_color :Color) -> CmdConsole:
|
||||
# using color table 16 - 231 a 6 x 6 x 6 RGB color cube (16 + R * 36 + G * 6 + B)
|
||||
if _color_mode == COLOR_TABLE:
|
||||
@warning_ignore("integer_division")
|
||||
var c2 = 16 + (int(p_color.r8/42) * 36) + (int(p_color.g8/42) * 6) + int(p_color.b8/42)
|
||||
if _debug_show_color_codes:
|
||||
printraw("%6d" % [c2])
|
||||
printraw("[38;5;%dm" % c2 )
|
||||
else:
|
||||
printraw("[38;2;%d;%d;%dm" % [p_color.r8, p_color.g8, p_color.b8] )
|
||||
return self
|
||||
|
||||
|
||||
func save_cursor() -> CmdConsole:
|
||||
printraw("[s")
|
||||
return self
|
||||
|
||||
|
||||
func restore_cursor() -> CmdConsole:
|
||||
printraw("[u")
|
||||
return self
|
||||
|
||||
|
||||
func end_color() -> CmdConsole:
|
||||
printraw("[0m")
|
||||
return self
|
||||
|
||||
|
||||
func row_pos(row :int) -> CmdConsole:
|
||||
printraw("[%d;0H" % row )
|
||||
return self
|
||||
|
||||
|
||||
func scrollArea(from :int, to :int ) -> CmdConsole:
|
||||
printraw("[%d;%dr" % [from ,to])
|
||||
return self
|
||||
|
||||
|
||||
func progressBar(p_progress :int, p_color :Color = Color.POWDER_BLUE) -> CmdConsole:
|
||||
if p_progress < 0:
|
||||
p_progress = 0
|
||||
if p_progress > 100:
|
||||
p_progress = 100
|
||||
color(p_color)
|
||||
printraw("[%-50s] %-3d%%\r" % ["".lpad(int(p_progress/2.0), "■").rpad(50, "-"), p_progress])
|
||||
end_color()
|
||||
return self
|
||||
|
||||
|
||||
func printl(value :String) -> CmdConsole:
|
||||
printraw(value)
|
||||
return self
|
||||
|
||||
|
||||
func new_line() -> CmdConsole:
|
||||
prints()
|
||||
return self
|
||||
|
||||
|
||||
func reset() -> CmdConsole:
|
||||
return self
|
||||
|
||||
|
||||
func bold(enable :bool) -> CmdConsole:
|
||||
if enable:
|
||||
printraw(__CSI_BOLD)
|
||||
return self
|
||||
|
||||
|
||||
func italic(enable :bool) -> CmdConsole:
|
||||
if enable:
|
||||
printraw(__CSI_ITALIC)
|
||||
return self
|
||||
|
||||
|
||||
func underline(enable :bool) -> CmdConsole:
|
||||
if enable:
|
||||
printraw(__CSI_UNDERLINE)
|
||||
return self
|
||||
|
||||
|
||||
func prints_error(message :String) -> CmdConsole:
|
||||
return color(Color.CRIMSON).printl(message).end_color().new_line()
|
||||
|
||||
|
||||
func prints_warning(message :String) -> CmdConsole:
|
||||
return color(Color.GOLDENROD).printl(message).end_color().new_line()
|
||||
|
||||
|
||||
func prints_color(p_message :String, p_color :Color, p_flags := 0) -> CmdConsole:
|
||||
return print_color(p_message, p_color, p_flags).new_line()
|
||||
|
||||
|
||||
func print_color(p_message :String, p_color :Color, p_flags := 0) -> CmdConsole:
|
||||
return color(p_color)\
|
||||
.bold(p_flags&BOLD == BOLD)\
|
||||
.italic(p_flags&ITALIC == ITALIC)\
|
||||
.underline(p_flags&UNDERLINE == UNDERLINE)\
|
||||
.printl(p_message)\
|
||||
.end_color()
|
||||
|
||||
|
||||
func print_color_table():
|
||||
prints_color("Color Table 6x6x6", Color.ANTIQUE_WHITE)
|
||||
_debug_show_color_codes = true
|
||||
for green in range(0, 6):
|
||||
for red in range(0, 6):
|
||||
for blue in range(0, 6):
|
||||
print_color("████████ ", Color8(red*42, green*42, blue*42))
|
||||
new_line()
|
||||
new_line()
|
||||
|
||||
prints_color("Color Table RGB", Color.ANTIQUE_WHITE)
|
||||
_color_mode = COLOR_RGB
|
||||
for green in range(0, 6):
|
||||
for red in range(0, 6):
|
||||
for blue in range(0, 6):
|
||||
print_color("████████ ", Color8(red*42, green*42, blue*42))
|
||||
new_line()
|
||||
new_line()
|
||||
_color_mode = COLOR_TABLE
|
||||
_debug_show_color_codes = false
|
||||
61
addons/gdUnit4/src/cmd/CmdOption.gd
Normal file
61
addons/gdUnit4/src/cmd/CmdOption.gd
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
class_name CmdOption
|
||||
extends RefCounted
|
||||
|
||||
|
||||
var _commands :PackedStringArray
|
||||
var _help :String
|
||||
var _description :String
|
||||
var _type :int
|
||||
var _arg_optional :bool = false
|
||||
|
||||
|
||||
# constructs a command option by given arguments
|
||||
# commands : a string with comma separated list of available commands begining with the short form
|
||||
# help: a help text show howto use
|
||||
# description: a full description of the command
|
||||
# type: the argument type
|
||||
# arg_optional: defines of the argument optional
|
||||
func _init(p_commands :String, p_help :String, p_description :String, p_type :int = TYPE_NIL, p_arg_optional :bool = false):
|
||||
_commands = p_commands.replace(" ", "").replace("\t", "").split(",")
|
||||
_help = p_help
|
||||
_description = p_description
|
||||
_type = p_type
|
||||
_arg_optional = p_arg_optional
|
||||
|
||||
|
||||
func commands() -> PackedStringArray:
|
||||
return _commands
|
||||
|
||||
|
||||
func short_command() -> String:
|
||||
return _commands[0]
|
||||
|
||||
|
||||
func help() -> String:
|
||||
return _help
|
||||
|
||||
|
||||
func description() -> String:
|
||||
return _description
|
||||
|
||||
|
||||
func type() -> int:
|
||||
return _type
|
||||
|
||||
|
||||
func is_argument_optional() -> bool:
|
||||
return _arg_optional
|
||||
|
||||
|
||||
func has_argument() -> bool:
|
||||
return _type != TYPE_NIL
|
||||
|
||||
|
||||
func describe() -> String:
|
||||
if help().is_empty():
|
||||
return " %-32s %s \n" % [commands(), description()]
|
||||
return " %-32s %s \n %-32s %s\n" % [commands(), description(), "", help()]
|
||||
|
||||
|
||||
func _to_string():
|
||||
return describe()
|
||||
31
addons/gdUnit4/src/cmd/CmdOptions.gd
Normal file
31
addons/gdUnit4/src/cmd/CmdOptions.gd
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
class_name CmdOptions
|
||||
extends RefCounted
|
||||
|
||||
|
||||
var _default_options :Array
|
||||
var _advanced_options :Array
|
||||
|
||||
|
||||
func _init(p_options :Array = Array(), p_advanced_options :Array = Array()):
|
||||
# default help options
|
||||
_default_options = p_options
|
||||
_advanced_options = p_advanced_options
|
||||
|
||||
|
||||
func default_options() -> Array:
|
||||
return _default_options
|
||||
|
||||
|
||||
func advanced_options() -> Array:
|
||||
return _advanced_options
|
||||
|
||||
|
||||
func options() -> Array:
|
||||
return default_options() + advanced_options()
|
||||
|
||||
|
||||
func get_option(cmd :String) -> CmdOption:
|
||||
for option in options():
|
||||
if Array(option.commands()).has(cmd):
|
||||
return option
|
||||
return null
|
||||
Loading…
Add table
Add a link
Reference in a new issue