mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-23 19:39:06 +00:00
make convert.lua "bimodal"
it does The Right Thing(tm) both when called from conky and as standalone
This commit is contained in:
parent
b5216499c2
commit
5c49c52a0b
@ -1,3 +0,0 @@
|
||||
#! /usr/bin/lua
|
||||
assert(loadfile("convertluafuncs"))()
|
||||
print(convertconfigfile())
|
@ -37,8 +37,8 @@ endif(BUILD_BUILTIN_CONFIG)
|
||||
|
||||
if(BUILD_OLD_CONFIG)
|
||||
add_custom_command(OUTPUT convertconf.h COMMAND
|
||||
${CMAKE_SOURCE_DIR}/text2c.sh ${CMAKE_SOURCE_DIR}/convertluafuncs
|
||||
convertconf.h convertconf DEPENDS ${CMAKE_SOURCE_DIR}/convertluafuncs
|
||||
${CMAKE_SOURCE_DIR}/text2c.sh ${CMAKE_SOURCE_DIR}/extras/convert.lua
|
||||
convertconf.h convertconf DEPENDS ${CMAKE_SOURCE_DIR}/extras/convert.lua
|
||||
)
|
||||
add_custom_target(convertconf ALL DEPENDS convertconf.h)
|
||||
set_source_files_properties(convertconf.h PROPERTIES GENERATED TRUE)
|
||||
|
52
convertluafuncs → extras/convert.lua
Normal file → Executable file
52
convertluafuncs → extras/convert.lua
Normal file → Executable file
@ -1,3 +1,19 @@
|
||||
#! /usr/bin/lua
|
||||
|
||||
local usage = [[
|
||||
Usage: convert.lua <old_conkyrc >new_conkyrc
|
||||
|
||||
Tries to convert conkyrc from the old v1.x format to the new, lua-based format.
|
||||
Reads from stdin, writes to stdout.
|
||||
|
||||
Keep in mind that there is no guarantee that the output will work correctly
|
||||
with conky, or that it will be able to convert every conkyrc. However, it
|
||||
should provide a good starting point.
|
||||
|
||||
For more information about the new format, read the wiki page
|
||||
<http://wiki.conky.be/index.php?title=conky2rc_format>
|
||||
]];
|
||||
|
||||
local function quote(s)
|
||||
if not s:find("[\n']") then
|
||||
return "'" .. s .. "'";
|
||||
@ -6,7 +22,7 @@ local function quote(s)
|
||||
while s:find(']' .. q .. ']', 1, true) do
|
||||
q = q .. '=';
|
||||
end;
|
||||
return string.format('[%s[%s]%s]', q, s, q);
|
||||
return string.format('[%s[\n%s]%s]', q, s, q);
|
||||
end;
|
||||
|
||||
local bool_setting = {
|
||||
@ -92,11 +108,33 @@ local function convert(s)
|
||||
return handle(setting:match('^%s*(%S*)%s*(.-)%s*$')) .. comment;
|
||||
end;
|
||||
|
||||
function convertconfig(oldconfig)
|
||||
local settings, text = oldconfig:match('^(.-)TEXT\n(.*)$');
|
||||
return 'conky.config = {\n' .. settings:gsub('.-\n', convert) .. '};\n\n' .. 'conky.text = \n' .. quote(text) .. ';\n'
|
||||
local input;
|
||||
local output;
|
||||
|
||||
if conky == nil then
|
||||
-- we are run as a standalone program, read from stdin, write to stdout
|
||||
input = io.input();
|
||||
if #arg > 0 then
|
||||
-- if the user provided some arguments, it means he doesn't know how to use us
|
||||
-- -> print usage
|
||||
io.output():write(usage);
|
||||
return;
|
||||
end;
|
||||
else
|
||||
-- we are called from conky, the filename is the first argument
|
||||
input = io.open(..., 'r');
|
||||
end;
|
||||
|
||||
function convertconfigfile(filename)
|
||||
return convertconfig(io.input(filename):read('*a'))
|
||||
end
|
||||
|
||||
local config = input:read('*a');
|
||||
|
||||
local settings, text = config:match('^(.-)TEXT\n(.*)$');
|
||||
|
||||
local output = 'conky.config = {\n' .. settings:gsub('.-\n', convert) .. '};\n\nconky.text = ' ..
|
||||
quote(text) .. ';\n';
|
||||
|
||||
if conky == nil then
|
||||
io.output():write(output);
|
||||
else
|
||||
return assert(loadstring(output, 'converted config'));
|
||||
end;
|
28
src/conky.cc
28
src/conky.cc
@ -2655,32 +2655,14 @@ void load_config_file()
|
||||
#endif
|
||||
l.loadfile(current_config.c_str());
|
||||
}
|
||||
#ifdef BUILD_OLD_CONFIG
|
||||
catch(lua::syntax_error &e) {
|
||||
#ifdef BUILD_OLD_CONFIG
|
||||
l.loadstring(convertconf);
|
||||
l.call(0, 0);
|
||||
#ifdef BUILD_BUILTIN_CONFIG
|
||||
if(current_config == builtin_config_magic) {
|
||||
l.getglobal("convertconfig");
|
||||
l.pushstring(defconfig);
|
||||
} else {
|
||||
#endif /* BUILD_BUILTIN_CONFIG */
|
||||
l.getglobal("convertconfigfile");
|
||||
l.pushstring(current_config.c_str());
|
||||
#ifdef BUILD_BUILTIN_CONFIG
|
||||
}
|
||||
#endif /* BUILD_BUILTIN_CONFIG */
|
||||
// the strchr thingy skips the first line (#! /usr/bin/lua)
|
||||
l.loadstring(strchr(convertconf, '\n'));
|
||||
l.pushstring(current_config.c_str());
|
||||
l.call(1, 1);
|
||||
try {
|
||||
l.loadstring(l.tostring(-1).c_str());
|
||||
}
|
||||
catch(lua::syntax_error &e) {
|
||||
#endif
|
||||
throw conky::critical_error(_("syntax error in configfile"));
|
||||
#ifdef BUILD_OLD_CONFIG
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
catch(lua::file_error &e) { throw conky::critical_error(_("no configfile given")); }
|
||||
l.call(0, 0);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user