/Tukui/modules/datatext/system.lua

http://github.com/Asphyxia/Tukui · Lua · 114 lines · 90 code · 15 blank · 9 comment · 18 complexity · 34639b99fee17f4e144a86ce32e24bcc MD5 · raw file

  1. --------------------------------------------------------------------
  2. -- System Stats
  3. --------------------------------------------------------------------
  4. local T, C, L = unpack(select(2, ...)) -- Import Functions/Constants, Config, Locales
  5. if not C["datatext"].system or C["datatext"].system == 0 then return end
  6. local Stat = CreateFrame("Frame")
  7. Stat:RegisterEvent("PLAYER_ENTERING_WORLD")
  8. Stat:SetFrameStrata("HIGH")
  9. Stat:SetFrameLevel(3)
  10. Stat:EnableMouse(true)
  11. Stat.tooltip = false
  12. local Text = TukuiInfoLeft:CreateFontString(nil, "OVERLAY")
  13. Text:SetFont(C.media.pixelfont, C["datatext"].fontsize, "MONOCHROMEOUTLINE")
  14. T.PP(C["datatext"].system, Text)
  15. local bandwidthString = "%.2f Mbps"
  16. local percentageString = "%.2f%%"
  17. local kiloByteString = "%d"..T.datacolor.." kb"
  18. local megaByteString = "%.2f"..T.datacolor.." mb"
  19. local function formatMem(memory)
  20. local mult = 10^1
  21. if memory > 999 then
  22. local mem = ((memory/1024) * mult) / mult
  23. return string.format(megaByteString, mem)
  24. else
  25. local mem = (memory * mult) / mult
  26. return string.format(kiloByteString, mem)
  27. end
  28. end
  29. local memoryTable = {}
  30. local function RebuildAddonList(self)
  31. local addOnCount = GetNumAddOns()
  32. if (addOnCount == #memoryTable) or self.tooltip == true then return end
  33. -- Number of loaded addons changed, create new memoryTable for all addons
  34. memoryTable = {}
  35. for i = 1, addOnCount do
  36. memoryTable[i] = { i, select(2, GetAddOnInfo(i)), 0, IsAddOnLoaded(i) }
  37. end
  38. self:SetAllPoints(Text)
  39. end
  40. local function UpdateMemory()
  41. -- Update the memory usages of the addons
  42. UpdateAddOnMemoryUsage()
  43. -- Load memory usage in table
  44. local addOnMem = 0
  45. local totalMemory = 0
  46. for i = 1, #memoryTable do
  47. addOnMem = GetAddOnMemoryUsage(memoryTable[i][1])
  48. memoryTable[i][3] = addOnMem
  49. totalMemory = totalMemory + addOnMem
  50. end
  51. -- Sort the table to put the largest addon on top
  52. table.sort(memoryTable, function(a, b)
  53. if a and b then
  54. return a[3] > b[3]
  55. end
  56. end)
  57. return totalMemory
  58. end
  59. local int = 10
  60. local function Update(self, t)
  61. int = int - t
  62. if int < 0 then
  63. RebuildAddonList(self)
  64. local total = UpdateMemory()
  65. Text:SetText(formatMem(total))
  66. int = 10
  67. end
  68. end
  69. Stat:SetScript("OnMouseDown", function () collectgarbage("collect") Update(Stat, 10) end)
  70. Stat:SetScript("OnEnter", function(self)
  71. if not InCombatLockdown() then
  72. self.tooltip = true
  73. local bandwidth = GetAvailableBandwidth()
  74. local anchor, panel, xoff, yoff = T.DataTextTooltipAnchor(Text)
  75. GameTooltip:SetOwner(panel, anchor, xoff, yoff)
  76. GameTooltip:ClearLines()
  77. if bandwidth ~= 0 then
  78. -- GetAvailableBandwidth() func is bugged as always, commenting for now
  79. -- GameTooltip:AddDoubleLine(L.datatext_bandwidth , string.format(bandwidthString, bandwidth),0.69, 0.31, 0.31,0.84, 0.75, 0.65)
  80. GameTooltip:AddDoubleLine(L.datatext_download , string.format(percentageString, GetDownloadedPercentage() *100),0.69, 0.31, 0.31, 0.84, 0.75, 0.65)
  81. GameTooltip:AddLine(" ")
  82. end
  83. local totalMemory = UpdateMemory()
  84. GameTooltip:AddDoubleLine(L.datatext_totalmemusage, formatMem(totalMemory), 0.69, 0.31, 0.31,0.84, 0.75, 0.65)
  85. GameTooltip:AddLine(" ")
  86. for i = 1, #memoryTable do
  87. if (memoryTable[i][4]) then
  88. local red = memoryTable[i][3] / totalMemory
  89. local green = 1 - red
  90. GameTooltip:AddDoubleLine(memoryTable[i][2], formatMem(memoryTable[i][3]), 1, 1, 1, red, green + .5, 0)
  91. end
  92. end
  93. GameTooltip:Show()
  94. end
  95. end)
  96. Stat:SetScript("OnLeave", function(self) self.tooltip = false GameTooltip:Hide() end)
  97. Stat:SetScript("OnUpdate", Update)
  98. Stat:SetScript("OnEvent", function(self, event) collectgarbage("collect") end)
  99. Update(Stat, 10)