Skip to content

Instantly share code, notes, and snippets.

@ktemkin
Created May 3, 2024 14:52
Show Gist options
  • Save ktemkin/9d35bcab82122385b5455f9d2138cdd0 to your computer and use it in GitHub Desktop.
Save ktemkin/9d35bcab82122385b5455f9d2138cdd0 to your computer and use it in GitHub Desktop.
hack to let you use unquoted flake attr refs in xonsh
""" Input transformer that automatically quotes mid-word #s for e.g. nix. """
from xonsh.events import events
@events.on_transform_command
def _autoquote_for_nix(cmd):
new_command = []
# Process each word individually.
words = cmd.strip().split(" ")
for word in words:
# If this is unquoted, and it has a comment in it without spaces,
# consider it something that we should auto-quote.
has_comment = '#' in word
is_comment = word.startswith("#")
is_quoted = ('"' in word) or ("'" in word)
if has_comment and not is_comment and not is_quoted:
word = '"' + word + '"'
new_command.append(word)
# Reconstruct our command.
return " ".join(new_command) + "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment