From 31ef24cd624865a60225e7fcff266d89b315a303 Mon Sep 17 00:00:00 2001 From: Russell Matney Date: Thu, 6 Jun 2024 11:21:51 -0400 Subject: [PATCH] feat: Log.disable_colors() and Log.enable_colors() Color wrapping can now be globally disabled via Log.disable_colors(). This could help in some situations where the colors are not interpreted and the output is an unreadable mess of `[color=blah][/color]` tags. --- addons/log/log.gd | 139 +++++++++++++++++++++++--------------------- project.godot | 1 + src/ExampleScene.gd | 12 +++- test/log_test.gd | 15 ++++- 4 files changed, 98 insertions(+), 69 deletions(-) diff --git a/addons/log/log.gd b/addons/log/log.gd index 7a4e5c6..66e391d 100644 --- a/addons/log/log.gd +++ b/addons/log/log.gd @@ -13,7 +13,11 @@ static func assoc(opts: Dictionary, key: String, val): static var config = { max_array_size=20, - dictionary_skip_keys=["layer_0/tile_data"], + dictionary_skip_keys=[ + "layer_0/tile_data", # skip huge tilemap arrays + ], + color_scheme={}, + disable_colors=false, } static func get_max_array_size(): @@ -22,6 +26,15 @@ static func get_max_array_size(): static func get_dictionary_skip_keys(): return Log.config.get("dictionary_skip_keys", []) +static func get_disable_colors(): + return Log.config.get("disable_colors", false) + +static func disable_colors(): + Log.config["disable_colors"] = true + +static func enable_colors(): + Log.config["disable_colors"] = false + static func set_color_scheme(scheme): Log.config["color_scheme"] = scheme @@ -173,52 +186,56 @@ static func color_scheme(opts={}): scheme.merge(Log.COLORS_TERMINAL_SAFE) return scheme +static func should_use_color(opts={}): + if Log.get_disable_colors(): + return false + # supports per-print color skipping + if opts.get("disable_colors", false): + return false + return true + static func color_wrap(s, opts={}): - var use_color = opts.get("use_color", true) # don't rebuild the color scheme every time var colors = opts.get("built_color_scheme", color_scheme(opts)) - if use_color: - var color = opts.get("color") - if not color: - var s_type = opts.get("typeof", typeof(s)) - if s_type is String: - # type overwrites - color = colors.get(s_type) - elif s_type is int and s_type == TYPE_STRING: - # specific strings/punctuation - var s_trimmed = s.strip_edges() - if s_trimmed in colors: - color = colors.get(s_trimmed) - else: - # fallback string color - color = colors.get(s_type) + if not should_use_color(opts): + return str(s) + + var color = opts.get("color") + if not color: + var s_type = opts.get("typeof", typeof(s)) + if s_type is String: + # type overwrites + color = colors.get(s_type) + elif s_type is int and s_type == TYPE_STRING: + # specific strings/punctuation + var s_trimmed = s.strip_edges() + if s_trimmed in colors: + color = colors.get(s_trimmed) else: - # all other types + # fallback string color color = colors.get(s_type) + else: + # all other types + color = colors.get(s_type) - if color == null: - print("Log.gd could not determine color for object: %s type: (%s)" % [str(s), typeof(s)]) + if color == null: + print("Log.gd could not determine color for object: %s type: (%s)" % [str(s), typeof(s)]) - return "[color=%s]%s[/color]" % [color, s] - else: - return s + return "[color=%s]%s[/color]" % [color, s] ## overwrites ########################################################################### static var log_overwrites = { "Vector2": func(msg, opts): - if opts.get("use_color", true): - return '%s%s%s%s%s' % [ - Log.color_wrap("(", opts), - Log.color_wrap(msg.x, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(",", opts), - Log.color_wrap(msg.y, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(")", opts), - ] - else: - return '(%s,%s)' % [msg.x, msg.y], - } + return '%s%s%s%s%s' % [ + Log.color_wrap("(", opts), + Log.color_wrap(msg.x, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(",", opts), + Log.color_wrap(msg.y, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(")", opts), + ] + } static func register_overwrite(key, handler): # TODO warning on key exists? @@ -232,7 +249,6 @@ static func register_overwrite(key, handler): # returns the passed object as a decorated string static func to_pretty(msg, opts={}): var newlines = opts.get("newlines", false) - var use_color = opts.get("use_color", true) var indent_level = opts.get("indent_level", 0) if not "indent_level" in opts: opts["indent_level"] = indent_level @@ -301,11 +317,8 @@ static func to_pretty(msg, opts={}): + range(indent_level)\ .map(func(_i): return "\t")\ .reduce(func(a, b): return str(a, b), "") - if use_color: - var key = Log.color_wrap('"%s"' % k, Log.assoc(opts, "typeof", "dict_key")) - tmp += "%s: %s" % [key, val] - else: - tmp += '"%s": %s' % [k, val] + var key = Log.color_wrap('"%s"' % k, Log.assoc(opts, "typeof", "dict_key")) + tmp += "%s: %s" % [key, val] if last and str(k) != str(last): tmp += Log.color_wrap(", ", opts) tmp += Log.color_wrap(" }", opts) @@ -330,33 +343,27 @@ static func to_pretty(msg, opts={}): return log_overwrites.get("Vector2").call(msg, opts) elif msg is Vector3 or msg is Vector3i: - if use_color: - return '%s%s%s%s%s%s%s' % [ - Log.color_wrap("(", opts), - Log.color_wrap(msg.x, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(",", opts), - Log.color_wrap(msg.y, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(",", opts), - Log.color_wrap(msg.z, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(")", opts), - ] - else: - return '(%s,%s,%s)' % [msg.x, msg.y, msg.z] + return '%s%s%s%s%s%s%s' % [ + Log.color_wrap("(", opts), + Log.color_wrap(msg.x, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(",", opts), + Log.color_wrap(msg.y, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(",", opts), + Log.color_wrap(msg.z, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(")", opts), + ] elif msg is Vector4 or msg is Vector4i: - if use_color: - return '%s%s%s%s%s%s%s%s%s' % [ - Log.color_wrap("(", opts), - Log.color_wrap(msg.x, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(",", opts), - Log.color_wrap(msg.y, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(",", opts), - Log.color_wrap(msg.z, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(",", opts), - Log.color_wrap(msg.w, Log.assoc(opts, "typeof", "vector_value")), - Log.color_wrap(")", opts), - ] - else: - return '(%s,%s,%s,%s)' % [msg.x, msg.y, msg.z, msg.w] + return '%s%s%s%s%s%s%s%s%s' % [ + Log.color_wrap("(", opts), + Log.color_wrap(msg.x, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(",", opts), + Log.color_wrap(msg.y, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(",", opts), + Log.color_wrap(msg.z, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(",", opts), + Log.color_wrap(msg.w, Log.assoc(opts, "typeof", "vector_value")), + Log.color_wrap(")", opts), + ] # packed scene elif msg is PackedScene: diff --git a/project.godot b/project.godot index 000e74a..58565d1 100644 --- a/project.godot +++ b/project.godot @@ -22,3 +22,4 @@ enabled=PackedStringArray("res://addons/gdUnit4/plugin.cfg", "res://addons/log/p [gdunit4] report/godot/push_error=true +settings/common/update_notification_enabled=false diff --git a/src/ExampleScene.gd b/src/ExampleScene.gd index 0d2d3e6..f10c2c6 100644 --- a/src/ExampleScene.gd +++ b/src/ExampleScene.gd @@ -3,6 +3,7 @@ extends CanvasLayer func _enter_tree(): Log.set_colors_pretty() + # Log.disable_colors() class ExampleObj: var val @@ -15,7 +16,11 @@ class ExampleObj: @export var some_custom_types : Array[SomeResource] func _ready(): - Log.pr("Hi there!", [1, 2.0, Vector2(3, 4), Vector3i(1, 3, 0), Vector4(1.1, 2.2, 3.4, 4000)]) + Log.pr("Hi there!") + + Log.pr("an array of vectors", [ + 1, 2.0, Vector2(3, 4), Vector3i(1, 3, 0), Vector4(1.1, 2.2, 3.4, 4000) + ]) Log.pr("example object", ExampleObj.new("example val")) Log.pr("with a Vector2i", ExampleObj.new(Vector2i(0, 6))) @@ -27,6 +32,9 @@ func _ready(): Log.prn("custom types", some_custom_types) - Log.pr("custom colors", 1) + Log.pr("custom colors") print_rich(Log.to_pretty(1)) print_rich(Log.to_pretty(1, {color_scheme={TYPE_INT: "purple"}})) + + Log.pr("disabled colors") + print_rich(Log.to_pretty(1, {disable_colors=true})) diff --git a/test/log_test.gd b/test/log_test.gd index 73d2695..c1b70a9 100644 --- a/test/log_test.gd +++ b/test/log_test.gd @@ -37,7 +37,7 @@ func test_already_colorized_string(): ## color ########################################## func test_color(): - # TODO + # test for printing colors, not log.gd color nuances pass ## numbers ########################################## @@ -247,3 +247,16 @@ func test_color_scheme_overwriting(): # reset colors Log.set_colors_termsafe() + +func test_disable_colors_to_pretty(): + assert_str(Log.to_pretty(1)).is_equal("[color=green]1[/color]") + assert_str(Log.to_pretty(1, {disable_colors=true})).is_equal("1") + +func test_disable_colors_via_config(): + assert_str(Log.to_pretty(1)).is_equal("[color=green]1[/color]") + + Log.disable_colors() + assert_str(Log.to_pretty(1)).is_equal("1") + + Log.enable_colors() + assert_str(Log.to_pretty(1)).is_equal("[color=green]1[/color]")