Skip to content

Commit

Permalink
feat: proof of concept for swapping color schemes
Browse files Browse the repository at this point in the history
Inits a Log.config dictionary that pulls the color scheme from
config.color_scheme. The colors can also be passed to Log.to_pretty in
the opts dict.

Once the schemes are being merged, it'll be possible to overwrite
specific types without clearing the rest of the colors.

Ideally the prettier colors would be opt-ed into by the editor-launched
games, and the term-safe would be used in other circumstances (test
runners, 'built' production games)

A similar feature should make it easy to opt-out of colors - I wonder if
it's easy to read a `--log-no-colors` arg from the command line.
  • Loading branch information
russmatney committed Apr 13, 2024
1 parent 54d8bb2 commit b8e5958
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
57 changes: 39 additions & 18 deletions addons/log/log.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@
extends Object
class_name Log

## helpers ####################################

static func assoc(opts: Dictionary, key: String, val):
var _opts = opts.duplicate(true)
_opts[key] = val
return _opts

## prefix ###########################################################################
## config ####################################

static func log_prefix(stack):
if len(stack) > 1:
var call_site = stack[1]
var basename = call_site["source"].get_file().get_basename()
var line_num = str(call_site.get("line", 0))
if call_site["source"].match("*/test/*"):
return "{" + basename + ":" + line_num + "}: "
elif call_site["source"].match("*/addons/*"):
return "<" + basename + ":" + line_num + ">: "
else:
return "[" + basename + ":" + line_num + "]: "
static var config = {}

## colors ###########################################################################

Expand Down Expand Up @@ -110,7 +102,7 @@ static var COLORS_PRETTY_V1 = {
"dict_key": "cadet_blue",
"vector_value": "cornflower_blue",
"class_name": "cadet_blue",
TYPE_NIL: "pink",
TYPE_NIL: "coral",
TYPE_BOOL: "pink",
TYPE_INT: "cornflower_blue",
TYPE_FLOAT: "cornflower_blue",
Expand Down Expand Up @@ -151,14 +143,31 @@ static var COLORS_PRETTY_V1 = {
TYPE_MAX: "pink",
}

## set color scheme ####################################

static func set_colors_termsafe():
set_color_scheme(Log.COLORS_TERMINAL_SAFE)

static func set_colors_pretty():
set_color_scheme(Log.COLORS_PRETTY_V1)

static func set_color_scheme(scheme):
config["color_scheme"] = scheme

static func color_scheme(opts={}):
return Log.COLORS_TERMINAL_SAFE
# return Log.COLORS_PRETTY_V1
# TODO merge schemes down to support partial color overwrites
var scheme = opts.get("color_scheme")
if not scheme:
scheme = config.get("color_scheme")
if not scheme:
# TODO default to pretty colors when launched from editor
# otherwise, use term-safe colors
scheme = Log.COLORS_TERMINAL_SAFE
return scheme

static func color_wrap(s, opts={}):
var use_color = opts.get("use_color", true)
var colors = opts.get("color_scheme", color_scheme(opts))
var colors = color_scheme(opts)

if use_color:
var color = opts.get("color")
Expand Down Expand Up @@ -372,6 +381,18 @@ static func to_pretty(msg, opts={}):

## to_printable ###########################################################################

static func log_prefix(stack):
if len(stack) > 1:
var call_site = stack[1]
var basename = call_site["source"].get_file().get_basename()
var line_num = str(call_site.get("line", 0))
if call_site["source"].match("*/test/*"):
return "{" + basename + ":" + line_num + "}: "
elif call_site["source"].match("*/addons/*"):
return "<" + basename + ":" + line_num + ">: "
else:
return "[" + basename + ":" + line_num + "]: "

static func to_printable(msgs, opts={}):
var stack = opts.get("stack", [])
var pretty = opts.get("pretty", true)
Expand All @@ -398,11 +419,11 @@ static func to_printable(msgs, opts={}):
m += "%s " % str(msg)
return m.trim_suffix(" ")

## public print fns ###########################################################################

static func is_not_default(v):
return not v is String or (v is String and v != "ZZZDEF")

## public print fns ###########################################################################

static func pr(msg, msg2="ZZZDEF", msg3="ZZZDEF", msg4="ZZZDEF", msg5="ZZZDEF", msg6="ZZZDEF", msg7="ZZZDEF"):
var msgs = [msg, msg2, msg3, msg4, msg5, msg6, msg7]
msgs = msgs.filter(Log.is_not_default)
Expand Down
15 changes: 15 additions & 0 deletions test/log_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,18 @@ func test_custom_resource_register_overwrite():
assert_str(val).is_equal(
"[color=red]{ [/color][color=magenta]\"name\"[/color]: [color=pink]Hanz[/color][color=red], [/color][color=magenta]\"level\"[/color]: [color=green]3[/color][color=red] }[/color]"
)

## color schemes ##########################################

func test_null_alt_colors():
Log.set_colors_termsafe()
var val = Log.to_pretty(null)
assert_str(val).is_equal("[color=pink]<null>[/color]")

Log.set_colors_pretty()
val = Log.to_pretty(null)
assert_str(val).is_equal("[color=coral]<null>[/color]")

Log.set_colors_termsafe()
val = Log.to_pretty(null)
assert_str(val).is_equal("[color=pink]<null>[/color]")
4 changes: 4 additions & 0 deletions todo.org
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func to_printable():
*** _not_ a production log-to-file helper
but maybe that's a future direction?
*** prior art
** [ ] docs for public api
*** Log.to_pretty
*** `to_pretty()`, `to_printable()`, `data()`
*** Log.set_color_scheme(scheme) and related
** [ ] readme link, docs install instructions
** [ ] Example scene should have buttons to print each thing
the example outputs could also colorize and print the source code - that output might
Expand Down

0 comments on commit b8e5958

Please sign in to comment.