Add support for echo -quoting (raw|kakoune|shell) switch

This commit is contained in:
Maxime Coste 2019-06-23 09:58:31 +10:00
parent 95da8cbc8f
commit a9e778fcc7
3 changed files with 32 additions and 1 deletions

View File

@ -210,6 +210,20 @@ of the file onto the filesystem
write the given text to the given file on the host write the given text to the given file on the host
filesystem. filesystem.
*-quoting* <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* <scope> <name> <facespec>:: *set-face* <scope> <name> <facespec>::
*alias* face + *alias* face +
define a face in *scope* define a face in *scope*

View File

@ -1245,6 +1245,7 @@ const CommandDesc echo_cmd = {
"echo <params>...: display given parameters in the status line", "echo <params>...: display given parameters in the status line",
ParameterDesc{ ParameterDesc{
{ { "markup", { false, "parse markup" } }, { { "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" } }, { "to-file", { true, "echo contents to given filename" } },
{ "debug", { false, "write to debug buffer instead of status line" } } }, { "debug", { false, "write to debug buffer instead of status line" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart ParameterDesc::Flags::SwitchesOnlyAtStart
@ -1254,7 +1255,13 @@ const CommandDesc echo_cmd = {
CommandCompleter{}, CommandCompleter{},
[](const ParametersParser& parser, Context& context, const ShellContext&) [](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>{}, *quoting))),
' ', false);
else
message = join(parser, ' ', false);
if (auto filename = parser.get_switch("to-file")) if (auto filename = parser.get_switch("to-file"))
return write_to_file(*filename, message); return write_to_file(*filename, message);

View File

@ -2,6 +2,7 @@
#define string_utils_hh_INCLUDED #define string_utils_hh_INCLUDED
#include "string.hh" #include "string.hh"
#include "enum.hh"
#include "vector.hh" #include "vector.hh"
#include "optional.hh" #include "optional.hh"
@ -143,6 +144,15 @@ enum class Quoting
Shell Shell
}; };
constexpr auto enum_desc(Meta::Type<Quoting>)
{
return make_array<EnumDesc<Quoting>, 3>({
{ Quoting::Raw, "raw" },
{ Quoting::Kakoune, "kakoune" },
{ Quoting::Shell, "shell" }
});
}
inline auto quoter(Quoting quoting) inline auto quoter(Quoting quoting)
{ {
switch (quoting) switch (quoting)