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.

492 lines
14KB

  1. -- vim: set fdm=indent
  2. -- conky uses lua5.1. lua51-utf8 package is needed
  3. utf8 = require 'lua-utf8'
  4. require 'cairo'
  5. netuphist = {}
  6. netdownhist = {}
  7. ramhist = {}
  8. swaphist = {}
  9. cpuhist = {}
  10. cur = 1
  11. -- SETTINGS
  12. nbCPU = 4
  13. FSs = {"/", "/var", "/home"}
  14. ladapter = "eth0" -- eno1
  15. wadapter = "wlan0"
  16. ntop = 10
  17. -- SETTINGS
  18. function conky_init()
  19. local cr, cs = nil
  20. if conky_window == nil then return end
  21. if cs == nil or cairo_xlib_surface_get_width(cs) ~= conky_window.width or cairo_xlib_surface_get_height(cs) ~= conky_window.height then
  22. if cs then cairo_surface_destroy(cs) end
  23. cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
  24. end
  25. if cr then cairo_destroy(cr) end
  26. cr = cairo_create(cs)
  27. local items = 30 + nbCPU + #FSs + ntop
  28. defaultSize = conky_window.height / items
  29. height = defaultSize + 1
  30. small = defaultSize * .8
  31. blue = {.4,.6,.8,.5}
  32. cur = cur % 100 + 1
  33. local ypos = 60
  34. local xpos = 25
  35. local margin = 50
  36. ypos = general(cr, xpos, ypos) + margin
  37. ypos = fs (cr, xpos, ypos) + margin
  38. ypos = ram (cr, xpos, ypos) + margin
  39. ypos = cpu (cr, xpos, ypos) + margin
  40. ypos = top (cr, xpos, ypos, ntop) + margin
  41. ypos = network(cr, xpos, ypos) + margin
  42. clock(cr, conky_window.width / 2, conky_window.height / 2 - 170)
  43. weather(cr, conky_window.width / 2 - 11.5*.6*15, conky_window.height / 2 - 60)
  44. cairo_destroy(cr)
  45. cairo_surface_destroy(cs)
  46. end
  47. function clock(cr, x, y)
  48. emboss(cr, x, y - 100, conky_parse("${time %H}"), 1, 200)
  49. emboss(cr, x, y - 50, conky_parse(":${time %M}"), 0, 150)
  50. end
  51. function weather(cr, x, y)
  52. size = 15
  53. meteo = ""
  54. yt = 0
  55. for line in io.lines("/tmp/meteo") do
  56. yt = yt + size
  57. xt = 0
  58. for substr in string.gmatch(line, "[^]+") do
  59. color = tonumber(substr:match("%[38;5;(%d+)m"))
  60. if color ~= nil then
  61. if color <= 231 then
  62. color = color - 16
  63. b = color % 36 % 6
  64. color = color - b
  65. g = color % 36 / 6
  66. color = color - g * 6
  67. r = color / 36
  68. color = {r * .2, g * .2, b * .2, 1}
  69. else
  70. a = (color - 232) / 24
  71. color = {a, a, a, 1}
  72. end
  73. end
  74. substr = substr:gsub("%[[^m]+m", "")
  75. emboss(cr, x + xt, y + yt, substr, 0, size, nil, color, "Fira Code")
  76. xt = xt + .6*size * utf8.len(substr)
  77. end
  78. end
  79. end
  80. function general(cr, x, y)
  81. cadre(cr, x, y, 450, 3 * height, 10)
  82. emboss(cr , x , y, "Kernel")
  83. y = emboss(cr, x + 450, y, conky_parse("$kernel") , 1)
  84. emboss(cr , x , y, "Uptime")
  85. y = emboss(cr, x + 450, y, conky_parse("$uptime") , 1)
  86. emboss(cr , x , y, "Load")
  87. y = emboss(cr, x + 450, y, conky_parse("$loadavg"), 1)
  88. return y
  89. end
  90. function ram(cr, x, y)
  91. cadre(cr, x, y, 450, 10 + 4 * height, 10)
  92. if #swaphist == 0 then
  93. local i
  94. for i = 1,100 do
  95. swaphist[i] =.001
  96. end
  97. end
  98. if #ramhist == 0 then
  99. local i
  100. for i = 1,100 do
  101. ramhist[i] =.001
  102. end
  103. end
  104. ramhist[cur] = tonumber(conky_parse("$memperc"))
  105. swaphist[cur] = tonumber(conky_parse("$swapperc"))
  106. emboss(cr, x , y, "Ram")
  107. emboss(cr, x + 180, y, conky_parse("$mem/") , 1)
  108. y = emboss(cr, x + 260, y, conky_parse("$memmax"), 1)
  109. bar(cr, x + 5, y, 255, conky_parse("$memperc"), blue)
  110. graph(cr, x + 280, y, 170, ramhist, blue, 100)
  111. y = y + height + 10
  112. emboss(cr, x , y, "Swap")
  113. emboss(cr, x + 180, y, conky_parse("$swap/") , 1)
  114. y = emboss(cr, x + 260, y, conky_parse("$swapmax"), 1)
  115. bar(cr, x + 5, y, 255, conky_parse("$swapperc"), blue)
  116. graph(cr, x + 280, y, 170, swaphist, blue, 100)
  117. y = y + height
  118. return y
  119. end
  120. function cpu(cr, x, y)
  121. cadre(cr, x, y, 450, 10 + (1 + nbCPU) * height, 10)
  122. if #cpuhist == 0 then
  123. local i, j
  124. for i = 1,nbCPU do
  125. cpuhist[i] = {}
  126. for j = 1,100 do
  127. cpuhist[i][j] = .001
  128. end
  129. end
  130. end
  131. emboss(cr , x , y, "Cpu")
  132. emboss(cr , x + 260, y, conky_parse("$freq_g GHz") , 1)
  133. y = emboss(cr, x + 450, y, conky_parse("Temp ${hwmon 0 temp 1}°C"), 1) + 10
  134. local cpu
  135. for cpu=1,nbCPU do
  136. bar(cr, x + 5, y, 255, conky_parse("${cpu cpu" .. cpu .. "}"), blue)
  137. cpuhist[cpu][cur] = tonumber(conky_parse("${cpu cpu" .. cpu .. "}"))
  138. graph(cr, x + 280, y, 170, cpuhist[cpu], blue, 100)
  139. y = y + height
  140. end
  141. return y
  142. end
  143. function network(cr, x, y)
  144. cadre(cr, x, y, 450, 4 * height + 10, 10)
  145. local upspd = conky_parse("${upspeedf " .. wadapter .. "}") + conky_parse("${upspeedf " .. ladapter .. "}")
  146. local downspd = conky_parse("${downspeedf " .. wadapter .. "}") + conky_parse("${downspeedf " .. ladapter .. "}")
  147. local upspdunit = "KiB/s"
  148. local downspdunit = "KiB/s"
  149. if upspd > 1024 then
  150. upspd = upspd / 1024
  151. upspdunit = "MiB/s"
  152. end
  153. if downspd > 1024 then
  154. downspd = downspd / 1024
  155. downspdunit = "MiB/s"
  156. end
  157. local ethup = string.match(conky_parse("${totalup " .. ladapter .. "}"),"[%d.]+")
  158. local ethupunit = string.match(conky_parse("${totalup " .. ladapter .. "}"),"%a")
  159. if ethupunit == "K" then
  160. ethup = ethup * 1024
  161. elseif ethupunit == "M" then
  162. ethup = ethup * 1024 * 1024
  163. elseif ethupunit == "G" then
  164. ethup = ethup * 1024 * 1024 * 1024
  165. end
  166. local ethdown = string.match(conky_parse("${totaldown " .. ladapter .. "}"),"[%d.]+")
  167. local ethdownunit = string.match(conky_parse("${totaldown " .. ladapter .. "}"),"%a")
  168. if ethdownunit == "K" then
  169. ethdown = ethdown * 1024
  170. elseif ethdownunit == "M" then
  171. ethdown = ethdown * 1024 * 1024
  172. elseif ethdownunit == "G" then
  173. ethdown = ethdown * 1024 * 1024 * 1024
  174. end
  175. local wlanup = string.match(conky_parse("${totalup " .. wadapter .. "}"),"[%d.]+")
  176. local wlanupunit = string.match(conky_parse("${totalup " .. wadapter .. "}"),"%a")
  177. if wlanupunit == "K" then
  178. wlanup = wlanup * 1024
  179. elseif wlanupunit == "M" then
  180. wlanup = wlanup * 1024 * 1024
  181. elseif wlanupunit == "G" then
  182. wlanup = wlanup * 1024 * 1024 * 1024
  183. end
  184. local wlandown = string.match(conky_parse("${totaldown " .. wadapter .. "}"),"[%d.]+")
  185. local wlandownunit = string.match(conky_parse("${totaldown " .. wadapter .. "}"),"%a")
  186. if wlandownunit == "K" then
  187. wlandown = wlandown * 1024
  188. elseif wlandownunit == "M" then
  189. wlandown = wlandown * 1024 * 1024
  190. elseif wlandownunit == "G" then
  191. wlandown = wlandown * 1024 * 1024 * 1024
  192. end
  193. local totalup = ethup + wlanup
  194. local upunit = "B"
  195. if totalup > (1024 * 1024 * 1024) then
  196. totalup = totalup / (1024 * 1024 * 1024)
  197. upunit = "GiB"
  198. elseif totalup > (1024 * 1024) then
  199. totalup = totalup / (1024 * 1024)
  200. upunit = "MiB"
  201. elseif totalup > 1024 then
  202. totalup = totalup / 1024
  203. upunit = "KiB"
  204. end
  205. local totaldown = ethdown + wlandown
  206. local downunit = "B"
  207. if totaldown > (1024 * 1024 * 1024) then
  208. totaldown = totaldown / (1024 * 1024 * 1024)
  209. downunit = "GiB"
  210. elseif totaldown > (1024 * 1024) then
  211. totaldown = totaldown / (1024 * 1024)
  212. downunit = "MiB"
  213. elseif totaldown > 1024 then
  214. totaldown = totaldown / 1024
  215. downunit = "KiB"
  216. end
  217. if #netuphist == 0 then
  218. local i
  219. for i = 1,100 do
  220. netuphist[i] = .001
  221. end
  222. end
  223. if #netdownhist == 0 then
  224. local i
  225. for i = 1,100 do
  226. netdownhist[i] = .001
  227. end
  228. end
  229. netuphist[cur] = tonumber(conky_parse("${upspeedf " .. wadapter .. "}") + conky_parse("${upspeedf " .. ladapter .. "}"))
  230. netdownhist[cur] = tonumber(conky_parse("${downspeedf " .. wadapter .. "}") + conky_parse("${downspeedf " .. ladapter .. "}"))
  231. maxspeed = max({max(netuphist), max(netdownhist)})
  232. if maxspeed == 0 then
  233. maxspeed = 1
  234. end
  235. emboss(cr, x , y, "Up")
  236. emboss(cr, x + 260, y, string.format("%.1f",upspd) .. upspdunit , 1)
  237. graph(cr, x + 280, y, 170, netuphist, blue, maxspeed)
  238. y = y + height
  239. emboss(cr, x , y, "Down")
  240. emboss(cr, x + 260, y, string.format("%.1f",downspd) .. downspdunit, 1)
  241. graph(cr, x + 280, y, 170, netdownhist, blue, maxspeed, 1)
  242. y = y + height + 10
  243. emboss(cr , x , y, "Total up")
  244. y = emboss(cr, x+260, y, string.format("%.2f",totalup) .. upunit , 1)
  245. emboss(cr , x , y, "Total down")
  246. y = emboss(cr, x+260, y, string.format("%.2f",totaldown) .. downunit , 1)
  247. return y
  248. end
  249. function fs(cr, x, y)
  250. cadre(cr, x, y, 450, height * #FSs, 10)
  251. for row=1,#FSs do
  252. emboss(cr, x , y , FSs[row])
  253. emboss(cr, x + 180, y , conky_parse("${fs_used " .. FSs[row] .. "}/"), 1)
  254. emboss(cr, x + 260, y , conky_parse("${fs_size " .. FSs[row] .. "}") , 1)
  255. bar(cr, x + 280, y, 170, conky_parse("${fs_used_perc " .. FSs[row] .. "}"), blue)
  256. y = y + height
  257. end
  258. return y
  259. end
  260. function top(cr, x, y, nrows)
  261. cadre(cr, x, y, 450, height + 10 + nrows * small, 10)
  262. emboss(cr , x , y, "Name")
  263. emboss(cr , x + 220, y, "Cpu", 1)
  264. emboss(cr , x + 230, y, "Name")
  265. y = emboss(cr, x + 450, y, "Ram", 1) + 10
  266. local row
  267. for row=1,nrows do
  268. emboss(cr , x , y, conky_parse("${top name " .. row .. "}") , 0, small)
  269. emboss(cr , x + 220, y, conky_parse("${top cpu " .. row .. "}") , 1, small)
  270. emboss(cr , x + 230, y, conky_parse("${top_mem name " .. row .. "}"), 0, small)
  271. y = emboss(cr, x + 450, y, conky_parse("${top_mem mem " .. row .. "}") , 1, small)
  272. end
  273. return y
  274. end
  275. function cadre(cr, x, y, w, h, r, pop, col)
  276. if pop == nil then pop = 1 end
  277. local pi = 3.141592
  278. if pop < 0 then y = y + pop end
  279. cairo_set_operator(cr, CAIRO_OPERATOR_XOR)
  280. cairo_move_to(cr, x, y - r)
  281. cairo_arc(cr, x + w, y , r, 1.5 * pi, 0 * pi)
  282. cairo_arc(cr, x + w, y + h, r, 0 * pi, .5 * pi)
  283. cairo_arc(cr, x , y + h, r, .5 * pi, 1 * pi)
  284. cairo_arc(cr, x , y , r, 1 * pi, 1.5 * pi)
  285. cairo_set_source_rgb(cr, 1, 1, 1)
  286. cairo_fill(cr)
  287. y = y - pop
  288. cairo_move_to(cr, x, y - r)
  289. cairo_arc(cr, x + w, y , r,1.5 * pi, 0 * pi)
  290. cairo_arc(cr, x + w, y + h, r,0 * pi, .5 * pi)
  291. cairo_arc(cr, x , y + h, r,.5 * pi, 1 * pi)
  292. cairo_arc(cr, x , y , r,1 * pi, 1.5 * pi)
  293. cairo_set_source_rgb(cr, 0, 0, 0)
  294. cairo_fill(cr)
  295. cairo_set_operator(cr, CAIRO_OPERATOR_OVER)
  296. cairo_move_to(cr, x, y - r)
  297. cairo_arc(cr, x + w, y , r, 1.5 * pi, 0 * pi)
  298. cairo_arc(cr, x + w, y + h, r, 0 * pi, .5 * pi)
  299. cairo_arc(cr, x , y + h, r, .5 * pi, 1 * pi)
  300. cairo_arc(cr, x , y , r, 1 * pi, 1.5 * pi)
  301. if pop > 0 then cairo_set_source_rgba(cr, 0, 0, 0, .1) else cairo_set_source_rgba(cr, 1, 1, 1, .1) end
  302. cairo_fill_preserve(cr)
  303. if col ~= nil then cairo_set_source_rgba(cr, col[1], col[2], col[3], col[4]) else cairo_set_source_rgba(cr, 0, 0, 0, 0)end
  304. cairo_fill(cr)
  305. end
  306. function bar(cr, x, y, width, percent, color)
  307. local barh = height / 3
  308. emboss(cr, x + width, y + 3, percent .. "%", 1, small)
  309. width = width - 45
  310. cadre(cr, x, y + barh + 4, width , barh - 4, 4)
  311. cadre(cr, x, y + barh + 4, width * percent / 100, barh - 4, 2, -1, color)
  312. end
  313. function emboss(cr, x, y, text, right, size, pop, col, font)
  314. if pop == nil then pop = 1 end
  315. if size == nil then size = defaultSize end
  316. if right == nil then right = 0 end
  317. if font == nil then font = "Impact" end
  318. y = y + size
  319. cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
  320. cairo_set_font_size (cr, size);
  321. if right == 1 then
  322. extent = cairo_text_extents_t:create()
  323. tolua.takeownership(extent)
  324. cairo_text_extents(cr, text, extent)
  325. x = x - extent.x_advance
  326. -- cairo_text_extents_t:destroy(extent)
  327. end
  328. if pop < 0 then y = y + pop end
  329. cairo_set_operator(cr, CAIRO_OPERATOR_XOR)
  330. cairo_move_to(cr, x, y)
  331. cairo_text_path(cr, text)
  332. cairo_set_source_rgb(cr, 1, 1, 1)
  333. cairo_fill(cr)
  334. cairo_move_to(cr,x,y - pop)
  335. cairo_text_path(cr, text)
  336. cairo_set_source_rgb(cr, 0, 0, 0)
  337. cairo_fill(cr)
  338. cairo_set_operator(cr, CAIRO_OPERATOR_OVER)
  339. cairo_move_to(cr, x, y)
  340. cairo_text_path(cr, text)
  341. if pop > 0 then cairo_set_source_rgba(cr, 0, 0, 0, .1) else cairo_set_source_rgba(cr, 1, 1, 1, .1) end
  342. cairo_fill_preserve(cr)
  343. if col ~= nil then cairo_set_source_rgba(cr, col[1], col[2], col[3], col[4]) else cairo_set_source_rgba(cr, 0, 0, 0, 0) end
  344. cairo_fill(cr)
  345. return y
  346. end
  347. function graph(cr, x, y, width, hist, color, maximum, reverse)
  348. if maximum == nil then maximum = max(hist) end
  349. if reverse == nil then reverse = 0 end
  350. x = x - 2
  351. width = width + 2
  352. local scalev = (height - 2) / maximum
  353. if reverse == 1 then
  354. scalev = -scalev
  355. y = y + 1
  356. else
  357. y = y + height - 1
  358. end
  359. local scaleh = width / 100
  360. cairo_move_to(cr, x + scaleh, y)
  361. local i
  362. for i = cur+1,100 do
  363. cairo_line_to(cr, x + (i - cur) * scaleh, y - hist[i] * scalev)
  364. end
  365. for i = 1,cur do
  366. cairo_line_to(cr, x + ((100 - cur) + i) * scaleh, y - hist[i] * scalev)
  367. end
  368. cairo_line_to(cr, x + 100 * scaleh, y)
  369. cairo_close_path(cr)
  370. cairo_set_source_rgb(cr, 1, 1, 1)
  371. cairo_fill(cr)
  372. y = y - 2
  373. cairo_move_to(cr, x + scaleh, y)
  374. for i = cur+1,100 do
  375. cairo_line_to(cr, x + (i - cur) * scaleh, y - hist[i] * scalev)
  376. end
  377. for i = 1,cur do
  378. cairo_line_to(cr, x + ((100 - cur) + i) * scaleh, y - hist[i] * scalev)
  379. end
  380. cairo_line_to(cr, x + 100 * scaleh, y)
  381. cairo_close_path(cr)
  382. cairo_set_source_rgb(cr, 0, 0, 0)
  383. cairo_fill(cr)
  384. y = y + 1
  385. cairo_move_to(cr, x + scaleh, y)
  386. for i = cur+1,100 do
  387. cairo_line_to(cr, x + (i - cur) * scaleh, y - hist[i] * scalev)
  388. end
  389. for i = 1,cur do
  390. cairo_line_to(cr, x + ((100 - cur) + i) * scaleh, y - hist[i] * scalev)
  391. end
  392. cairo_line_to(cr, x + 100 * scaleh, y)
  393. cairo_close_path(cr)
  394. cairo_set_source_rgba(cr, color[1], color[2], color[3], 1)
  395. cairo_fill(cr)
  396. end
  397. function max(hist)
  398. local i
  399. local max = 0
  400. for i=1,#hist do
  401. if hist[i] > max then max = hist[i] end
  402. end
  403. return max
  404. end