utils.lua (4139B)
1 local lfs = require( "lfs" ) 2 3 getmetatable( "" ).__mod = function( self, form ) 4 if type( form ) == "table" then 5 return self:format( table.unpack( form ) ) 6 end 7 8 return self:format( form ) 9 end 10 11 function string.plural( count, plur, sing ) 12 return count == 1 and ( sing or "" ) or ( plur or "s" ) 13 end 14 15 function string.startsWith( self, needle, ignoreCase ) 16 if ignoreCase then 17 return self:lower():sub( 1, needle:len() ) == needle:lower() 18 end 19 20 return self:sub( 1, needle:len() ) == needle 21 end 22 23 function string.commas( num ) 24 num = tonumber( num ) 25 26 local out = "" 27 28 while num >= 1000 do 29 out = ( ",%03d%s" ):format( num % 1000, out ) 30 31 num = math.floor( num / 1000 ) 32 end 33 34 return tostring( num ) .. out 35 end 36 37 function math.round( num ) 38 return math.floor( num + 0.5 ) 39 end 40 41 function math.avg( a, b ) 42 return ( a + b ) / 2 43 end 44 45 function io.readable( path ) 46 local attr, err = lfs.attributes( path ) 47 if not attr then 48 return false, err 49 end 50 51 if attr.mode == "directory" then 52 return true 53 end 54 55 local file, err = io.open( path, "r" ) 56 if not file then 57 return false, err 58 end 59 60 io.close( file ) 61 62 return true 63 end 64 65 function enforce( var, name, ... ) 66 local acceptable = { ... } 67 local ok = false 68 69 for _, accept in ipairs( acceptable ) do 70 if type( var ) == accept then 71 ok = true 72 73 break 74 end 75 end 76 77 if not ok then 78 error( "argument `%s' to %s should be of type %s (got %s)" % { name, debug.getinfo( 2, "n" ).name, table.concat( acceptable, " or " ), type( var ) }, 3 ) 79 end 80 end 81 82 local function genericPrint( message, main, chat ) 83 local bold = false 84 local fg = 7 85 local bg = 0 86 87 local function setFG( colour ) 88 return function() 89 fg = colour 90 end 91 end 92 93 local Sequences = { 94 d = setFG( 7 ), 95 k = setFG( 0 ), 96 r = setFG( 1 ), 97 g = setFG( 2 ), 98 y = setFG( 3 ), 99 b = setFG( 4 ), 100 m = setFG( 5 ), 101 c = setFG( 6 ), 102 w = setFG( 7 ), 103 s = setFG( 8 ), 104 } 105 106 for line, newLine in message:gmatch( "([^\n]*)(\n?)" ) do 107 for text, hashPos, light, colour, colourPos in ( line .. "#a" ):gmatch( "(.-)()#(l?)(%l)()" ) do 108 if main then 109 mud.printMain( text:gsub( "##", "#" ), fg, bg, bold ) 110 end 111 112 if chat then 113 mud.printChat( text:gsub( "##", "#" ), fg, bg, bold ) 114 end 115 116 if line:sub( hashPos - 1, hashPos - 1 ) ~= "#" then 117 if colourPos ~= line:len() + 3 then 118 assert( Sequences[ colour ], "invalid colour sequence: #" .. light .. colour ) 119 120 bold = light == "l" 121 Sequences[ colour ]() 122 end 123 else 124 if main then 125 mud.printMain( light .. colour, fg, bg, bold ) 126 end 127 128 if chat then 129 mud.printChat( light .. colour, fg, bg, bold ) 130 end 131 end 132 end 133 134 if newLine ~= "" then 135 if main then 136 mud.newlineMain() 137 end 138 139 if chat then 140 mud.newlineChat() 141 end 142 end 143 end 144 end 145 146 function mud.print( form, ... ) 147 genericPrint( form:format( ... ), true, false ) 148 end 149 150 function mud.printc( form, ... ) 151 genericPrint( form:format( ... ), false, true ) 152 end 153 154 function mud.printb( form, ... ) 155 genericPrint( form:format( ... ), true, true ) 156 end 157 158 function mud.printr( str, fg, bg, bold ) 159 fg = fg or 7 160 bg = bg or 0 161 bold = bold or false 162 163 for line, newLine in str:gmatch( "([^\n]*)(\n?)" ) do 164 mud.printMain( line, fg, bg, bold ) 165 166 if newLine ~= "" then 167 mud.newlineMain() 168 end 169 end 170 end 171 172 function mud.enable( ... ) 173 local things = { ... } 174 175 if not things[ 1 ].enable then 176 things = things[ 1 ] 177 end 178 179 for _, thing in ipairs( things ) do 180 thing:enable() 181 end 182 end 183 184 function mud.disable( ... ) 185 local things = { ... } 186 187 if not things[ 1 ].enable then 188 things = things[ 1 ] 189 end 190 191 for _, thing in ipairs( things ) do 192 thing:disable() 193 end 194 end 195 196 local ColourSequences = { 197 d = 0, 198 k = 30, 199 r = 31, 200 g = 32, 201 y = 33, 202 b = 34, 203 m = 35, 204 c = 36, 205 w = 37, 206 } 207 208 function string.parseColours( message ) 209 message = assert( tostring( message ) ) 210 211 return ( message:gsub( "()#(l?)(%l)", function( patternPos, bold, sequence ) 212 if message:sub( patternPos - 1, patternPos - 1 ) == "#" then 213 return 214 end 215 216 if bold == "l" then 217 return "\27[1m\27[%dm" % { ColourSequences[ sequence ] } 218 end 219 220 return "\27[0m\27[%dm" % { ColourSequences[ sequence ] } 221 end ):gsub( "##", "#" ) ) 222 end