ram-widget-icon.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. local beautiful = require("beautiful")
  2. local wibox = require("wibox")
  3. local awful = require("awful")
  4. local gears = require("gears")
  5. local ram_widget_icon = {}
  6. local config = {}
  7. local CMD = [[bash -c "ps axch -o cmd:17,%mem --sort=-%mem | head -5"]]
  8. config.popup_bg_color = "#222222"
  9. config.popup_border_width = 1
  10. config.popup_border_color = "#7e7e7e"
  11. config.popup_height = 6
  12. config.popup_width = 300
  13. config.font_ubuntu = "Ubuntu Nerd Font 10"
  14. config.terminal = "alacritty"
  15. config.shell = "/usr/bin/fish"
  16. local function worker(user_args)
  17. local args, _config = user_args or {}, {}
  18. for prop, value in pairs(config) do
  19. _config[prop] = args[prop] or beautiful[prop] or value
  20. end
  21. -- Main ram_widget_icon shown on wibar
  22. ram_widget_icon = wibox.widget {
  23. {
  24. id = "txt_icon",
  25. text = " ",
  26. font = args.font,
  27. widget = wibox.widget.textbox,
  28. },
  29. valign = "center",
  30. layout = wibox.layout.align.horizontal,
  31. -- layout = wibox.container.place,
  32. }
  33. -- Popup scrolling
  34. local rows, ptr = wibox.layout.fixed.vertical(), 0
  35. rows:connect_signal("button::press", function(_,_,_,button)
  36. if button == 4 then
  37. if ptr > 0 then
  38. rows.children[ptr].visible = true
  39. ptr = ptr - 1
  40. end
  41. elseif button == 5 then
  42. if ptr < #rows.children and ((#rows.children - ptr) > _config.popup_height) then
  43. ptr = ptr + 1
  44. rows.children[ptr].visible = false
  45. end
  46. end
  47. end)
  48. local popup = awful.popup {
  49. border_width = _config.popup_border_width,
  50. border_color = _config.popup_border_color,
  51. shape = gears.shape.rounded_rect,
  52. visible = false,
  53. ontop = true,
  54. offset = { y = 5 },
  55. widget = {}
  56. }
  57. popup:buttons(
  58. awful.util.table.join(
  59. awful.button({}, 3, function()
  60. popup.visible = not popup.visible
  61. end)
  62. )
  63. )
  64. ram_widget_icon:buttons(
  65. awful.util.table.join(
  66. awful.button({}, 1, function()
  67. if popup.visible then
  68. popup.visible = false
  69. else
  70. popup.visible = true
  71. popup:move_next_to(mouse.current_widget_geometry)
  72. -- popup:move_next_to(_G.mouse.current_widget_geometry)
  73. end
  74. end),
  75. awful.button({}, 3, function()
  76. awful.spawn(_config.terminal.." -e ".._config.shell.." -c htop")
  77. end)
  78. )
  79. )
  80. -- Show top 5 processes
  81. -- awful.spawn.easy_async_with_shell([[bash -c "ps axch -o cmd:17,%mem --sort=-%mem | head -5"]],
  82. awful.spawn.easy_async_with_shell(CMD,
  83. function(stdout)
  84. local top_tbl = {}
  85. for value in stdout:gmatch("([^\n]+)") do
  86. top_tbl[#top_tbl+1] = value.." %"
  87. end
  88. local popup_header_height, popup_row_height = 30, 20
  89. local header = wibox.widget {
  90. {
  91. nil,
  92. {
  93. markup = "<b> Top of MEMORY usage:</b>",
  94. layout = wibox.widget.textbox,
  95. },
  96. expand = "none",
  97. layout = wibox.layout.align.horizontal,
  98. },
  99. forced_height = popup_header_height,
  100. left = 20,
  101. right = 20,
  102. layout = wibox.container.margin
  103. }
  104. for k, v in ipairs(top_tbl) do
  105. for i = 1, #rows.children do
  106. if v == rows.children[i].get_txt() then goto continue end
  107. end
  108. local row = wibox.widget{
  109. {
  110. id = "idx",
  111. text = tostring(k),
  112. widget = wibox.widget.textbox
  113. },
  114. {
  115. id = "txt",
  116. text = v,
  117. forced_height = popup_row_height,
  118. paddings = 1,
  119. widget = wibox.widget.textbox
  120. },
  121. layout = wibox.layout.ratio.horizontal,
  122. }
  123. function row.get_txt() return row:get_children_by_id("txt")[1].text end
  124. function row.set_idx(idx) row:get_children_by_id("idx")[1]:set_text(idx) end
  125. row:ajust_ratio(2, 0.1, 0.9, 0)
  126. rows:insert(k, row)
  127. ::continue::
  128. end
  129. local height = popup_header_height + math.min(#top_tbl, _config.popup_height) * (popup_row_height + 1) -- +1 corrects popup output!!!
  130. popup:setup {
  131. {
  132. {
  133. {
  134. {
  135. header,
  136. rows,
  137. forced_height = height,
  138. layout = wibox.layout.fixed.vertical
  139. },
  140. content_fill_horizontal = true,
  141. layout = wibox.container.place
  142. },
  143. margins = 10,
  144. layout = wibox.container.margin
  145. },
  146. bg = _config.popup_bg_color,
  147. layout = wibox.container.background
  148. },
  149. forced_width = _config.popup_width,
  150. layout = wibox.layout.fixed.horizontal
  151. }
  152. end
  153. )
  154. -- Set fg and bg colors for ram_widget_icon
  155. local ram_widget_icon_clr = wibox.widget.background()
  156. ram_widget_icon_clr:set_widget(ram_widget_icon)
  157. ram_widget_icon_clr:set_fg("#ffcb6b")
  158. -- return wibox.container.background(ram_widget_icon, "#ff0000")
  159. return ram_widget_icon_clr
  160. end
  161. return setmetatable(ram_widget_icon, { __call = function(_, ...) return worker(...) end })