ram-widget.lua_Default 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. local awful = require("awful")
  2. local beautiful = require("beautiful")
  3. local gears = require("gears")
  4. local watch = require("awful.widget.watch")
  5. local wibox = require("wibox")
  6. local ramgraph_widget = {}
  7. local function worker(user_args)
  8. local args = user_args or {}
  9. local timeout = args.timeout or 1
  10. local color_used = args.color_used or beautiful.bg_urgent
  11. local color_free = args.color_free or beautiful.fg_normal
  12. local color_buf = args.color_buf or beautiful.border_color_active
  13. local widget_show_buf = args.widget_show_buf or false
  14. local widget_height = args.widget_height or 25
  15. local widget_width = args.widget_width or 25
  16. --- Main ram widget shown on wibar
  17. ramgraph_widget = wibox.widget {
  18. border_width = 0,
  19. colors = {
  20. color_used,
  21. color_free,
  22. color_buf,
  23. },
  24. display_labels = false,
  25. forced_height = widget_height,
  26. forced_width = widget_width,
  27. widget = wibox.widget.piechart
  28. }
  29. --- Widget which is shown when user clicks on the ram widget
  30. local popup = awful.popup{
  31. ontop = true,
  32. visible = false,
  33. widget = {
  34. widget = wibox.widget.piechart,
  35. forced_height = 200,
  36. forced_width = 400,
  37. colors = {
  38. color_used,
  39. color_free,
  40. color_buf, -- buf_cache
  41. },
  42. },
  43. shape = gears.shape.rounded_rect,
  44. border_color = beautiful.border_color_active,
  45. border_width = 1,
  46. offset = { y = 5 },
  47. }
  48. --luacheck:ignore 231
  49. local total, used, free, shared, buff_cache, available, total_swap, used_swap, free_swap
  50. local function getPercentage(value)
  51. return math.floor(value / (total+total_swap) * 100 + 0.5) .. '%'
  52. end
  53. watch('bash -c "LANGUAGE=en_US.UTF-8 free | grep -z Mem.*Swap.*"', timeout,
  54. function(widget, stdout)
  55. total, used, free, shared, buff_cache, available, total_swap, used_swap, free_swap =
  56. stdout:match('(%d+)%s*(%d+)%s*(%d+)%s*(%d+)%s*(%d+)%s*(%d+)%s*Swap:%s*(%d+)%s*(%d+)%s*(%d+)')
  57. if widget_show_buf then
  58. widget.data = { used, free, buff_cache }
  59. else
  60. widget.data = { used, total-used }
  61. end
  62. if popup.visible then
  63. popup:get_widget().data_list = {
  64. {'used ' .. getPercentage(used + used_swap), used + used_swap},
  65. {'free ' .. getPercentage(free + free_swap), free + free_swap},
  66. {'buff_cache ' .. getPercentage(buff_cache), buff_cache}
  67. }
  68. end
  69. end,
  70. ramgraph_widget
  71. )
  72. ramgraph_widget:buttons(
  73. awful.util.table.join(
  74. awful.button({}, 1, function()
  75. popup:get_widget().data_list = {
  76. {'used ' .. getPercentage(used + used_swap), used + used_swap},
  77. {'free ' .. getPercentage(free + free_swap), free + free_swap},
  78. {'buff_cache ' .. getPercentage(buff_cache), buff_cache}
  79. }
  80. if popup.visible then
  81. popup.visible = not popup.visible
  82. else
  83. popup:move_next_to(mouse.current_widget_geometry)
  84. end
  85. end)
  86. )
  87. )
  88. return ramgraph_widget
  89. end
  90. return setmetatable(ramgraph_widget, { __call = function(_, ...)
  91. return worker(...)
  92. end })