From fa02cdceab9c4059c74f7bbe6d58cd1a2fa787a2 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sat, 13 Feb 2010 17:00:20 +0100 Subject: [PATCH] An object-oriented api for accessing data sources from lua --- src/conky.cc | 2 ++ src/lua-config.cc | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/conky.cc b/src/conky.cc index cf2e7f9b..6203e64c 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -4323,6 +4323,8 @@ int main(int argc, char **argv) "print(conky.asnumber(conky.variables.asdf{}));\n" "print(conky.astext(conky.variables.asdf{}));\n" "print(conky.asnumber(conky.variables.zxcv{}));\n" + "print(conky.variables.asdf{}.text);\n" + "print(conky.variables.asdf{}.xxx);\n" ); l.call(0, 0); } diff --git a/src/lua-config.cc b/src/lua-config.cc index ae9f32e3..455e871e 100644 --- a/src/lua-config.cc +++ b/src/lua-config.cc @@ -64,6 +64,17 @@ namespace conky { l->setmetatable(-2); return 1; } + + const char data_source__index[] = + "local table, key = ...;\n" + "if key == 'num' then\n" + " return conky.asnumber(table);\n" + "elseif key == 'text' then\n" + " return conky.astext(table);\n" + "else\n" + " print(string.format([[Invalid data source operation: '%s']], key));\n" + " return 0/0;\n" + "end\n"; } void export_symbols(lua::state &l) @@ -77,6 +88,9 @@ namespace conky { l.pushdestructor>(); ++s; l.rawsetfield(-2, "__gc"); --s; + + l.loadstring(data_source__index); ++s; + l.rawsetfield(-2, "__index"); --s; } l.pop(); --s; l.newtable(); ++s; {