From d9bf9830b0d72837000e8a5dbf05536881eab42b Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 31 Jan 2015 04:16:11 +0100 Subject: [PATCH] Better number formating Is it 120000 or 1200000? --- technic/helpers.lua | 49 +++++++++++++++++++++++ technic/machines/LV/solar_panel.lua | 2 +- technic/machines/MV/wind_mill.lua | 2 +- technic/machines/register/battery_box.lua | 2 +- technic/machines/register/solar_array.lua | 2 +- technic/machines/switching_station.lua | 2 +- 6 files changed, 54 insertions(+), 5 deletions(-) diff --git a/technic/helpers.lua b/technic/helpers.lua index d4306cf..0c085e0 100644 --- a/technic/helpers.lua +++ b/technic/helpers.lua @@ -1,3 +1,16 @@ +--load config +local sepchar, baresepchar = nil, nil +do + local sepcode = technic.config:get("thousand_separator") + --default is SI style + sepchar = sepcode and string.char(sepcode) or " " + baresepchar = sepchar + --handling if sepchar is magic... + for magic in string.gmatch("().%+-*?[^$", ".") do + if sepchar == magic then sepchar = "%"..sepchar end + end +end + -- Only changes name, keeps other params function technic.swap_node(pos, name) local node = minetest.get_node(pos) @@ -44,3 +57,39 @@ function technic.get_or_load_node(pos) local MinEdge, MaxEdge = vm:read_from_map(pos, pos) return nil end + +function technic.format(str, ...) + local arg={...} + local param = nil + local percent = false + local res = "" + local i = 1 + for c in str:gmatch"." do + if percent then + assert(c ~= "%") --syntax error + if c == "e" then + -- use enhanced number formatting + -- only works for unsigned numbers + local numstr = tostring(math.abs(arg[i])) + local a, b, body, frac = numstr:find("^(%d+)([.]?.-)$") + a = 1 + body = body..baresepchar + while a ~= 0 do + body, a = body:gsub("(%d)(%d%d%d)"..sepchar, "%1"..sepchar.."%2"..sepchar, 1) + end + body = body:gsub(sepchar.."$", "") + res = res .. body .. frac + else + --use traditional string:format + res = res .. (string.format(("%"..c), arg[i])) + i = i + 1 + end + percent = false + elseif c == "%" then + percent = true + else + res = res .. c + end + end + return res +end \ No newline at end of file diff --git a/technic/machines/LV/solar_panel.lua b/technic/machines/LV/solar_panel.lua index 959de52..4e0eaaa 100644 --- a/technic/machines/LV/solar_panel.lua +++ b/technic/machines/LV/solar_panel.lua @@ -24,7 +24,7 @@ local run = function(pos, node) local charge_to_give = math.floor((light + pos1.y) * 3) charge_to_give = math.max(charge_to_give, 0) charge_to_give = math.min(charge_to_give, 200) - meta:set_string("infotext", S("%s Active"):format(machine_name).." ("..charge_to_give.."EU)") + meta:set_string("infotext", technic.format(S("%s Active (%e EU)"), machine_name, charge_to_give)) meta:set_int("LV_EU_supply", charge_to_give) else meta:set_string("infotext", S("%s Idle"):format(machine_name)) diff --git a/technic/machines/MV/wind_mill.lua b/technic/machines/MV/wind_mill.lua index 4d63ddc..0e3a1a0 100644 --- a/technic/machines/MV/wind_mill.lua +++ b/technic/machines/MV/wind_mill.lua @@ -55,7 +55,7 @@ local run = function(pos, node) meta:set_int("MV_EU_supply", power) end - meta:set_string("infotext", machine_name.." ("..power.."EU)") + meta:set_string("infotext", technic.format("%s (%eEU)", machine_name, power)) end minetest.register_node("technic:wind_mill", { diff --git a/technic/machines/register/battery_box.lua b/technic/machines/register/battery_box.lua index 669a06b..b09278e 100644 --- a/technic/machines/register/battery_box.lua +++ b/technic/machines/register/battery_box.lua @@ -142,7 +142,7 @@ function technic.register_battery_box(data) .."^[lowpart:"..charge_percent ..":technic_power_meter_fg.png]") - local infotext = S("%s Battery Box: %d/%d"):format(tier, + local infotext = technic.format(S("%s Battery Box: %e/%e"), tier, current_charge, max_charge) if eu_input == 0 then infotext = S("%s Idle"):format(infotext) diff --git a/technic/machines/register/solar_array.lua b/technic/machines/register/solar_array.lua index 615219b..9660b47 100644 --- a/technic/machines/register/solar_array.lua +++ b/technic/machines/register/solar_array.lua @@ -28,7 +28,7 @@ function technic.register_solar_array(data) local charge_to_give = math.floor((light + pos.y) * data.power) charge_to_give = math.max(charge_to_give, 0) charge_to_give = math.min(charge_to_give, data.power * 50) - meta:set_string("infotext", S("%s Active"):format(machine_name).." ("..charge_to_give.."EU)") + meta:set_string("infotext", technic.format(S("%s Active (%e EU)"), machine_name, charge_to_give)) meta:set_int(tier.."_EU_supply", charge_to_give) else meta:set_string("infotext", S("%s Idle"):format(machine_name)) diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua index 737970d..46a911d 100644 --- a/technic/machines/switching_station.lua +++ b/technic/machines/switching_station.lua @@ -291,7 +291,7 @@ minetest.register_abm({ --dprint("Total BA demand:"..BA_eu_demand) meta:set_string("infotext", - S("%s. Supply: %d Demand: %d"):format( + technic.format(S("%s. Supply: %e Demand: %e"), machine_name, PR_eu_supply, RE_eu_demand)) -- If the PR supply is enough for the RE demand supply them all