mudgangster

Tiny, scriptable MUD client
Log | Files | Refs | README

status.lua (1095B)


      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 end
     39 
     40 function mud.bar( priority )
     41 	local item = {
     42 		priority = priority,
     43 		text = "",
     44 
     45 		set = function( self, form, ... )
     46 			enforce( form, "form", "string" )
     47 
     48 			self.text = string.parseColours( form:format( ... ) )
     49 			drawBar()
     50 		end,
     51 	}
     52 
     53 	table.insert( barItems, item )
     54 	table.sort( barItems, function( a, b )
     55 		return a.priority < b.priority
     56 	end )
     57 
     58 	return item
     59 end
     60 
     61 return {
     62 	init = function( set )
     63 		setStatus = set
     64 	end,
     65 }