@shoobdaloob
Hello! I recently added this macro to my setup and had to make a few changes to get it working.
For one, your .cfg’s should definitely contain an instance of [respond], whether empty or filled with your specific config values. Klipper will only enable M118/RESPOND functionality if [respond] is in the configuration.
I ran into some problems with the prefix handling and also wanted to be able to color the prefix portion, so I made a few modifications. Specifically and aside from fixing an issue with the prefix handling, I made it so that the prefix can either be seperately colored by providing a valid PREFIX_COLOR parameter OR omit that parameter to use the same color supplied for COLOR, if available and valid. Hopefully this helps!
[gcode_macro EXTENDED_RESPOND]
gcode:
{% set colors = ('primary', 'secondary', 'accent', 'info', 'success', 'error', 'warning') %}
{% if params.TYPE is defined and params.MSG is defined %} # if TYPE and MSG are defined, just use the default RESPOND
{% if params.TYPE in ('echo', 'echo_no_space', 'command', 'error') and params.TYPE != '' %}
{% set type = 'TYPE=' + params.TYPE|string %}
{% else %}
RESPOND TYPE=error MSG="RESPOND TYPE '{params.TYPE}' is invalid. Must be one of 'echo', 'echo_no_space', 'command' or 'error'"
{% endif %}
{% endif %}
{% set color = "DEFAULT" %} # default garbage value
{% if params.MSG is defined and params.COLOR is defined %}
{% set color = params.COLOR|lower %}
{% if color in colors %} # valid color supplied
{% set msg = 'MSG="<span class=' + color + '--text>' + params.MSG + '</span>"'|string %} # apply color to main message body
{% else %}
RESPOND TYPE=error MSG="RESPOND COLOR '{color}' is invalid. Must be one of 'primary', 'secondary', 'accent', 'info', 'success', 'warning' or 'error'"
{% endif %}
{% elif params.MSG is defined %} # no color, but yes message
{% set msg = 'MSG="'+ params.MSG + '"'|string %}
{% endif %}
{% if params.PREFIX is defined and params.PREFIX_COLOR is defined %} # check if a separate color was supplied for the prefix - otherwise, fall back on the default one
{% set prefixColor = params.PREFIX_COLOR | default("DEFAULT") | lower %}
{% if prefixColor in colors %} # valid prefix color supplied
{% set prefix = 'PREFIX="<span class=' + prefixColor + '--text>' + params.PREFIX + '</span>"'|string %}
{% elif color in color %} # prefix color is invalid - fall back on overall value if possible
{% set prefix = 'PREFIX="<span class=' + color + '--text>' + params.PREFIX + '</span>"'|string %}
{% else %}
RESPOND TYPE=error MSG="RESPOND PREFIX_COLOR '{prefixColor}' is invalid. Must be one of 'primary', 'secondary', 'accent', 'info', 'success', 'warning' or 'error'"
{% endif %}
{% elif params.PREFIX is defined and color in colors %} # no provided prefix color - use the same color for the prefix as is used for the main message body if possible
{% set prefix = 'PREFIX="<span class=' + color + '--text>' + params.PREFIX + '</span>"'|string %}
{% elif params.PREFIX is defined %} # no valid colors provided either for the prefix or overall but we DO have a prefix- leave it default
{% set prefix = 'PREFIX="' + params.PREFIX + '"'|string %}
{% else %} # no prefix provided - don't use one!
{% set prefix = "" | string %}
{% endif %}
RESPOND {type} {prefix} {msg}
which can be tested with the following macro:
[gcode_macro TEST_EXTENDED_RESPOND]
gcode:
EXTENDED_RESPOND MSG="normal message with no prefix"
EXTENDED_RESPOND PREFIX="normal prefix" MSG="normal message"
EXTENDED_RESPOND PREFIX="normal prefix" MSG="echo message" TYPE="echo"
EXTENDED_RESPOND PREFIX="normal prefix" MSG="echo_no_space message" TYPE="echo_no_space"
EXTENDED_RESPOND PREFIX="normal prefix" MSG="command message" TYPE="command"
EXTENDED_RESPOND PREFIX="normal prefix" MSG="error message (messages of type error which have a prefix are printed normally)" TYPE="error"
EXTENDED_RESPOND MSG="error message (plain old message with no prefix supplied)" TYPE="error"
EXTENDED_RESPOND PREFIX="normal prefix (no separate prefix color)" MSG="info message" COLOR="info"
EXTENDED_RESPOND PREFIX="accented prefix" PREFIX_COLOR="accent" MSG="info message" COLOR="info"
EXTENDED_RESPOND PREFIX="warning prefix" PREFIX_COLOR="warning" MSG="error message" COLOR="error"
EXTENDED_RESPOND PREFIX="prefix with invalid color" PREFIX_COLOR="garbage" MSG="secondary message" COLOR="secondary"
which should yield the following results: