Skip to content

Instantly share code, notes, and snippets.

@mpalpha
Last active June 2, 2024 15:32
Show Gist options
  • Save mpalpha/16de86b987cb634cf43dc5d7a8611ebb to your computer and use it in GitHub Desktop.
Save mpalpha/16de86b987cb634cf43dc5d7a8611ebb to your computer and use it in GitHub Desktop.
klipper macro to search for variables.
# This macro will allow you to type search_vars s={some word} from the terminal and
# it will respond with all of the matching items in the printer Object.
# Say I wanted to know what the name and path of for the value of the currently loaded bed
# mesh. I could do type search_vars s=profile in my terminal and it will respond with
# any items containing the word ‘profile’.
#
# $ SEARCH_VARS s="profile"
# // printer.bed_mesh.profile_name : default
#
[gcode_macro SEARCH_VARS]
# Search like 'SEARCH_VARS s="profile"'
gcode:
{% if not params.S %}
{action_respond_info("hmm.. try 'SEARCH_VARS s=\"profile\"'")}
{% else %}
{% set resultcount = namespace(total=0) %}
{% set search = params.S|lower %}
{% set ns = namespace() %}
{% for item in printer %}
{% if ' ' in item %}
{% set ns.path = ['printer', "['%s']" % (item), ''] %}
{% else %}
{% set ns.path = ['printer.', item, ''] %}
{% endif %}
{% if search in ns.path|lower %}
{ action_respond_info(ns.path|join) }
{% set resultcount.total = 1 + resultcount.total %}
{% endif %}
{% if printer[item].items() %}
{% for childkey, child in printer[item].items() recursive %}
{% set ns.path = ns.path[:loop.depth|int + 1] %}
{% if ' ' in childkey %}
{% set null = ns.path.append("['%s']" % (childkey)) %}
{% else %}
{% set null = ns.path.append(".%s" % (childkey)) %}
{% endif %}
{% if child is mapping %}
{ loop(child.items()) }
{% else %}
{% if search in ns.path|lower %}
{ action_respond_info("%s : %s" % (ns.path|join, child)) }
{% set resultcount.total = 1 + resultcount.total %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% if resultcount.total >= 1 %}
{action_respond_info("found \"" + params.S + "\" " + resultcount.total|string +" times.")}
{% else %}
{action_respond_info("\"" + params.S + "\" not found.")}
{% endif %}
{% set resultcount.total = 0|int %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment