dotfiles/xonsh/includes/quote-comments.xsh
2024-11-07 16:25:41 -07:00

25 lines
678 B
Text

""" Input transformer that automatically quotes #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"