ShellManager: init from a static list of env vars

No need to go through a static list and then copy them one by
one in a vector.
This commit is contained in:
Maxime Coste 2018-02-18 14:52:29 +11:00
parent d9cddf41f2
commit 5924694bda
3 changed files with 134 additions and 146 deletions

View File

@ -77,13 +77,7 @@ String config_directory()
return format("{}/kak", config_home); return format("{}/kak", config_home);
} }
void register_env_vars() static const EnvVarDesc builtin_env_vars[] = { {
{
static const struct {
const char* name;
bool prefix;
String (*func)(StringView, const Context&);
} env_vars[] = { {
"bufname", false, "bufname", false,
[](StringView name, const Context& context) -> String [](StringView name, const Context& context) -> String
{ return context.buffer().display_name(); } { return context.buffer().display_name(); }
@ -200,12 +194,7 @@ void register_env_vars()
[](StringView name, const Context& context) -> String [](StringView name, const Context& context) -> String
{ return to_string(context.window().dimensions().line); } { return to_string(context.window().dimensions().line); }
} }
}; };
ShellManager& shell_manager = ShellManager::instance();
for (auto& env_var : env_vars)
shell_manager.register_env_var(env_var.name, env_var.prefix, env_var.func);
}
void register_registers() void register_registers()
{ {
@ -584,7 +573,7 @@ int run_server(StringView session, StringView server_init,
StringRegistry string_registry; StringRegistry string_registry;
GlobalScope global_scope; GlobalScope global_scope;
ShellManager shell_manager; ShellManager shell_manager{builtin_env_vars};
CommandManager command_manager; CommandManager command_manager;
RegisterManager register_manager; RegisterManager register_manager;
HighlighterRegistry highlighter_registry; HighlighterRegistry highlighter_registry;
@ -594,7 +583,6 @@ int run_server(StringView session, StringView server_init,
BufferManager buffer_manager; BufferManager buffer_manager;
register_options(); register_options();
register_env_vars();
register_registers(); register_registers();
register_commands(); register_commands();
register_highlighters(); register_highlighters();
@ -731,14 +719,13 @@ int run_filter(StringView keystr, StringView commands, ConstArrayView<StringView
StringRegistry string_registry; StringRegistry string_registry;
GlobalScope global_scope; GlobalScope global_scope;
EventManager event_manager; EventManager event_manager;
ShellManager shell_manager; ShellManager shell_manager{builtin_env_vars};
CommandManager command_manager; CommandManager command_manager;
RegisterManager register_manager; RegisterManager register_manager;
ClientManager client_manager; ClientManager client_manager;
BufferManager buffer_manager; BufferManager buffer_manager;
register_options(); register_options();
register_env_vars();
register_registers(); register_registers();
register_commands(); register_commands();

View File

@ -23,7 +23,8 @@ extern char **environ;
namespace Kakoune namespace Kakoune
{ {
ShellManager::ShellManager() ShellManager::ShellManager(ConstArrayView<EnvVarDesc> builtin_env_vars)
: m_env_vars{builtin_env_vars}
{ {
// Get a guaranteed to be POSIX shell binary // Get a guaranteed to be POSIX shell binary
{ {
@ -293,12 +294,6 @@ std::pair<String, int> ShellManager::eval(
return { std::move(stdout_contents), WIFEXITED(status) ? WEXITSTATUS(status) : -1 }; return { std::move(stdout_contents), WIFEXITED(status) ? WEXITSTATUS(status) : -1 };
} }
void ShellManager::register_env_var(StringView str, bool prefix,
EnvVarRetriever retriever)
{
m_env_vars.push_back({ str.str(), prefix, std::move(retriever) });
}
String ShellManager::get_val(StringView name, const Context& context) const String ShellManager::get_val(StringView name, const Context& context) const
{ {
auto env_var = find_if(m_env_vars, [name](const EnvVarDesc& desc) { auto env_var = find_if(m_env_vars, [name](const EnvVarDesc& desc) {

View File

@ -12,18 +12,26 @@ namespace Kakoune
class Context; class Context;
using EnvVarRetriever = std::function<String (StringView name, const Context&)>;
struct ShellContext struct ShellContext
{ {
ConstArrayView<String> params; ConstArrayView<String> params;
EnvVarMap env_vars; EnvVarMap env_vars;
}; };
struct EnvVarDesc
{
using Retriever = String (*)(StringView name, const Context&);
StringView str;
bool prefix;
Retriever func;
};
class ShellManager : public Singleton<ShellManager> class ShellManager : public Singleton<ShellManager>
{ {
public: public:
ShellManager(); ShellManager(ConstArrayView<EnvVarDesc> builtin_env_vars);
enum class Flags enum class Flags
{ {
@ -37,7 +45,6 @@ public:
Flags flags = Flags::WaitForStdout, Flags flags = Flags::WaitForStdout,
const ShellContext& shell_context = {}); const ShellContext& shell_context = {});
void register_env_var(StringView str, bool prefix, EnvVarRetriever retriever);
String get_val(StringView name, const Context& context) const; String get_val(StringView name, const Context& context) const;
CandidateList complete_env_var(StringView prefix, ByteCount cursor_pos) const; CandidateList complete_env_var(StringView prefix, ByteCount cursor_pos) const;
@ -45,8 +52,7 @@ public:
private: private:
String m_shell; String m_shell;
struct EnvVarDesc { String str; bool prefix; EnvVarRetriever func; }; ConstArrayView<EnvVarDesc> m_env_vars;
Vector<EnvVarDesc, MemoryDomain::EnvVars> m_env_vars;
}; };
} }