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
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
3 changes: 3 additions & 0 deletions doc/pages/commands.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ of the file onto the filesystem
also wrap each arguments 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
1 change: 1 addition & 0 deletions src/command_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "optional.hh"
#include "option_types.hh"
#include "profile.hh"
#include "quoting.hh"
#include "ranges.hh"
#include "regex.hh"
#include "register_manager.hh"
Expand Down
11 changes: 8 additions & 3 deletions src/commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#include "insert_completer.hh"
#include "normal.hh"
#include "option_manager.hh"
#include "option_strings.hh"
#include "option_types.hh"
#include "parameters_parser.hh"
#include "profile.hh"
#include "quoting.hh"
#include "ranges.hh"
#include "ranked_match.hh"
#include "regex.hh"
Expand Down Expand Up @@ -1524,7 +1526,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 @@ -1538,8 +1540,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 quoting_type = option_from_string(Meta::Type<Quoting>{}, *quoting);
message = join(parser | transform(quoter(quoting_type)),
(quoting_type == Quoting::Json ? '\n' : ' '), false);
}
else
message = join(parser, ' ', false);

Expand Down
2 changes: 2 additions & 0 deletions src/normal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "file.hh"
#include "flags.hh"
#include "option_manager.hh"
#include "option_strings.hh"
#include "option_types.hh"
#include "quoting.hh"
#include "ranges.hh"
#include "regex.hh"
#include "register_manager.hh"
Expand Down
2 changes: 2 additions & 0 deletions src/option_manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "exception.hh"
#include "hash_map.hh"
#include "option.hh"
#include "option_strings.hh"
#include "quoting.hh"
#include "ranges.hh"
#include "utils.hh"
#include "vector.hh"
Expand Down
19 changes: 19 additions & 0 deletions src/option_strings.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef option_strings_hh_INCLUDED
#define option_strings_hh_INCLUDED

#include "quoting.hh"
#include "string.hh"
#include "string_utils.hh"
#include "vector.hh"

namespace Kakoune
{

inline String option_to_string(StringView opt, Quoting quoting) { return quoter(quoting)(opt); }
inline Vector<String> option_to_strings(StringView opt) { return {opt.str()}; }
inline String option_from_string(Meta::Type<String>, StringView str) { return str.str(); }
inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); }

}

#endif // option_strings_hh_INCLUDED
2 changes: 2 additions & 0 deletions src/option_types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "flags.hh"
#include "hash_map.hh"
#include "option.hh"
#include "option_strings.hh"
#include "quoting.hh"
#include "string.hh"
#include "string_utils.hh"
#include "format.hh"
Expand Down
60 changes: 60 additions & 0 deletions src/quoting.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef quoting_hh_INCLUDED
#define quoting_hh_INCLUDED

#include "format.hh"
#include "json.hh"
#include "meta.hh"
#include "string.hh"
#include "string_utils.hh"

namespace Kakoune
{

inline String quote(StringView s)
{
return format("'{}'", double_up(s, "'"));
}

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,
Json
};

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

inline auto quoter(Quoting quoting)
{
switch (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(); };
}
}

}
#endif // quoting_hh_INCLUDED
3 changes: 3 additions & 0 deletions src/regex.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "regex.hh"

#include "option_strings.hh"
#include "quoting.hh"
#include "ranges.hh"
#include "string_utils.hh"

Expand Down
1 change: 1 addition & 0 deletions src/shell_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "flags.hh"
#include "option.hh"
#include "option_types.hh"
#include "quoting.hh"
#include "regex.hh"

#include <array>
Expand Down
1 change: 1 addition & 0 deletions src/string.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <climits>
#include <cstddef>
#include <algorithm>
#include "memory.hh"
#include "hash.hh"
#include "units.hh"
Expand Down
43 changes: 0 additions & 43 deletions src/string_utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,49 +110,6 @@ Optional<int> str_to_int_ifp(StringView str);

String double_up(StringView s, StringView characters);

inline String quote(StringView s)
{
return format("'{}'", double_up(s, "'"));
}

inline String shell_quote(StringView s)
{
return format("'{}'", replace(s, "'", R"('\'')"));
}

enum class Quoting
{
Raw,
Kakoune,
Shell
};

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

inline auto quoter(Quoting quoting)
{
switch (quoting)
{
case Quoting::Kakoune: return &quote;
case Quoting::Shell: return &shell_quote;
case Quoting::Raw:
default:
return +[](StringView s) { return s.str(); };
}
}

inline String option_to_string(StringView opt, Quoting quoting) { return quoter(quoting)(opt); }
inline Vector<String> option_to_strings(StringView opt) { return {opt.str()}; }
inline String option_from_string(Meta::Type<String>, StringView str) { return str.str(); }
inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); }

}

#endif // string_utils_hh_INCLUDED