popups.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. --local beautiful = require("beautiful")
  2. --@author cedlemo
  3. local naughty = require("naughty")
  4. local os = require("os")
  5. local awful = require("awful")
  6. local helpers =require("blingbling.helpers")
  7. local string = require("string")
  8. local superproperties = require('blingbling.superproperties')
  9. ---Differents popups for Awesome widgets
  10. --@module blingbling.popups
  11. local function colorize(string, pattern, color)
  12. local mystring=""
  13. mystring=string.gsub(string,pattern,'<span color="'..color..'">%1</span>')
  14. return mystring
  15. end
  16. local processpopup = nil
  17. local processstats = nil
  18. local proc_offset = 25
  19. local function hide_process_info()
  20. if processpopup ~= nil then
  21. naughty.destroy(processpopup)
  22. processpopup = nil
  23. proc_offset = 25
  24. end
  25. end
  26. local function show_process_info(inc_proc_offset, title_color,user_color, root_color)
  27. local save_proc_offset = proc_offset
  28. hide_process_info()
  29. proc_offset = save_proc_offset + inc_proc_offset
  30. processstats = awful.util.pread('/bin/ps --sort -c,-s -eo fname,user,%cpu,%mem,pid,gid,ppid,tname,label | /usr/bin/head -n '..proc_offset)
  31. processstats = colorize(processstats, "COMMAND", title_color)
  32. processstats = colorize(processstats, "USER", title_color)
  33. processstats = colorize(processstats, "%%CPU", title_color)
  34. processstats = colorize(processstats, "%%MEM", title_color)
  35. processstats = colorize(processstats, " PID", title_color)
  36. processstats = colorize(processstats, "GID", title_color)
  37. processstats = colorize(processstats, "PPID", title_color)
  38. processstats = colorize(processstats, "TTY", title_color)
  39. processstats = colorize(processstats, "LABEL", title_color)
  40. processstats = colorize(processstats, "root", root_color)
  41. processstats = colorize(processstats, os.getenv("USER"), user_color)
  42. processpopup = naughty.notify({
  43. text = processstats,
  44. timeout = 0, hover_timeout = 0.5,
  45. })
  46. end
  47. ---Top popup.
  48. --It binds a colorized output of the top command to a widget, and the possibility to launch htop with a click on the widget.
  49. --</br>Example blingbling.popups.htop(mycairograph,{ title_color = "#rrggbbaa", user_color = "#rrggbbaa", root_color="#rrggbbaa", terminal = "urxvt"})
  50. --</br>The terminal parameter is not mandatory, htop will be launch in xterm. Mandatory arguments:
  51. -- <ul> <li>title_color define the color of the title's columns.</li>
  52. -- <li>user_color display the name of the current user with this color in the top output.</li>
  53. -- <li>root_color display the root name with this color in the top output. </li></ul>
  54. --@param mywidget the widget
  55. --@param args a table of arguments { title_color = "#rrggbbaa", user_color = "#rrggbbaa", root_color="#rrggbbaa", terminal = a terminal name})
  56. function htop(mywidget, args)
  57. local args = args or {}
  58. mywidget:connect_signal("mouse::enter", function()
  59. show_process_info(0, args["title_color"] or superproperties.htop_title_color,
  60. args["user_color"] or superproperties.htop_user_color,
  61. args["root_color"] or superproperties.htop_root_color)
  62. end)
  63. mywidget:connect_signal("mouse::leave", function()
  64. hide_process_info()
  65. end)
  66. mywidget:buttons(awful.util.table.join(
  67. awful.button({ }, 4, function()
  68. show_process_info(-1, args["title_color"] or superproperties.htop_title_color,
  69. args["user_color"] or superproperties.htop_user_color,
  70. args["root_color"] or superproperties.htop_root_color)
  71. end),
  72. awful.button({ }, 5, function()
  73. show_process_info(1, args["title_color"] or superproperties.htop_title_color,
  74. args["user_color"] or superproperties.htop_user_color,
  75. args["root_color"] or superproperties.htop_root_color)
  76. end),
  77. awful.button({ }, 1, function()
  78. if args["terminal"] then
  79. awful.util.spawn_with_shell(args["terminal"] .. " -e htop")
  80. else
  81. awful.util.spawn_with_shell("xterm" .. " -e htop")
  82. end
  83. end)
  84. ))
  85. end
  86. local netpopup = nil
  87. local function get_netinfo( my_title_color, my_established_color, my_listen_color)
  88. local str=awful.util.pread('/bin/netstat -pa -u -t | grep -v TIME_WAIT')
  89. str=colorize(str,"Proto", my_title_color)
  90. str=colorize(str,'Recv%XQ', my_title_color)
  91. str=colorize(str,"Send%XQ", my_title_color)
  92. str=colorize(str,"Local Address", my_title_color)
  93. str=colorize(str,"Foreign Address", my_title_color)
  94. str=colorize(str,"State", my_title_color)
  95. str=colorize(str,'PID/Program name', my_title_color)
  96. str=colorize(str,"Security Context", my_title_color)
  97. str=colorize(str,"ESTABLISHED", my_established_color)
  98. str=colorize(str,"LISTEN", my_listen_color)
  99. return str
  100. end
  101. local function hide_netinfo()
  102. if netpopup ~= nil then
  103. naughty.destroy(netpopup)
  104. netpopup = nil
  105. end
  106. end
  107. local function show_netinfo(c1,c2,c3)
  108. hide_netinfo()
  109. netpopup=naughty.notify({
  110. text = get_netinfo(c1,c2,c3),
  111. timeout = 0, hover_timeout = 0.5,
  112. })
  113. end
  114. ---Netstat popup.
  115. --It binds a colorized output of the netstat command to a widget.
  116. --</br>Example: blingbling.popups.netstat(net,{ title_color = "#rrggbbaa", established_color= "#rrggbbaa", listen_color="#rrggbbaa"})
  117. --</br>Mandatory arguments:
  118. --<ul><li>widget (if blinbling widget add .widget ex: cpu.widget, if textbox or image box just put the widget name)</li>
  119. --<li>title_color define the color of the title's columns.</li>
  120. --<li>established_color display the state "ESTABLISHED" of a connexion with this color in the netstat output.</li>
  121. --<li>listen_color display the state "LISTEN" with this color in the netstat output.</li></ul>
  122. --@param mywidget the widget
  123. --@param args a table { title_color = "#rrggbbaa", established_color= "#rrggbbaa", listen_color="#rrggbbaa"}
  124. function netstat(mywidget, args)
  125. local args = args or {}
  126. mywidget:connect_signal("mouse::enter", function()
  127. show_netinfo( args["title_color"] or superproperties.netstat_title_color,
  128. args["established_color"] or superproperties.netstat_established_color,
  129. args["listen_color"] or superproperties.netstat_listen_color)
  130. end)
  131. mywidget:connect_signal("mouse::leave", function()
  132. hide_netinfo()
  133. end)
  134. end
  135. return {
  136. htop = htop ,
  137. netstat = netstat
  138. }