Regex are overkill for shell manager env vars, we just need exact match or prefix match

This commit is contained in:
Maxime Coste 2015-09-03 13:21:35 +01:00
parent ea7f76f7f2
commit e69db0f671
3 changed files with 31 additions and 27 deletions

View File

@ -55,92 +55,93 @@ void register_env_vars()
{
static const struct {
const char* name;
bool prefix;
String (*func)(StringView, const Context&);
} env_vars[] = { {
"bufname",
"bufname", false,
[](StringView name, const Context& context) -> String
{ return context.buffer().display_name(); }
}, {
"buffile",
"buffile", false,
[](StringView name, const Context& context) -> String
{ return context.buffer().name(); }
}, {
"buflist",
"buflist", false,
[](StringView name, const Context& context)
{ return join(transformed(BufferManager::instance(),
[](const SafePtr<Buffer>& b)
{ return b->display_name(); }), ':'); }
}, {
"timestamp",
"timestamp", false,
[](StringView name, const Context& context) -> String
{ return to_string(context.buffer().timestamp()); }
}, {
"selection",
"selection", false,
[](StringView name, const Context& context)
{ const Selection& sel = context.selections().main();
return content(context.buffer(), sel); }
}, {
"selections",
"selections", false,
[](StringView name, const Context& context)
{ return join(context.selections_content(), ':'); }
}, {
"runtime",
"runtime", false,
[](StringView name, const Context& context)
{ return runtime_directory(); }
}, {
"opt_.+",
"opt_", true,
[](StringView name, const Context& context)
{ return context.options()[name.substr(4_byte)].get_as_string(); }
}, {
"reg_.+",
"reg_", true,
[](StringView name, const Context& context)
{ return context.main_sel_register_value(name.substr(4_byte)).str(); }
}, {
"client_env_.+",
"client_env_", true,
[](StringView name, const Context& context)
{ return context.client().get_env_var(name.substr(11_byte)).str(); }
}, {
"session",
"session", false,
[](StringView name, const Context& context) -> String
{ return Server::instance().session(); }
}, {
"client",
"client", false,
[](StringView name, const Context& context) -> String
{ return context.name(); }
}, {
"cursor_line",
"cursor_line", false,
[](StringView name, const Context& context) -> String
{ return to_string(context.selections().main().cursor().line + 1); }
}, {
"cursor_column",
"cursor_column", false,
[](StringView name, const Context& context) -> String
{ return to_string(context.selections().main().cursor().column + 1); }
}, {
"cursor_char_column",
"cursor_char_column", false,
[](StringView name, const Context& context) -> String
{ auto coord = context.selections().main().cursor();
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
}, {
"selection_desc",
"selection_desc", false,
[](StringView name, const Context& context)
{ return selection_to_string(context.selections().main()); }
}, {
"selections_desc",
"selections_desc", false,
[](StringView name, const Context& context)
{ return selection_list_to_string(context.selections()); }
}, {
"window_width",
"window_width", false,
[](StringView name, const Context& context) -> String
{ return to_string(context.window().dimensions().column); }
}, {
"window_height",
"window_height", false,
[](StringView name, const Context& context) -> String
{ 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.func);
shell_manager.register_env_var(env_var.name, env_var.prefix, env_var.func);
}
void register_registers()

View File

@ -135,22 +135,24 @@ std::pair<String, int> ShellManager::eval(
return {};
}
void ShellManager::register_env_var(StringView regex,
void ShellManager::register_env_var(StringView str, bool prefix,
EnvVarRetriever retriever)
{
m_env_vars.push_back({ Regex{regex}, std::move(retriever) });
m_env_vars.push_back({ str.str(), prefix, std::move(retriever) });
}
String ShellManager::get_val(StringView name, const Context& context) const
{
auto env_var = std::find_if(
m_env_vars.begin(), m_env_vars.end(),
[name](const std::pair<Regex, EnvVarRetriever>& pair)
{ return regex_match(name.begin(), name.end(), pair.first); });
[name](const EnvVarDesc& desc) {
return desc.prefix ? prefix_match(name, desc.str) : name == desc.str;
});
if (env_var == m_env_vars.end())
throw runtime_error("no such env var: " + name);
return env_var->second(name, context);
return env_var->func(name, context);
}
}

View File

@ -33,11 +33,12 @@ public:
ConstArrayView<String> params = {},
const EnvVarMap& env_vars = EnvVarMap{});
void register_env_var(StringView regex, EnvVarRetriever retriever);
void register_env_var(StringView str, bool prefix, EnvVarRetriever retriever);
String get_val(StringView name, const Context& context) const;
private:
Vector<std::pair<Regex, EnvVarRetriever>> m_env_vars;
struct EnvVarDesc { String str; bool prefix; EnvVarRetriever func; };
Vector<EnvVarDesc> m_env_vars;
};
template<> struct WithBitOps<ShellManager::Flags> : std::true_type {};