3dca925 - Fix up libraries
[wowui.git] / UpdateStatusTextHook.lua
1 local UnitIsConnected = UnitIsConnected;
2 local UnitIsDeadOrGhost = UnitIsDeadOrGhost;
3 local UnitHealthMax = UnitHealthMax;
4 local UnitHealth = UnitHealth;
5 local format = string.format
6
7 hooksecurefunc("CompactUnitFrame_UpdateStatusText", function(frame)
8     if frame and not frame:IsForbidden() and frame:GetName():match("^CompactRaidFrame%d") then
9         -- conditions taken from CompactUnitFrame.lua
10         if not UnitIsConnected(frame.unit) or UnitIsDeadOrGhost(frame.displayedUnit) then
11             return;
12         elseif (frame.optionTable.healthText == "losthealth") then
13             -- only losthealth option is condensed
14             local healthLost = UnitHealthMax(frame.displayedUnit) - UnitHealth(frame.displayedUnit);
15             if healthLost <= 0 then return end
16
17             local prettyHealth;
18             if healthLost > 1200000000 then -- 1.2B
19                 local h1b = healthLost / 1000000000;
20                 local h100m = (healthLost % 1000000000) / 100000000;
21                 prettyHealth = format("-%d.%dB", h1b, h100m);
22             elseif healthLost > 1200000 then -- 1.2M
23                 prettyHealth = format("-%dM", healthLost / 1000000);
24             elseif healthLost > 1000 then -- 1K
25                 prettyHealth = format("-%dK", healthLost / 1000);
26             else
27                 prettyHealth = format("-%d", healthLost)
28             end
29
30             frame.statusText:SetText(prettyHealth);
31         end
32     end
33 end);