Extend RESPOND command with some color options

This macro extend the original Klipper RESPOND command by adding a COLOR parameter.

The COLOR parameter adds a css class on the MSG content. Those css classes are already available on Mainsail.

This has been designed to work on Mainsail but it should work on Fluidd as well.

[gcode_macro RESPOND]
rename_existing: BASE_RESPOND
gcode:
  {% set colors = ('primary', 'secondary', 'accent', 'info', 'success', 'error', 'warning') %}

  {% if params.PREFIX is defined %}
    {% set prefix = 'PREFIX=' + params.PREFIX|string %}
  {% endif %}

  {% if params.TYPE is defined %}
      {% if params.TYPE in ('echo', 'echo_no_space', 'command', 'error') and params.TYPE != '' %}    
          {% set type = 'TYPE=' + params.TYPE|string %}
      {% else %}
          BASE_RESPOND TYPE=error MSG="RESPOND TYPE '{params.TYPE}' is invalid. Must be one of 'echo', 'command' or 'error'"
      {% endif %}
  {% endif %}
        
  {% if params.MSG is defined %}   
    {% set msg = 'MSG="'+ params.MSG + '"'|string %}
  {% endif %}

  {% if params.COLOR is defined %}
      {% set color = params.COLOR|lower %}

      {% if color in colors %}
          {% set msg = 'MSG="<span class=' + color + '--text>' + params.MSG + '</span>"'|string %}
  
      {% else %}
          BASE_RESPOND TYPE=error MSG="RESPOND COLOR '{color}' is invalid. Must be one of 'primary', 'secondary', 'accent', 'info', 'success', 'warning' or 'error'"
      {% endif %}
       
  {% endif %}
  BASE_RESPOND {prefix} {type} {msg}

Here is a little preview of the different combinations of messages that you can obtain with this macro. Those are the default colors of Mainsail and they can be changed easily with few lines of css too.

Enjoy :smiley:

10 Likes

Hi! This looks awesome. Can you give an example of how to get this to work? I’ve tried “RESPOND info TYPE= … MSG=…", “RESPOND Prefix=info TYPE=… MSG=…” “RESPOND ‘info’ Type=… MSG=…” and just can’t seem to find the correct format.

Thanks in advance!

RESPOND MSG="bla" COLOR="primary"

https://www.klipper3d.org/G-Codes.html#respond_1

1 Like

Thank you so much!

1 Like

Hi sorry to revive this necro thread, but this macro no longer works on modern mainsail.

There is no instance of RESPOND_BASE or BASE_RESPOND. How would you get around this, I would love to use this coloring scheme for my macros.

@Sineos

@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:

1 Like