Regex are overkill for shell manager env vars, we just need exact match or prefix match
This commit is contained in:
parent
ea7f76f7f2
commit
e69db0f671
41
src/main.cc
41
src/main.cc
|
@ -55,92 +55,93 @@ void register_env_vars()
|
||||||
{
|
{
|
||||||
static const struct {
|
static const struct {
|
||||||
const char* name;
|
const char* name;
|
||||||
|
bool prefix;
|
||||||
String (*func)(StringView, const Context&);
|
String (*func)(StringView, const Context&);
|
||||||
} env_vars[] = { {
|
} env_vars[] = { {
|
||||||
"bufname",
|
"bufname", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return context.buffer().display_name(); }
|
{ return context.buffer().display_name(); }
|
||||||
}, {
|
}, {
|
||||||
"buffile",
|
"buffile", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return context.buffer().name(); }
|
{ return context.buffer().name(); }
|
||||||
}, {
|
}, {
|
||||||
"buflist",
|
"buflist", false,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return join(transformed(BufferManager::instance(),
|
{ return join(transformed(BufferManager::instance(),
|
||||||
[](const SafePtr<Buffer>& b)
|
[](const SafePtr<Buffer>& b)
|
||||||
{ return b->display_name(); }), ':'); }
|
{ return b->display_name(); }), ':'); }
|
||||||
}, {
|
}, {
|
||||||
"timestamp",
|
"timestamp", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return to_string(context.buffer().timestamp()); }
|
{ return to_string(context.buffer().timestamp()); }
|
||||||
}, {
|
}, {
|
||||||
"selection",
|
"selection", false,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ const Selection& sel = context.selections().main();
|
{ const Selection& sel = context.selections().main();
|
||||||
return content(context.buffer(), sel); }
|
return content(context.buffer(), sel); }
|
||||||
}, {
|
}, {
|
||||||
"selections",
|
"selections", false,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return join(context.selections_content(), ':'); }
|
{ return join(context.selections_content(), ':'); }
|
||||||
}, {
|
}, {
|
||||||
"runtime",
|
"runtime", false,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return runtime_directory(); }
|
{ return runtime_directory(); }
|
||||||
}, {
|
}, {
|
||||||
"opt_.+",
|
"opt_", true,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
||||||
}, {
|
}, {
|
||||||
"reg_.+",
|
"reg_", true,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return context.main_sel_register_value(name.substr(4_byte)).str(); }
|
{ return context.main_sel_register_value(name.substr(4_byte)).str(); }
|
||||||
}, {
|
}, {
|
||||||
"client_env_.+",
|
"client_env_", true,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return context.client().get_env_var(name.substr(11_byte)).str(); }
|
{ return context.client().get_env_var(name.substr(11_byte)).str(); }
|
||||||
}, {
|
}, {
|
||||||
"session",
|
"session", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return Server::instance().session(); }
|
{ return Server::instance().session(); }
|
||||||
}, {
|
}, {
|
||||||
"client",
|
"client", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return context.name(); }
|
{ return context.name(); }
|
||||||
}, {
|
}, {
|
||||||
"cursor_line",
|
"cursor_line", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return to_string(context.selections().main().cursor().line + 1); }
|
{ return to_string(context.selections().main().cursor().line + 1); }
|
||||||
}, {
|
}, {
|
||||||
"cursor_column",
|
"cursor_column", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return to_string(context.selections().main().cursor().column + 1); }
|
{ return to_string(context.selections().main().cursor().column + 1); }
|
||||||
}, {
|
}, {
|
||||||
"cursor_char_column",
|
"cursor_char_column", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ auto coord = context.selections().main().cursor();
|
{ auto coord = context.selections().main().cursor();
|
||||||
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
|
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
|
||||||
}, {
|
}, {
|
||||||
"selection_desc",
|
"selection_desc", false,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return selection_to_string(context.selections().main()); }
|
{ return selection_to_string(context.selections().main()); }
|
||||||
}, {
|
}, {
|
||||||
"selections_desc",
|
"selections_desc", false,
|
||||||
[](StringView name, const Context& context)
|
[](StringView name, const Context& context)
|
||||||
{ return selection_list_to_string(context.selections()); }
|
{ return selection_list_to_string(context.selections()); }
|
||||||
}, {
|
}, {
|
||||||
"window_width",
|
"window_width", false,
|
||||||
[](StringView name, const Context& context) -> String
|
[](StringView name, const Context& context) -> String
|
||||||
{ return to_string(context.window().dimensions().column); }
|
{ return to_string(context.window().dimensions().column); }
|
||||||
}, {
|
}, {
|
||||||
"window_height",
|
"window_height", false,
|
||||||
[](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();
|
ShellManager& shell_manager = ShellManager::instance();
|
||||||
for (auto& env_var : env_vars)
|
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()
|
void register_registers()
|
||||||
|
|
|
@ -135,22 +135,24 @@ std::pair<String, int> ShellManager::eval(
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShellManager::register_env_var(StringView regex,
|
void ShellManager::register_env_var(StringView str, bool prefix,
|
||||||
EnvVarRetriever retriever)
|
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
|
String ShellManager::get_val(StringView name, const Context& context) const
|
||||||
{
|
{
|
||||||
auto env_var = std::find_if(
|
auto env_var = std::find_if(
|
||||||
m_env_vars.begin(), m_env_vars.end(),
|
m_env_vars.begin(), m_env_vars.end(),
|
||||||
[name](const std::pair<Regex, EnvVarRetriever>& pair)
|
[name](const EnvVarDesc& desc) {
|
||||||
{ return regex_match(name.begin(), name.end(), pair.first); });
|
return desc.prefix ? prefix_match(name, desc.str) : name == desc.str;
|
||||||
|
});
|
||||||
|
|
||||||
if (env_var == m_env_vars.end())
|
if (env_var == m_env_vars.end())
|
||||||
throw runtime_error("no such env var: " + name);
|
throw runtime_error("no such env var: " + name);
|
||||||
return env_var->second(name, context);
|
|
||||||
|
return env_var->func(name, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,11 +33,12 @@ public:
|
||||||
ConstArrayView<String> params = {},
|
ConstArrayView<String> params = {},
|
||||||
const EnvVarMap& env_vars = EnvVarMap{});
|
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;
|
String get_val(StringView name, const Context& context) const;
|
||||||
|
|
||||||
private:
|
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 {};
|
template<> struct WithBitOps<ShellManager::Flags> : std::true_type {};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user