-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeawesomemenu.lua
282 lines (259 loc) · 8.09 KB
/
makeawesomemenu.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env luajit
require('pl')
package.path = os.getenv("HOME") .. "/misc/dotfiles/awesome/?.lua;" .. package.path
local icontheme = require("currenttheme").icon_theme
local parsed = {}
local cat_mapping = {
["2DGraphics"] = "Graphics",
["Audio"] = "Sound & Video",
["AudioVideo"] = "Sound & Video",
["AudioVideoEditing"] = "Sound & Video",
["DesktopSettings"] = "System Tools",
--["Development"] = "Programming",
--["Education"] = "Education",
["FileManager"] = "Accessories",
["FileTools"] = "Accessories",
["Filesystem"] = "Accessories",
["FlowChart"] = "Graphics",
["Graphics"] = "Graphics",
["HardwareSettings"] = "System Tools",
--["Math"] = "Education",
["Mixer"] = "Accessories",
["Network"] = "Internet",
["Office"] = "Office",
["Player"] = "Sound & Video",
["Presentation"] = "Office",
["Printing"] = "System Tools",
["Publishing"] = "Office",
["RasterGraphics"] = "Graphics",
["Recorder"] = "Accessories",
--["Science"] = "Education",
["Settings"] = "System Tools",
["Spreadsheet"] = "Office",
["System"] = "System Tools",
["TV"] = "Sound & Video",
["TerminalEmulator"] = "System Tools",
["TextEditor"] = "Accessories",
["Utility"] = "Accessories",
["VectorGraphics"] = "Graphics",
["Video"] = "Sound & Video",
["WebApps"] = "Internet",
["WebBrowser"] = "Internet",
["WordProcessor"] = "Office"
}
local menus = {
{"Accessories", {}},
--Education = {},
{"Graphics", {}},
{"Internet", {}},
{"Office", {}},
--Programming = {},
{"Sound & Video", {}},
{"System Tools", {}}
}
local desktop_folders = {
os.getenv("HOME") .. '/.local/share/applications',
'/usr/share/applications'
}
local iconlocations = {
os.getenv("HOME") .. "/.local/share/icons/" .. icontheme,
os.getenv("HOME") .. "/.local/share/icons/hicolor",
"/usr/share/icons/" .. icontheme,
"/usr/share/icons/hicolor"
}
local iconsizes = {
'48x48',
'scalable',
'64x64',
'96x96',
'128x128',
'144x144',
'24x24',
'32x32',
'192x192',
'16x16',
'256x256',
'512x512'
}
local iconfolders = {
'apps',
'categories',
'mimetypes',
'devices'
}
local importantkeys = {
'Categories',
'Name',
'Exec',
'Terminal',
'Icon',
'NoDisplay',
'OnlyShowIn'
}
local outputfile = os.getenv("HOME") .. "/misc/dotfiles/awesome/xdgmenu.lua"
local termcmd='kitty'
-- local termcmd='wezterm start --'
----------------------------------------
-- FUNCTIONS
----------------------------------------
function categoryicon(cat)
if (cat == "Accessories") then
return iconpath('applications-accessories')
elseif (cat == "Graphics") then
return iconpath('applications-graphics')
elseif (cat == "Internet") then
return iconpath('applications-internet')
elseif (cat == "Office") then
return iconpath('applications-office')
elseif (cat == "Sound & Videos") then
return iconpath('applications-multimedia')
elseif (cat == "System Tools") then
return iconpath('applications-system')
else
return iconpath('applications-all')
end
end
function fixedexec(execstr, termstr)
local rv = ''
if (termstr == 'true') then
rv = rv .. termcmd .. ' '
end
rv = rv .. execstr:gsub(' %%[A-Za-z]','')
return rv
end
function handle_desktopfile(desktopfile)
local props = {}
local f = io.open(desktopfile)
for line in f:lines() do
if (line:match('=')) then
local linesplit = stringx.split(line,'=',2)
linekey, lineval = linesplit[1], linesplit[2]
if ((props[linekey] == nil) and (tablex.find(importantkeys, linekey))) then
props[linekey] = lineval
end
end
end
-- skip those with nodisplay
if ((props.NoDisplay) and (props.NoDisplay == "true")) then
return
end
if ((props.OnlyShowIn) and (not(props.OnlyShowIn:match('[Aa]wesome')))) then
return
end
local newentry = {
props.Name,
fixedexec(props.Exec, props.Terminal),
iconpath(props.Icon)
}
-- sioyek's categorization is nonsense
if (props.Name == 'Sioyek') then
props.Categories = "Office"
end
if (props.Categories) then
local mycats = stringx.split(props.Categories,';')
local submenus = {}
for k, cat in ipairs(mycats) do
if ((cat_mapping[cat]) and
(tablex.find(submenus,cat_mapping[cat]) == nil)) then
table.insert(submenus, cat_mapping[cat])
end
end
local lowername = newentry[1]:lower()
for l, submenu in ipairs(submenus) do
for n, mgroup in ipairs(menus) do
if (mgroup[1] == submenu) then
local entered = false
for m, oldentry in ipairs(mgroup[2]) do
local oldlowername = oldentry[1]:lower()
if (lowername < oldlowername) then
table.insert(mgroup[2], m, newentry)
entered = true
break
end
end
if (not(entered)) then
table.insert(mgroup[2], newentry)
end
break
end
end
end
end
end
function iconpath(iconname)
iconname = iconname or 'application-default-icon'
local prevext = path.extension(iconname)
local hasext = ((prevext == ".png") or (prevext == ".svg"))
local haspath = (iconname:sub(1,1) == '/')
-- has both, check if it exists
if (((hasext) and (haspath)) and (path.exists(iconname))) then
return iconname
else
-- doesn't exist; try to look for icon with same basename
iconname = path.basename(iconname)
haspath = false
end
-- strip extensions
if (hasext) then
iconname = iconname:gsub('%.[^%.]*$','')
end
local searchexts = { '.svg', '.png' }
-- look for pngs first if specified as png
if (prevext == '.png') then
searchexts = { '.png', '.svg' }
end
-- if has path, look there first
if (haspath) then
for e, ext in ipairs(searchexts) do
if path.exists(iconname .. ext) then
return iconname .. ext
end
end
-- wasn't found; strip path
iconname = path.basename(iconname)
end
for sn, isize in ipairs(iconsizes) do
for sc, ifldr in ipairs(iconfolders) do
for p, spath in ipairs(iconlocations) do
local fullspath = spath .. '/' .. isize .. '/' .. ifldr
if (path.isdir(fullspath)) then
for e, ext in ipairs(searchexts) do
local fulliconpath = fullspath .. '/' .. iconname .. ext
if (path.exists(fulliconpath)) then
return fulliconpath
end
end
end
end
end
end
-- last ditch; look at top level (damn you sioyek!)
for p, spath in ipairs(iconlocations) do
local parpath = path.dirname(spath)
for e, ext in ipairs(searchexts) do
local fulliconpath = parpath .. '/' .. iconname .. ext
if (path.exists(fulliconpath)) then
return fulliconpath
end
end
end
return iconname
end
----------------------------------------
-- MAIN ROUTINE
----------------------------------------
-- fill in menu group icons
for x, mgroup in ipairs(menus) do
table.insert(mgroup, categoryicon(mgroup[1]))
end
for i, folder in ipairs(desktop_folders) do
local all_desktops = dir.getallfiles(folder, '*.desktop')
for j, desktopfile in ipairs(all_desktops) do
basename = path.basename(desktopfile)
if (tablex.find(parsed, basename) == nil) then
handle_desktopfile(desktopfile)
table.insert(parsed, basename)
end
end
end
file.write(outputfile, 'xdgmenu = ' .. pretty.write(menus, ' '))