PageRenderTime 80ms CodeModel.GetById 68ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/databars/memory.lua

http://github.com/Asphyxia/Tukui
Lua | 123 lines | 101 code | 18 blank | 4 comment | 14 complexity | 2ff54f6fd97290bf336010a000869661 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2
  3local ADDON_NAME, ns = ...
  4local oUF = ns.oUF or oUF
  5
  6ns._Objects = {}
  7ns._Headers = {}
  8
  9if not C["databars"].memory or C["databars"].memory == 0 then return end
 10local barNum = C["databars"].memory
 11local barTex = C.media.normTex
 12
 13T.databars[barNum]:Show()
 14
 15local Stat = CreateFrame("Frame", nil, T.databars[barNum])
 16Stat:EnableMouse(true)
 17Stat:SetFrameStrata("BACKGROUND")
 18Stat:SetFrameLevel(4)
 19Stat.tooltip = false
 20Stat:ClearAllPoints()
 21Stat:SetAllPoints(T.databars[barNum])
 22
 23local StatusBar = T.databars[barNum].statusbar
 24local Text = T.databars[barNum].text
 25
 26local bandwidthString = "%.2f Mbps"
 27local percentageString = "%.2f%%"
 28
 29local kiloByteString = "%d"..T.datacolor.." kb"
 30local megaByteString = "%.2f"..T.datacolor.." mb"
 31
 32local function formatMem(memory)
 33	local mult = 10^1
 34	if memory > 999 then
 35		local mem = ((memory/1024) * mult) / mult
 36		return string.format(megaByteString, mem)
 37	else
 38		local mem = (memory * mult) / mult
 39		return string.format(kiloByteString, mem)
 40	end
 41end
 42
 43local memoryTable = {}
 44
 45local function RebuildAddonList(self)
 46	local addOnCount = GetNumAddOns()
 47	if (addOnCount == #memoryTable) or self.tooltip == true then return end
 48
 49	-- Number of loaded addons changed, create new memoryTable for all addons
 50	memoryTable = {}
 51	for i = 1, addOnCount do
 52		memoryTable[i] = { i, select(2, GetAddOnInfo(i)), 0, IsAddOnLoaded(i) }
 53	end
 54	self:SetAllPoints(T.databars[barNum])
 55end
 56
 57local function UpdateMemory()
 58	-- Update the memory usages of the addons
 59	UpdateAddOnMemoryUsage()
 60	-- Load memory usage in table
 61	local addOnMem = 0
 62	local totalMemory = 0
 63	for i = 1, #memoryTable do
 64		addOnMem = GetAddOnMemoryUsage(memoryTable[i][1])
 65		memoryTable[i][3] = addOnMem
 66		totalMemory = totalMemory + addOnMem
 67	end
 68	-- Sort the table to put the largest addon on top
 69	table.sort(memoryTable, function(a, b)
 70		if a and b then
 71			return a[3] > b[3]
 72		end
 73	end)
 74	
 75	return totalMemory
 76end
 77
 78local int = 10
 79
 80local function Update(self, t)
 81	int = int - t
 82	
 83	if int < 0 then
 84		RebuildAddonList(self)
 85		local total = UpdateMemory()
 86		Text:SetText(formatMem(total))
 87		StatusBar:SetMinMaxValues(0,10000)
 88		StatusBar:SetValue(total)
 89		StatusBar:SetStatusBarColor( 41/255,  79/255, 155/255 )
 90		int = 10
 91	end
 92end
 93
 94Stat:SetScript("OnMouseDown", function () collectgarbage("collect") Update(Stat, 10) end)
 95Stat:SetScript("OnEnter", function(self)
 96	if not InCombatLockdown() then
 97		self.tooltip = true
 98		local bandwidth = GetAvailableBandwidth()
 99		local xoff, yoff = T.DataBarTooltipAnchor(barNum)
100		GameTooltip:SetOwner(T.databars[barNum], "ANCHOR_BOTTOMRIGHT", xoff, yoff)
101		GameTooltip:ClearLines()
102		if bandwidth ~= 0 then
103			GameTooltip:AddDoubleLine(L.datatext_bandwidth , string.format(bandwidthString, bandwidth),0.69, 0.31, 0.31,0.84, 0.75, 0.65)
104			GameTooltip:AddDoubleLine(L.datatext_download , string.format(percentageString, GetDownloadedPercentage() *100),0.69, 0.31, 0.31, 0.84, 0.75, 0.65)
105			GameTooltip:AddLine(" ")
106		end
107		local totalMemory = UpdateMemory()
108		GameTooltip:AddDoubleLine(L.datatext_totalmemusage, formatMem(totalMemory), 0.69, 0.31, 0.31,0.84, 0.75, 0.65)
109		GameTooltip:AddLine(" ")
110		for i = 1, #memoryTable do
111			if (memoryTable[i][4]) then
112				local red = memoryTable[i][3] / totalMemory
113				local green = 1 - red
114				GameTooltip:AddDoubleLine(memoryTable[i][2], formatMem(memoryTable[i][3]), 1, 1, 1, red, green + .5, 0)
115			end						
116		end
117		GameTooltip:Show()
118	end
119end)
120Stat:SetScript("OnLeave", function(self) self.tooltip = false GameTooltip:Hide() end)
121Stat:SetScript("OnUpdate", Update)
122Stat:SetScript("OnEvent", function(self, event) collectgarbage("collect") end)
123Update(Stat, 10)