All of my important config files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.9 KiB

  1. --[[
  2. Awesome-Freedesktop
  3. Freedesktop.org compliant desktop entries and menu
  4. Menu section
  5. Licensed under GNU General Public License v2
  6. * (c) 2016, Luke Bonham
  7. * (c) 2014, Harvey Mittens
  8. --]]
  9. local awful_menu = require("awful.menu")
  10. local menu_gen = require("menubar.menu_gen")
  11. local menu_utils = require("menubar.utils")
  12. local icon_theme = require("menubar.icon_theme")
  13. local io, pairs, string, table, os = io, pairs, string, table, os
  14. -- Add support for NixOS systems too
  15. table.insert(menu_gen.all_menu_dirs, string.format("%s/.nix-profile/share/applications", os.getenv("HOME")))
  16. -- Expecting a wm_name of awesome omits too many applications and tools
  17. menu_utils.wm_name = ""
  18. -- Menu
  19. -- freedesktop.menu
  20. local menu = {}
  21. -- Determines if a path points to a directory, by checking if it can be read
  22. -- (which is `nil` also for empty files) and if its size is not 0.
  23. -- @author blueyed
  24. -- @param path the path to check
  25. function menu.is_dir(path)
  26. local f = io.open(path)
  27. return f and not f:read(0) and f:seek("end") ~= 0 and f:close()
  28. end
  29. -- Remove non existent paths in order to avoid issues
  30. local existent_paths = {}
  31. for k,v in pairs(menu_gen.all_menu_dirs) do
  32. if menu.is_dir(v) then
  33. table.insert(existent_paths, v)
  34. end
  35. end
  36. menu_gen.all_menu_dirs = existent_paths
  37. -- Determines whether an table includes a certain element
  38. -- @param tab a given table
  39. -- @param val the element to search for
  40. -- @return true if the given string is found within the search table; otherwise, false if not
  41. function menu.has_value (tab, val)
  42. for index, value in pairs(tab) do
  43. if val:find(value) then
  44. return true
  45. end
  46. end
  47. return false
  48. end
  49. -- Use MenuBar parsing utils to build a menu for Awesome
  50. -- @return awful.menu
  51. function menu.build(args)
  52. local args = args or {}
  53. local icon_size = args.icon_size
  54. local before = args.before or {}
  55. local after = args.after or {}
  56. local skip_items = args.skip_items or {}
  57. local sub_menu = args.sub_menu or false
  58. local result = {}
  59. local _menu = awful_menu({ items = before })
  60. menu_gen.generate(function(entries)
  61. -- Add category icons
  62. for k, v in pairs(menu_gen.all_categories) do
  63. table.insert(result, { k, {}, v.icon })
  64. end
  65. -- Get items table
  66. for k, v in pairs(entries) do
  67. for _, cat in pairs(result) do
  68. if cat[1] == v.category then
  69. if not menu.has_value(skip_items, v.name) then
  70. table.insert(cat[2], { v.name, v.cmdline, v.icon })
  71. end
  72. break
  73. end
  74. end
  75. end
  76. -- Cleanup things a bit
  77. for i = #result, 1, -1 do
  78. local v = result[i]
  79. if #v[2] == 0 then
  80. -- Remove unused categories
  81. table.remove(result, i)
  82. else
  83. --Sort entries alphabetically (by name)
  84. table.sort(v[2], function (a, b) return string.byte(a[1]) < string.byte(b[1]) end)
  85. -- Replace category name with nice name
  86. v[1] = menu_gen.all_categories[v[1]].name
  87. end
  88. end
  89. -- Sort categories alphabetically also
  90. table.sort(result, function(a, b) return string.byte(a[1]) < string.byte(b[1]) end)
  91. -- Add menu item to hold the generated menu
  92. if sub_menu then
  93. result = {{sub_menu, result}}
  94. end
  95. -- Add items to menu
  96. for _, v in pairs(result) do _menu:add(v) end
  97. for _, v in pairs(after) do _menu:add(v) end
  98. end)
  99. -- Set icon size
  100. if icon_size then
  101. for _,v in pairs(menu_gen.all_categories) do
  102. v.icon = icon_theme():find_icon_path(v.icon_name, icon_size)
  103. end
  104. end
  105. -- Hold the menu in the module
  106. menu.menu = _menu
  107. return _menu
  108. end
  109. return menu