Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Echo quoting json #5217

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

web-sst
Copy link

@web-sst web-sst commented Aug 14, 2024

This PR implements a new quoting option, echo -quoting json as suggested in #3783. This option formats each argument as a json string and prints them joined by newlines. A simple use case is:

echo -quoting json -to-shell-script "jq -n '[inputs]' | ..."

This way large and list-valued outputs are easy to handle in external scripts without having to pass them in environment.

The implementation is not very complex, but does involve some minor refactoring, splitting quoting.hh and option_strings.hh out from string_utils.hh. While this could have been done with a split of quoting.hh alone, it seemed to me less awkward to split them both. I followed the apparent conventions as I understand them, but would appreciate a review from a real C++ programmer, especially of the #include updates .

Also string.hh needed #include <algorithm> to compile on FreeBSD 14.1, clang 18.1.5.

Format each argument as a json string and join on newline

Split out quoting.hh from string_utils.hh. Also split
options_strings.hh from string_utils.hh. This seemed
less awkward than keeping the material in quoting.hh.

Added `#include <algorithm>` to strings.hh. Without this compile
failed on FreeBSD 14.1, clang version 18.1.5.
I dedicate any and all copyright interest in this software to the
public domain.  I make this dedication for the benefit of the public at
large and to the detriment of my heirs and successors.  I intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
@web-sst web-sst mentioned this pull request Aug 14, 2024
@web-sst
Copy link
Author

web-sst commented Aug 14, 2024

@mawww wrote:

I am not sure how much value json quoting has in the first place as we still only emit a list of strings. I would like to see a use case where using the existing mechanisms is much more cumbersome before adding this.

You can, for example, trivially get the list of strings in "$@" with echo -quoting shell -to-shell-script 'set -- $(cat); ...'

Following this suggestion I can do this:

echo -quoting shell -to-shell-script 'eval set -- "$(cat)"; jq -n "\$ARGS.positional" --args "$@" ...'

rather than

echo -quoting json -to-shell-script 'jq "[inputs]" ...'

I prefer the second version because it avoids extra parsing in shell, avoids having to put the potentially large content into command-line arguments, and is much clearer. The first approach was not obvious to me, and needed the eval and double quotes to function as expected, whereas the second approach does seem obvious. There are perhaps two notions of simplicity here: minimizing the number of ways to access data versus allowing the most direct access to data. I look at the addition of json quoting as the mere composition of features already in kakoune in service of direct access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant