From a9e778fcc7f06a27f2d06da225f08110c9e2dca9 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 23 Jun 2019 09:58:31 +1000 Subject: [PATCH] Add support for `echo -quoting (raw|kakoune|shell)` switch --- doc/pages/commands.asciidoc | 14 ++++++++++++++ src/commands.cc | 9 ++++++++- src/string_utils.hh | 10 ++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/doc/pages/commands.asciidoc b/doc/pages/commands.asciidoc index ebaafaea..2ef0f3a9 100644 --- a/doc/pages/commands.asciidoc +++ b/doc/pages/commands.asciidoc @@ -210,6 +210,20 @@ of the file onto the filesystem write the given text to the given file on the host filesystem. + *-quoting* ::: + define how each arguments are quoted in echo output: + + - *raw* (default):::: + just join each argument with a space + + - *kakoune*:::: + also wrap each argument in single quotes, doubling-up + embedded quotes. + + - *shell*:::: + also wrap each arguments in single quotes and escape + embedded quotes in a shell compatible way. + *set-face* :: *alias* face + define a face in *scope* diff --git a/src/commands.cc b/src/commands.cc index c03358ce..75ccecc2 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -1245,6 +1245,7 @@ const CommandDesc echo_cmd = { "echo ...: display given parameters in the status line", ParameterDesc{ { { "markup", { false, "parse markup" } }, + { "quoting", { true, "quote each argument separately using the given style (raw|kakoune|shell)" } }, { "to-file", { true, "echo contents to given filename" } }, { "debug", { false, "write to debug buffer instead of status line" } } }, ParameterDesc::Flags::SwitchesOnlyAtStart @@ -1254,7 +1255,13 @@ const CommandDesc echo_cmd = { CommandCompleter{}, [](const ParametersParser& parser, Context& context, const ShellContext&) { - String message = join(parser, ' ', false); + String message; + if (auto quoting = parser.get_switch("quoting")) + message = join(parser | transform(quoter(option_from_string(Meta::Type{}, *quoting))), + ' ', false); + else + message = join(parser, ' ', false); + if (auto filename = parser.get_switch("to-file")) return write_to_file(*filename, message); diff --git a/src/string_utils.hh b/src/string_utils.hh index 224dffc2..928070d3 100644 --- a/src/string_utils.hh +++ b/src/string_utils.hh @@ -2,6 +2,7 @@ #define string_utils_hh_INCLUDED #include "string.hh" +#include "enum.hh" #include "vector.hh" #include "optional.hh" @@ -143,6 +144,15 @@ enum class Quoting Shell }; +constexpr auto enum_desc(Meta::Type) +{ + return make_array, 3>({ + { Quoting::Raw, "raw" }, + { Quoting::Kakoune, "kakoune" }, + { Quoting::Shell, "shell" } + }); +} + inline auto quoter(Quoting quoting) { switch (quoting)