A beautifully embossed conky
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.

447 lines
12KB

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