fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a7d6bcc
GUI
1function table . merge ( a , b ) 2for k , v in pairs ( b ) do 3if not a [ k ] then a [ k ] =v end 4end 5return a 6end 7 8function table . clone ( a ) 9local ret ={} 10for k , v in pairs ( a ) do 11ret [ k ] =type ( v ) == "table" and table . clone ( v ) or v 12end 13return ret 14end 15 16local function reversedipairsiter ( t , i ) 17i =i - 1 18if i ~= 0 then 19return i , t [ i ] 20end 21end 22 23function reversedipairs ( t ) 24return reversedipairsiter , t , # t + 1 25end 26 27function table . hasValue ( tbl , val ) 28for key , value in pairs ( tbl ) do 29if ( value == val ) then return true , key end 30end 31return false 32end 33 34do 35local function val_to_str ( v , stack , scope ) 36if type ( v ) == "string" then 37v =string . gsub ( v , "\n" , "\\n" ) 38if string . match ( string . gsub ( v , "[^'\"]" , "" ), '^"+$' ) then 39return "'" .. v .. "'" 40end 41return '"' .. string . gsub ( v , '"' , '\\"' ) .. '"' 42else 43return type ( v ) == "table" and table . tostring ( v , stack , scope ) or tostring ( v ) 44end 45end 46 47local function key_to_str ( k , stack , scope ) 48if type ( k ) == "string" and string . match ( k , "^[_%a][_%a%d]*$" ) then 49return k 50else 51return "[" .. val_to_str ( k , stack , scope ) .. "]" 52end 53end 54 55function table . tostring ( tbl , stack , scope ) 56stack =stack or {} 57scope =scope or 0 58 59if stack [ tbl ] then return error ( "circular reference" ) end 60 61stack [ tbl ] =true 62scope =scope + 1 63 64local result ="{\n" 65 66for k , v in pairs ( tbl ) do 67local tabs =string . rep ( "\t" , scope ) 68if type ( v ) == "table" then 69result =result .. tabs .. key_to_str ( k , stack , scope ) .. " = " .. table . tostring ( v , stack , scope ) .. "\n" 70else 71result =result .. tabs .. key_to_str ( k , stack , scope ) .. " = " .. val_to_str ( v , stack , scope ) .. "\n" 72end 73end 74 75scope =scope - 1 76stack [ tbl ] =nil 77 78return result .. string . rep ( "\t" , scope ) .. "}" 79end 80end 81 82--[[local test = { 831, 2, 5, 84["Kappa 1 2 3 \n lul"] = true, 85data = { 861337.84239, 87test = { 887, 8, "NINE" 89} 90} 91} 92 93print(table.tostring(love))]] 94 95function table . copy ( object ) 96local lookup_table ={} 97local function _copy ( object ) 98if type ( object ) ~= "table" then 99return object 100elseif lookup_table [ object ] then 101return lookup_table [ object ] 102end 103local new_table ={} 104lookup_table [ object ] =new_table 105for index , value in pairs ( object ) do 106new_table [ _copy ( index )] =_copy ( value ) 107end 108return setmetatable ( new_table , getmetatable ( object )) 109end 110return _copy ( object ) 111end 112 113function table . concatNumberedList ( tbl , sep , oxford ) 114if type ( tbl ) ~= "table" then 115return tostring ( tbl ) 116end 117 118local str ="" 119local num =# tbl 120for i =1 , num do 121if i < num then 122-- If we aren't at the end of the list, use commas 123-- If we're one before the end of the list, use an oxford comma if we want it 124str =str .. i .. ". " .. tbl [ i ] .. (( oxford or i < num - 1 ) and ", " or " " ) 125elseif i > 1 then 126-- If we're the last item and we have more than 1 item already.. 127-- Seperate using our defined seperator word 128str =str .. ( sep or "and" ) .. " " .. i .. ". " .. tbl [ i ] 129else 130-- Single item.. 131str =i .. ". " .. tbl [ i ] 132end 133end 134return str 135end 136 137function table . concatList ( tbl , sep , oxford ) 138if type ( tbl ) ~= "table" then 139return tostring ( tbl ) 140end 141 142local str ="" 143local num =# tbl 144for i =1 , num do 145if i < num then 146-- If we aren't at the end of the list, use commas 147-- If we're one before the end of the list, use an oxford comma if we want it 148str =str .. tbl [ i ] .. (( oxford or i < num - 1 ) and ", " or " " ) 149elseif i > 1 then 150-- If we're the last item and we have more than 1 item already.. 151-- Seperate using our defined seperator word 152str =str .. ( sep or "and" ) .. " " .. tbl [ i ] 153else 154-- Single item.. 155-- Don't even bother concating the string 156str =tbl [ i ] 157end 158end 159return str 160end 161 162function table . max ( tbl ) 163local max_key =nil 164local max_value =- math . huge 165for key , value in pairs ( tbl ) do 166value =assert ( tonumber ( value ), "expected a table full of numbers" ) 167if value > max_value then 168max_key =key 169max_value =value 170end 171end 172return max_key , max_value 173end 174 175function table . min ( tbl ) 176local min_key =nil 177local min_value =math . huge 178for key , value in pairs ( tbl ) do 179value =assert ( tonumber ( value ), "expected a table full of numbers" ) 180if value < min_value then 181min_key =key 182min_value =value 183end 184end 185return min_key , min_value 186end 187 188function table . avg ( tbl ) 189local num =0 190local total =0 191for key , value in pairs ( tbl ) do 192value =assert ( tonumber ( value ), "expected a table full of numbers" ) 193total =total + value 194num =num + 1 195end 196return total / num 197end 198 199function table . sum ( tbl ) 200local total =0 201for key , value in pairs ( tbl ) do 202value =assert ( tonumber ( value ), "expected a table full of numbers" ) 203total =total + value 204end 205return total 206end 207 208function table . random ( tbl ) 209return tbl [ math . random ( 1 , # tbl )] 210end 211 212function table . randomkeyvalue ( tbl ) 213local keys ={} 214for k , v in pairs ( tbl ) do 215table . insert ( keys , k ) 216end 217local key =keys [ math . random ( 1 , # keys )] 218return key , tbl [ key ] 219end