widget.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. -- vim:fileencoding=utf-8:foldmethod=marker:foldmarker={{{,}}}
  2. -- {{{ Libraries
  3. local awful = require("awful")
  4. local wibox = require("wibox")
  5. local watch = require("awful.widget.watch")
  6. local naughty = require("naughty")
  7. -- }}}
  8. -- {{{ Variables
  9. local cpu_temp_widget = {}
  10. -- uses for notify
  11. local last_id
  12. -- }}}
  13. -- {{{ Worker function
  14. local function worker(user_args)
  15. -- {{{ Variables
  16. local args = user_args or {}
  17. local widget_dir = os.getenv("HOME") .. '/.config/awesome/widgets/temperature/'
  18. local font = args.font or 'FontAwesome bold 9'
  19. local refresh_rate = args.refresh_rate or 5
  20. --local GET_TEMP_COMMAND = args.GET_TEMP_COMMAND or "sensors | awk '/^CPU/ {t=substr($2,2,4); print t}'"
  21. local GET_TEMP_COMMAND = "/bin/bash -c \"sensors | awk '/^CPU/ {t=substr($2,2,4); print t}'\""
  22. --naughty.notify({text = GET_TEMP_COMMAND})
  23. local low_temp_color = args.low_temp_color or "#669900"
  24. local medium_temp_color = args.medium_temp_color or "#F5F549"
  25. local high_temp_color = args.high_temp_color or "#FF8000"
  26. local critical_temp_color = args.critical_temp_color or "#FF0000"
  27. local medium_temp_value = args.medium_temp_value or 60.0
  28. local high_temp_value = args.high_temp_value or 70.0
  29. local critical_temp_value = args.critical_temp_value or 80.0
  30. local critical_temp_icon = args.critical_temp_icon or widget_dir.."hot.png"
  31. -- End variables}}}
  32. -- {{{ Work functions
  33. -- function get_cpu_temp()
  34. -- local cpu_temp = -1
  35. -- awful.spawn.easy_async_with_shell(command, function (out)
  36. -- cpu_temp = out
  37. -- end)
  38. -- return cpu_temp
  39. -- end
  40. -- End work functions }}}
  41. -- {{{ Widget settings
  42. cpu_temp_widget = wibox.widget {
  43. {
  44. id = 'tempw',
  45. font = font,
  46. widget = wibox.widget.textbox,
  47. text = "init text",
  48. },
  49. layout = wibox.layout.align.horizontal,
  50. set_temp = function (self, cpu_temp)
  51. local set_color = "<span color='"
  52. local output_temp
  53. --local color = "#ff44ff"
  54. if tonumber(cpu_temp) > tonumber(critical_temp_value) then
  55. set_color = set_color..critical_temp_color.."'>"
  56. last_id = naughty.notify ({
  57. title = "CPU temp is critical",
  58. text = "CPU temperature is dangerously hot, turn it off to prevent damage.",
  59. preset = naughty.config.presets.critical,
  60. replaces_id = last_id,
  61. icon = critical_temp_icon,
  62. }).id
  63. --cpu_temp = cpu_temp .. " "
  64. output_temp = cpu_temp:match('%d+.%d+') .. " "
  65. --color = critical_temp_color
  66. elseif tonumber(cpu_temp) > tonumber(high_temp_value) then
  67. set_color = set_color..high_temp_color.."'>"
  68. --cpu_temp = tostring(cpu_temp) .. " "
  69. output_temp = cpu_temp:match('(%d+.%d+)') .. " "
  70. --color = high_temp_color
  71. elseif tonumber(cpu_temp) > tonumber(medium_temp_value) then
  72. set_color = set_color..medium_temp_color.."'>"
  73. --cpu_temp = tostring(cpu_temp) .. " "
  74. output_temp = cpu_temp:match('(%d+.%d+)') .. " "
  75. --color = medium_temp_color
  76. else
  77. set_color = set_color..low_temp_color.."'>"
  78. output_temp = cpu_temp:match('(%d+.%d+)') .. " "
  79. --color = low_temp_color
  80. end
  81. self:get_children_by_id('tempw')[1]:set_markup(set_color..output_temp.."</span>")
  82. --self:get_children_by_id('tempw')[1]:set_markup("<span color='"..color.."'>"..cpu_temp.."</span>")
  83. end
  84. }
  85. -- End widget settings }}}
  86. local update_widget_text = function (widget, stdout, _, _, _)
  87. widget:set_temp(stdout)
  88. end
  89. watch(GET_TEMP_COMMAND, refresh_rate, update_widget_text, cpu_temp_widget)
  90. return cpu_temp_widget
  91. end
  92. return setmetatable(cpu_temp_widget, {
  93. __call = function (_, ...)
  94. return worker(...)
  95. end
  96. })
  97. -- End worker function}}}