Use StringView in shell manager
This commit is contained in:
parent
63a0b9d22a
commit
7cdb94d5c7
36
src/main.cc
36
src/main.cc
|
@ -59,27 +59,27 @@ void register_env_vars()
|
|||
{
|
||||
static const struct {
|
||||
const char* name;
|
||||
String (*func)(const String&, const Context&);
|
||||
String (*func)(StringView, const Context&);
|
||||
} env_vars[] = { {
|
||||
"bufname",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return context.buffer().display_name(); }
|
||||
}, {
|
||||
"buffile",
|
||||
[](const String& name, const Context& context) -> String
|
||||
[](StringView name, const Context& context) -> String
|
||||
{ return context.buffer().name(); }
|
||||
}, {
|
||||
"timestamp",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return to_string(context.buffer().timestamp()); }
|
||||
}, {
|
||||
"selection",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ const Selection& sel = context.selections().main();
|
||||
return content(context.buffer(), sel); }
|
||||
}, {
|
||||
"selections",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ auto sels = context.selections_content();
|
||||
String res;
|
||||
for (size_t i = 0; i < sels.size(); ++i)
|
||||
|
@ -91,55 +91,55 @@ void register_env_vars()
|
|||
return res; }
|
||||
}, {
|
||||
"runtime",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return runtime_directory(); }
|
||||
}, {
|
||||
"opt_.+",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
||||
}, {
|
||||
"reg_.+",
|
||||
[](const String& name, const Context& context) -> String
|
||||
[](StringView name, const Context& context) -> String
|
||||
{ return RegisterManager::instance()[name[4]].values(context)[0]; }
|
||||
}, {
|
||||
"client_env_.+",
|
||||
[](const String& name, const Context& context) -> String
|
||||
[](StringView name, const Context& context) -> String
|
||||
{ return context.client().get_env_var(name.substr(11_byte)); }
|
||||
}, {
|
||||
"session",
|
||||
[](const String& name, const Context& context) -> String
|
||||
[](StringView name, const Context& context) -> String
|
||||
{ return Server::instance().session(); }
|
||||
}, {
|
||||
"client",
|
||||
[](const String& name, const Context& context) -> String
|
||||
[](StringView name, const Context& context) -> String
|
||||
{ return context.name(); }
|
||||
}, {
|
||||
"cursor_line",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return to_string(context.selections().main().cursor().line + 1); }
|
||||
}, {
|
||||
"cursor_column",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return to_string(context.selections().main().cursor().column + 1); }
|
||||
}, {
|
||||
"cursor_char_column",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ auto coord = context.selections().main().cursor();
|
||||
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
|
||||
}, {
|
||||
"selection_desc",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ auto& sel = context.selections().main();
|
||||
auto beg = sel.min();
|
||||
return to_string(beg.line + 1) + ':' + to_string(beg.column + 1) + '+' +
|
||||
to_string((int)context.buffer().distance(beg, sel.max())+1); }
|
||||
}, {
|
||||
"window_width",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return to_string(context.window().dimensions().column); }
|
||||
}, {
|
||||
"window_height",
|
||||
[](const String& name, const Context& context)
|
||||
[](StringView name, const Context& context)
|
||||
{ return to_string(context.window().dimensions().line); }
|
||||
} };
|
||||
|
||||
|
|
|
@ -16,15 +16,15 @@ ShellManager::ShellManager()
|
|||
{
|
||||
}
|
||||
|
||||
String ShellManager::eval(const String& cmdline, const Context& context,
|
||||
String ShellManager::eval(StringView cmdline, const Context& context,
|
||||
memoryview<String> params,
|
||||
const EnvVarMap& env_vars)
|
||||
{
|
||||
return pipe("", cmdline, context, params, env_vars);
|
||||
}
|
||||
|
||||
String ShellManager::pipe(const String& input,
|
||||
const String& cmdline, const Context& context,
|
||||
String ShellManager::pipe(StringView input,
|
||||
StringView cmdline, const Context& context,
|
||||
memoryview<String> params,
|
||||
const EnvVarMap& env_vars)
|
||||
{
|
||||
|
@ -43,8 +43,7 @@ String ShellManager::pipe(const String& input,
|
|||
close(read_pipe[1]);
|
||||
close(error_pipe[1]);
|
||||
|
||||
auto data = input.data();
|
||||
write(write_pipe[1], data.pointer(), data.size());
|
||||
write(write_pipe[1], input.data(), (int)input.length());
|
||||
close(write_pipe[1]);
|
||||
|
||||
char buffer[1024];
|
||||
|
@ -79,18 +78,18 @@ String ShellManager::pipe(const String& input,
|
|||
dup2(error_pipe[1], 2); close(error_pipe[1]);
|
||||
dup2(write_pipe[0], 0); close(write_pipe[0]);
|
||||
|
||||
boost::regex_iterator<String::const_iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
|
||||
boost::regex_iterator<String::const_iterator> end;
|
||||
boost::regex_iterator<StringView::iterator> it(cmdline.begin(), cmdline.end(), env_var_regex);
|
||||
boost::regex_iterator<StringView::iterator> end;
|
||||
|
||||
while (it != end)
|
||||
{
|
||||
auto& match = *it;
|
||||
|
||||
String name;
|
||||
StringView name;
|
||||
if (match[1].matched)
|
||||
name = String(match[1].first, match[1].second);
|
||||
name = StringView(match[1].first, match[1].second);
|
||||
else if (match[2].matched)
|
||||
name = String(match[2].first, match[2].second);
|
||||
name = StringView(match[2].first, match[2].second);
|
||||
else
|
||||
kak_assert(false);
|
||||
kak_assert(name.length() > 0);
|
||||
|
@ -111,7 +110,7 @@ String ShellManager::pipe(const String& input,
|
|||
try
|
||||
{
|
||||
String value = env_var->second(name, context);
|
||||
setenv(("kak_" + name).c_str(), value.c_str(), 1);
|
||||
setenv(("kak_"_str + name).c_str(), value.c_str(), 1);
|
||||
}
|
||||
catch (runtime_error&) {}
|
||||
}
|
||||
|
@ -119,22 +118,23 @@ String ShellManager::pipe(const String& input,
|
|||
|
||||
++it;
|
||||
}
|
||||
String shell = "/bin/sh";
|
||||
std::vector<const char*> execparams = { shell.c_str(), "-c", cmdline.c_str() };
|
||||
const char* shell = "/bin/sh";
|
||||
auto cmdlinezstr = cmdline.zstr();
|
||||
std::vector<const char*> execparams = { shell, "-c", cmdlinezstr };
|
||||
if (not params.empty())
|
||||
execparams.push_back(shell.c_str());
|
||||
execparams.push_back(shell);
|
||||
for (auto& param : params)
|
||||
execparams.push_back(param.c_str());
|
||||
execparams.push_back(nullptr);
|
||||
|
||||
execvp(shell.c_str(), (char* const*)execparams.data());
|
||||
execvp(shell, (char* const*)execparams.data());
|
||||
exit(-1);
|
||||
}
|
||||
catch (...) { exit(-1); }
|
||||
return output;
|
||||
}
|
||||
|
||||
void ShellManager::register_env_var(const String& regex,
|
||||
void ShellManager::register_env_var(StringView regex,
|
||||
EnvVarRetriever retriever)
|
||||
{
|
||||
m_env_vars.push_back({ Regex(regex.begin(), regex.end()), std::move(retriever) });
|
||||
|
|
|
@ -9,23 +9,23 @@ namespace Kakoune
|
|||
{
|
||||
|
||||
class Context;
|
||||
using EnvVarRetriever = std::function<String (const String& name, const Context&)>;
|
||||
using EnvVarRetriever = std::function<String (StringView name, const Context&)>;
|
||||
|
||||
class ShellManager : public Singleton<ShellManager>
|
||||
{
|
||||
public:
|
||||
ShellManager();
|
||||
|
||||
String eval(const String& cmdline, const Context& context,
|
||||
String eval(StringView cmdline, const Context& context,
|
||||
memoryview<String> params,
|
||||
const EnvVarMap& env_vars);
|
||||
|
||||
String pipe(const String& input,
|
||||
const String& cmdline, const Context& context,
|
||||
String pipe(StringView input,
|
||||
StringView cmdline, const Context& context,
|
||||
memoryview<String> params,
|
||||
const EnvVarMap& env_vars);
|
||||
|
||||
void register_env_var(const String& regex, EnvVarRetriever retriever);
|
||||
void register_env_var(StringView regex, EnvVarRetriever retriever);
|
||||
|
||||
private:
|
||||
std::vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
|
||||
|
|
Loading…
Reference in New Issue
Block a user