Skip to content

Commit

Permalink
feat: Log.disable_colors() and Log.enable_colors()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
russmatney committed Jun 6, 2024
1 parent 92b62b0 commit 31ef24c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 69 deletions.
139 changes: 73 additions & 66 deletions addons/log/log.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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

Expand Down Expand Up @@ -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?
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions src/ExampleScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extends CanvasLayer

func _enter_tree():
Log.set_colors_pretty()
# Log.disable_colors()

class ExampleObj:
var val
Expand All @@ -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)))
Expand All @@ -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}))
15 changes: 14 additions & 1 deletion test/log_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##########################################
Expand Down Expand Up @@ -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]")

0 comments on commit 31ef24c

Please sign in to comment.