Advertisement
ReverseFlux

lua

Feb 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #! /usr/bin/lua
  2.  
  3. local usage = [[
  4. Usage: convert.lua old_conkyrc [new_conkyrc]
  5. Tries to convert conkyrc from the old v1.x format to the new, lua-based format.
  6. Keep in mind that there is no guarantee that the output will work correctly
  7. with conky, or that it will be able to convert every conkyrc. However, it
  8. should provide a good starting point.
  9. Altough you can use this script with only 1 arg and let it overwrite the old
  10. config, it's suggested to use 2 args so that the new config is written in a new
  11. file (so that you have backup if something went wrong).
  12. For more information about the new format, read the wiki page
  13. <http://wiki.conky.be/index.php?title=conky2rc_format>
  14. ]];
  15.  
  16. local function quote(s)
  17. if not s:find("[\n'\\]") then
  18. return "'" .. s .. "'";
  19. end;
  20. local q = '';
  21. while s:find(']' .. q .. ']', 1, true) do
  22. q = q .. '=';
  23. end;
  24. return string.format('[%s[\n%s]%s]', q, s, q);
  25. end;
  26.  
  27. local bool_setting = {
  28. background = true, disable_auto_reload = true, double_buffer = true, draw_borders = true,
  29. draw_graph_borders = true, draw_outline = true, draw_shades = true, extra_newline = true,
  30. format_human_readable = true, no_buffers = true, out_to_console = true,
  31. out_to_ncurses = true, out_to_stderr = true, out_to_x = true, override_utf8_locale = true,
  32. own_window = true, own_window_argb_visual = true, own_window_transparent = true,
  33. short_units = true, show_graph_range = true, show_graph_scale = true,
  34. times_in_seconds = true, top_cpu_separate = true, uppercase = true, use_xft = true
  35. };
  36.  
  37. local num_setting = {
  38. border_inner_margin = true, border_outer_margin = true, border_width = true,
  39. cpu_avg_samples = true, diskio_avg_samples = true, gap_x = true, gap_y = true,
  40. imlib_cache_flush_interval = true, imlib_cache_size = true,
  41. max_port_monitor_connections = true, max_text_width = true, max_user_text = true,
  42. maximum_width = true, mpd_port = true, music_player_interval = true, net_avg_samples = true,
  43. own_window_argb_value = true, pad_percents = true, stippled_borders = true,
  44. text_buffer_size = true, top_name_width = true, total_run_times = true,
  45. update_interval = true, update_interval_on_battery = true, xftalpha = true,
  46. xinerama_head = true,
  47. };
  48.  
  49. local split_setting = {
  50. default_bar_size = true, default_gauge_size = true, default_graph_size = true,
  51. minimum_size = true
  52. };
  53.  
  54. local colour_setting = {
  55. color0 = true, color1 = true, color2 = true, color3 = true, color4 = true, color5 = true,
  56. color6 = true, color7 = true, color8 = true, color9 = true, default_color = true,
  57. default_outline_color = true, default_shade_color = true, own_window_colour = true
  58. };
  59.  
  60. local function alignment_map(value)
  61. local map = { m = 'middle', t = 'top', b = 'bottom', r = 'right', l = 'left' };
  62. if map[value] == nil then
  63. return value;
  64. else
  65. return map[value];
  66. end;
  67. end;
  68.  
  69. local function handle(setting, value)
  70. setting = setting:lower();
  71. if setting == '' then
  72. return '';
  73. end;
  74. if split_setting[setting] then
  75. local x, y = value:match('^(%S+)%s*(%S*)$');
  76. local ret = setting:gsub('_size', '_width = ') .. x .. ',';
  77. if y ~= '' then
  78. ret = ret .. ' ' .. setting:gsub('_size', '_height = ') .. y .. ',';
  79. end;
  80. return '\t' .. ret;
  81. end;
  82. if bool_setting[setting] then
  83. value = value:lower();
  84. if value == 'yes' or value == 'true' or value == '1' or value == '' then
  85. value = 'true';
  86. else
  87. value = 'false';
  88. end;
  89. elseif not num_setting[setting] then
  90. if setting == 'alignment' and value:len() == 2 then
  91. value = alignment_map(value:sub(1,1)) .. '_' .. alignment_map(value:sub(2,2));
  92. elseif colour_setting[setting] and value:match('^[0-9a-fA-F]+$') then
  93. value = '#' .. value;
  94. elseif setting == 'xftfont' then
  95. setting = 'font';
  96. end;
  97. value = quote(value);
  98. end;
  99. return '\t' .. setting .. ' = ' .. value .. ',';
  100. end;
  101.  
  102. local function convert(s)
  103. local setting, comment = s:match('^([^#]*)#?(.*)\n$');
  104. if comment ~= '' then
  105. comment = '--' .. comment;
  106. end;
  107. comment = comment .. '\n';
  108. return handle(setting:match('^%s*(%S*)%s*(.-)%s*$')) .. comment;
  109. end;
  110.  
  111. local input;
  112. local output;
  113.  
  114. if conky == nil then --> standalone program
  115. -- 1 arg: arg is input and outputfile
  116. -- 2 args: 1st is inputfile, 2nd is outputfile
  117. -- 0, 3 or more args: print usage to STDERR and quit
  118. if #arg == 1 or #arg == 2 then
  119. input = io.input(arg[1]);
  120. else
  121. io.stderr:write(usage);
  122. return;
  123. end;
  124. else
  125. -- we are called from conky, the filename is the first argument
  126. input = io.open(..., 'r');
  127. end;
  128.  
  129.  
  130. local config = input:read('*a');
  131. input:close();
  132.  
  133. local settings, text = config:match('^(.-)TEXT\n(.*)$');
  134.  
  135. local converted = 'conky.config = {\n' .. settings:gsub('.-\n', convert) .. '};\n\nconky.text = ' ..
  136. quote(text) .. ';\n';
  137.  
  138. if conky == nil then
  139. if #arg == 2 then
  140. output = io.output(arg[2]);
  141. else
  142. output = io.output(arg[1]);
  143. end
  144. output:write(converted);
  145. output:close();
  146. else
  147. return assert(loadstring(converted, 'converted config'));
  148. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement