mudgangster

Log | Files | Refs

status.lua (1117B)


      1 local setStatus
      2 local barItems = { }
      3 
      4 local function drawBar()
      5 	local status = { }
      6 
      7 	for _, item in ipairs( barItems ) do
      8 		local fg = 7
      9 		local bold = false
     10 
     11 		for text, opts, escape in ( item.text .. "\27[m" ):gmatch( "(.-)\27%[([%d;]*)(%a)" ) do
     12 			if text ~= "" then
     13 				table.insert( status, {
     14 					text = text,
     15 					fg = fg,
     16 					bold = bold,
     17 				} )
     18 			end
     19 
     20 			for opt in opts:gmatch( "([^;]+)" ) do
     21 				if opt == "0" then
     22 					fg = 7
     23 					bold = false
     24 				elseif opt == "1" then
     25 					bold = true
     26 				else
     27 					opt = tonumber( opt )
     28 
     29 					if opt and opt >= 30 and opt <= 37 then
     30 						fg = opt - 30
     31 					end
     32 				end
     33 			end
     34 		end
     35 	end
     36 
     37 	setStatus( status )
     38 
     39 	mud.handleXEvents()
     40 end
     41 
     42 function mud.bar( priority )
     43 	local item = {
     44 		priority = priority,
     45 		text = "",
     46 
     47 		set = function( self, form, ... )
     48 			enforce( form, "form", "string" )
     49 
     50 			self.text = string.parseColours( form:format( ... ) )
     51 			drawBar()
     52 		end,
     53 	}
     54 
     55 	table.insert( barItems, item )
     56 	table.sort( barItems, function( a, b )
     57 		return a.priority < b.priority
     58 	end )
     59 
     60 	return item
     61 end
     62 
     63 return {
     64 	init = function( set )
     65 		setStatus = set
     66 	end,
     67 }