mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-29 05:12:41 +00:00
Make modifying settings from C++ a bit easier
This commit is contained in:
parent
454f4a12a8
commit
568c372920
@ -4305,6 +4305,9 @@ int main(int argc, char **argv)
|
||||
l.call(0, 0);
|
||||
conky::check_config_settings(l);
|
||||
std::cout << "config.asdf = " << conky::asdf.get(l) << std::endl;
|
||||
l.pushstring("X");
|
||||
conky::asdf.lua_set(l);
|
||||
std::cout << "config.asdf = " << conky::asdf.get(l) << std::endl;
|
||||
l.loadstring(
|
||||
"print('config.asdf = ', conky.config.asdf);\n"
|
||||
"conky.config.asdf = 42;\n"
|
||||
|
@ -49,6 +49,21 @@ namespace conky {
|
||||
if(not inserted)
|
||||
throw std::logic_error("Setting with name '" + name + "' already registered");
|
||||
}
|
||||
|
||||
void config_setting_base::lua_set(lua::state &l)
|
||||
{
|
||||
lua::stack_sentry s(l, 1);
|
||||
l.checkstack(2);
|
||||
|
||||
l.getglobal("conky"); ++s;
|
||||
l.rawgetfield(-1, "config"); ++s;
|
||||
--s; l.replace(-2);
|
||||
l.insert(-2);
|
||||
l.pushstring(name.c_str()); ++s;
|
||||
l.insert(-2);
|
||||
s-=2; l.settable(-3);
|
||||
--s; l.pop();
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
@ -107,6 +107,13 @@ namespace conky {
|
||||
|
||||
config_setting_base(const std::string &name_, const lua_setter_t &lua_setter_);
|
||||
virtual ~config_setting_base() {}
|
||||
|
||||
/*
|
||||
* Set the setting manually.
|
||||
* stack on entry: | ... new_value |
|
||||
* stack on exit: | ... |
|
||||
*/
|
||||
void lua_set(lua::state &l);
|
||||
};
|
||||
|
||||
typedef std::unordered_map<std::string, config_setting_base *> config_settings_t;
|
||||
@ -120,7 +127,7 @@ namespace conky {
|
||||
* lua stack. It should pop it and return the C++ value. In case the value is nil, it should
|
||||
* return a predefined default value. Translation into basic types is provided with the
|
||||
* default simple_getter::do_it functions.
|
||||
* The lua_setter function is called when the user tries to set the value it the lua script.
|
||||
* The lua_setter function is called when someone tries to set the value.
|
||||
* It recieves the new and the old value on the stack (old one is on top). It should return
|
||||
* the new value for the setting. It doesn't have to be the value the user set, if e.g. the
|
||||
* value doesn't make sense. The second parameter is true if the assignment occurs during the
|
||||
|
Loading…
Reference in New Issue
Block a user