-
-
Notifications
You must be signed in to change notification settings - Fork 620
/
convert.lua
executable file
·161 lines (140 loc) · 5.59 KB
/
convert.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /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.
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.
Although you can use this script with only 1 arg and let it overwrite the old
config, it's suggested to use 2 args so that the new config is written in a new
file (so that you have backup if something went wrong).
Optional: Install dos2unix. We will attempt to use this if it is available
because Conky configs downloaded from Internet sometimes are created on DOS/Windows
machines with different line endings than Conky configs created on Unix/Linux.
For more information about the new format, read the wiki page
<https://github.com/brndnmtthws/conky/wiki>
]];
local function quote(s)
if not s:find("[\n'\\]") then
return "'" .. s .. "'";
end;
local q = '';
while s:find(']' .. q .. ']', 1, true) do
q = q .. '=';
end;
return string.format('[%s[\n%s]%s]', q, s, q);
end;
local bool_setting = {
background = true, disable_auto_reload = true, double_buffer = true, draw_borders = true,
draw_graph_borders = true, draw_outline = true, draw_shades = true, extra_newline = true,
format_human_readable = true, no_buffers = true, out_to_console = true,
out_to_wayland = true,
out_to_ncurses = true, out_to_stderr = true, out_to_x = true, override_utf8_locale = true,
own_window = true, own_window_argb_visual = true, own_window_transparent = true,
short_units = true, show_graph_range = true, show_graph_scale = true,
times_in_seconds = true, top_cpu_separate = true, uppercase = true, use_xft = true,
draw_blended = true, forced_redraw = true
};
local num_setting = {
border_inner_margin = true, border_outer_margin = true, border_width = true,
cpu_avg_samples = true, diskio_avg_samples = true, gap_x = true, gap_y = true,
imlib_cache_flush_interval = true, imlib_cache_size = true,
max_port_monitor_connections = true, max_text_width = true, max_user_text = true,
maximum_width = true, mpd_port = true, music_player_interval = true, net_avg_samples = true,
own_window_argb_value = true, pad_percents = true, stippled_borders = true,
text_buffer_size = true, top_name_width = true, total_run_times = true,
update_interval = true, update_interval_on_battery = true, xftalpha = true,
xinerama_head = true,
};
local split_setting = {
default_bar_size = true, default_gauge_size = true, default_graph_size = true,
minimum_size = true
};
local colour_setting = {
color0 = true, color1 = true, color2 = true, color3 = true, color4 = true, color5 = true,
color6 = true, color7 = true, color8 = true, color9 = true, default_color = true,
default_outline_color = true, default_shade_color = true, own_window_colour = true
};
local function alignment_map(value)
local map = { m = 'middle', t = 'top', b = 'bottom', r = 'right', l = 'left' };
if map[value] == nil then
return value;
else
return map[value];
end;
end;
local function handle(setting, value)
setting = setting:lower();
if setting == '' then
return '';
end;
if split_setting[setting] then
local x, y = value:match('^(%S+)%s*(%S*)$');
local ret = setting:gsub('_size', '_width = ') .. x .. ',';
if y ~= '' then
ret = ret .. ' ' .. setting:gsub('_size', '_height = ') .. y .. ',';
end;
return '\t' .. ret;
end;
if bool_setting[setting] then
value = value:lower();
if value == 'yes' or value == 'true' or value == '1' or value == '' then
value = 'true';
else
value = 'false';
end;
elseif not num_setting[setting] then
if setting == 'alignment' and value:len() == 2 then
value = alignment_map(value:sub(1,1)) .. '_' .. alignment_map(value:sub(2,2));
elseif colour_setting[setting] and value:match('^[0-9a-fA-F]+$') then
value = '#' .. value;
elseif setting == 'xftfont' then
setting = 'font';
end;
value = quote(value);
end;
return '\t' .. setting .. ' = ' .. value .. ',';
end;
local function convert(s)
local setting, comment = s:match('^([^#]*)#?(.*)\n$');
if comment ~= '' then
comment = '--' .. comment;
end;
comment = comment .. '\n';
return handle(setting:match('^%s*(%S*)%s*(.-)%s*$')) .. comment;
end;
local input;
local output;
if conky == nil then --> standalone program
-- 1 arg: arg is input and outputfile
-- 2 args: 1st is inputfile, 2nd is outputfile
-- 0, 3 or more args: print usage to STDERR and quit
if #arg == 1 or #arg == 2 then
if os.execute('command -v dos2unix 2>&1 >/dev/null') == 0 then
os.execute('dos2unix ' .. arg[1]);
end
input = io.input(arg[1]);
else
io.stderr:write(usage);
return;
end;
else
-- we are called from conky, the filename is the first argument
input = io.open(..., 'r');
end;
local config = input:read('*a');
input:close();
local settings, text = config:match('^(.-)TEXT\n(.*)$');
local converted = 'conky.config = {\n' .. settings:gsub('.-\n', convert) .. '};\n\nconky.text = ' ..
quote(text) .. ';\n';
if conky == nil then
if #arg == 2 then
output = io.output(arg[2]);
else
output = io.output(arg[1]);
end
output:write(converted);
output:close();
else
return assert(load(converted, 'converted config'));
end;