fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
d16d89b
GUI
1local floor =math . floor 2local random =math . random 3 4local byte =string . byte 5local char =string . char 6local find =string . find 7local format =string . format 8local gmatch =string . gmatch 9local gsub =string . gsub 10local match =string . match 11local reverse =string . reverse 12local sub =string . sub 13local upper =string . upper 14 15local concat =table . concat 16local insert =table . insert 17 18do 19-- With special chars: 33-126 20-- without: 48-57, 65-90, 97-122 (this is going to be a bit messy without continue) 21local num =0 --recycling these up here since they get called a lot 22local function numroll () 23num =random ( 48 , 122 ) 24if ( num > 57 and num < 65 ) or ( num > 90 and num < 97 ) then 25return numroll () 26else 27return num 28end 29end 30 31local tab ={} 32function string . random ( len , specialchars ) 33tab ={} 34if specialchars then 35for i =1 , len do 36insert ( tab , char ( random ( 33 , 126 ))) 37end 38return concat ( tab ) 39else 40for i =1 , len do 41insert ( tab , char ( numroll ())) 42end 43return concat ( tab ) 44end 45end 46end 47 48function string . toTable ( str ) 49local tbl ={} 50 51for i =1 , # str do 52tbl [ i ] =sub ( str , i , i ) 53end 54 55return tbl 56end 57 58function string . explode ( str , sep , pat ) 59if sep == "" then return string . toTable ( str ) end 60 61local ret ={} 62local i , pos =1 , 1 63 64if not pat then sep =gsub ( sep , "[%-%^%$%(%)%%%.%[%]%*%+%-%?]" , "%%%1" ) end 65 66for startpos , endpos in gmatch ( str , "()" .. sep .. "()" ) do 67ret [ i ] =sub ( str , pos , startpos - 1 ) 68i =i + 1 69pos =endpos 70end 71 72ret [ i ] =sub ( str , pos ) 73return ret 74end 75 76-- remove trailing and leading whitespace from string. 77function string . trim ( s ) 78return gsub ( s , "^%s*(.-)%s*$" , "%1" ) 79end 80 81local trim =string . trim 82 83-- remove leading whitespace from string. 84function string . ltrim ( s ) 85return gsub ( s , "^%s*" , "" ) 86end 87 88-- remove trailing whitespace from string. 89function string . rtrim ( s ) 90local n =# s 91while n > 0 and find ( s , "^%s" , n ) do n =n - 1 end 92return sub ( s , 1 , n ) 93end 94 95function string . parseArgs ( line ) 96local cmd , val =match ( line , "(%S-)%s-=%s+(.+)" ) 97if cmd and val then 98return { trim ( cmd ), trim ( val ) } 99end 100local quote =sub ( line , 1 , 1 ) ~= '"' 101local ret ={} 102for chunk in gmatch ( line , '[^"]+' ) do 103quote =not quote 104if quote then 105insert ( ret , chunk ) 106else 107for chunk in gmatch ( chunk , "%S+" ) do -- changed %w to %S to allow all characters except space 108insert ( ret , chunk ) 109end 110end 111end 112return ret 113end 114 115local pattern_escape_replacements ={ 116[ "(" ] ="%(" , 117[ ")" ] ="%)" , 118[ "." ] ="%." , 119[ "%" ] ="%%" , 120[ "+" ] ="%+" , 121[ "-" ] ="%-" , 122[ "*" ] ="%*" , 123[ "?" ] ="%?" , 124[ "[" ] ="%[" , 125[ "]" ] ="%]" , 126[ "^" ] ="%^" , 127[ "$" ] ="%$" , 128[ "\0" ] ="%z" 129} 130 131function string . escape ( str ) 132return gsub ( str , "." , pattern_escape_replacements ) 133end 134 135function string . toSize ( size ) 136size =tonumber ( size ) or 0 137 138if size <= 0 then return "0" end 139if size < 1024 then return format ( "%.2f Bytes" , size ) end 140if size < 1024 * 1024 then return format ( "%.2f KB" , size / 1024 ) end 141if size < 1024 * 1024 * 1024 then return format ( "%.2f MB" , size / ( 1024 * 1024 )) end 142 143return format ( "%.2f GB" , size / ( 1024 * 1024 * 1024 )) 144end 145 146function string . upperFirst ( str ) 147return gsub ( str , "^%l" , upper ) 148end 149 150function string . addCommas ( str ) 151str =tostring ( str ) 152return reverse ( gsub ( gsub ( reverse ( str ), "(...)" , "%1," ), ",$" , "" )) 153end 154 155function string . niceNumber ( num ) 156return string . addCommas ( format ( "%0.0f" , num )) 157end 158 159function string . singularOrMuliple ( str , num ) 160local fmt =format ( "%i %s" , num , str ) 161return num == 1 and fmt or ( fmt .. "s" ) 162end 163 164function string . AOrAn ( s ) 165return match ( s , "^h?[AaEeIiOoUu]" ) and "an" or "a" 166end 167 168function string . secondsToHuman ( sec , accuracy ) 169 170local accuracy =accuracy or 2 171 172local years =sec / 31536000 173local yearsRemainder =sec % 31536000 174local days =yearsRemainder / 86400 175local daysRemainder =sec % 86400 176local hours =daysRemainder / 3600 177local hourRemainder =( sec - 86400 ) % 3600 178local min =hourRemainder / 60 179local sec =sec % 60 180 181years =floor ( years ) 182days =floor ( days ) 183hours =floor ( hours ) 184min =floor ( min ) 185sec =floor ( sec ) 186 187local results ={} 188 189if years >= 1 then 190insert ( results , string . singularOrMuliple ( "year" , years )) 191end 192if days >= 1 then 193insert ( results , string . singularOrMuliple ( "day" , days )) 194end 195if hours >= 1 then 196insert ( results , string . singularOrMuliple ( "hour" , hours )) 197end 198if min >= 1 then 199insert ( results , string . singularOrMuliple ( "minute" , min )) 200end 201if sec >= 1 then 202insert ( results , string . singularOrMuliple ( "second" , sec )) 203end 204 205local result ={} 206for i =1 , accuracy do 207result [ i ] =results [ i ] 208end 209 210return concat ( result , ", " ) 211end 212 213function string . escapeURL ( url ) 214return gsub ( url , "([^A-Za-z0-9_])" , function ( c ) 215return format ( "%%%02x" , byte ( c )) 216end ) 217end 218 219function string . enescapeURL ( url ) 220return gsub ( url , "%%(%x%x)" , function ( hex ) 221return char ( tonumber ( hex , 16 )) 222end ) 223end