mudgangster

Log | Files | Refs

utils.lua (4083B)


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