Skip to content
New issue

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

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

Already on GitHub? # to your account

Echo json #5215

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/pages/commands.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,12 @@ of the file onto the filesystem
embedded quotes.

- *shell*::::
also wrap each arguments in single quotes and escape
also wrap each argument in single quotes and escape
embedded quotes in a shell compatible way.

- *json*::::
convert each argument to a json string and join on newline.

*set-face* <scope> <name> <facespec>::
*alias* face +
define a face in *scope*
Expand Down
10 changes: 7 additions & 3 deletions src/commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "remote.hh"
#include "shell_manager.hh"
#include "string.hh"
#include "string_utils.hh"
#include "user_interface.hh"
#include "window.hh"

Expand Down Expand Up @@ -1523,7 +1524,7 @@ const CommandDesc echo_cmd = {
"echo <params>...: display given parameters in the status line",
ParameterDesc{
{ { "markup", { {}, "parse markup" } },
{ "quoting", { {arg_completer(Array{"raw", "kakoune", "shell"})}, "quote each argument separately using the given style (raw|kakoune|shell)" } },
{ "quoting", { {arg_completer(Array{"raw", "kakoune", "shell", "json"})}, "quote each argument separately using the given style (raw|kakoune|shell|json)" } },
{ "end-of-line", { {}, "add trailing end-of-line" } },
{ "to-file", { {filename_arg_completer<false>}, "echo contents to given filename" } },
{ "to-shell-script", { ArgCompleter{}, "pipe contents to given shell script" } },
Expand All @@ -1537,8 +1538,11 @@ const CommandDesc echo_cmd = {
{
String message;
if (auto quoting = parser.get_switch("quoting"))
message = join(parser | transform(quoter(option_from_string(Meta::Type<Quoting>{}, *quoting))),
' ', false);
{
auto quote_type = option_from_string(Meta::Type<Quoting>{}, *quoting);
message = join(parser | transform(quoter(quote_type)),
(quote_type == Quoting::Json ? '\n' : ' '), false);
}
else
message = join(parser, ' ', false);

Expand Down
13 changes: 11 additions & 2 deletions src/string_utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ranges.hh"
#include "optional.hh"
#include "utils.hh"
#include "json.hh"

namespace Kakoune
{
Expand Down Expand Up @@ -188,19 +189,26 @@ inline String shell_quote(StringView s)
return format("'{}'", replace(s, "'", R"('\'')"));
}

inline String json_quote(StringView s)
{
return format("{}", to_json(s));
}

enum class Quoting
{
Raw,
Kakoune,
Shell
Shell,
Json
};

constexpr auto enum_desc(Meta::Type<Quoting>)
{
return make_array<EnumDesc<Quoting>>({
{ Quoting::Raw, "raw" },
{ Quoting::Kakoune, "kakoune" },
{ Quoting::Shell, "shell" }
{ Quoting::Shell, "shell" },
{ Quoting::Json, "json" }
});
}

Expand All @@ -210,6 +218,7 @@ inline auto quoter(Quoting quoting)
{
case Quoting::Kakoune: return &quote;
case Quoting::Shell: return &shell_quote;
case Quoting::Json: return &json_quote;
case Quoting::Raw:
default:
return +[](StringView s) { return s.str(); };
Expand Down