We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

User:SB112/fprint.lua

From Esolang
Jump to navigation Jump to search
local fprintcontrols = {
	RESET=0,
	NONE=0,
	BOLD=1,
	UNBOLD=22,
	LIGHT=2,
	UNLIGHT=22,
	FAINT=2,
	UNFAINT=22,
	ITALIC=3,
	UNITALIC=23,
	UNDERLINE=4,
	UNUNDERLINE=24,
	DERLINE=24,
	BLINK=5,
	UNBLINK=25,
	FASTBLINK=6, -- not supported on all systems
	UNFASTBLINK=25,
	INVERT=7,
	UNINVERT=27,
	STRIKETHROUGH=9,
	UNSTRIKETHROUGH=29,
}
local fprintcolors = {
	BLACK=0,
	RED=1,
	GREEN=2,
	YELLOW=3,
	BLUE=4,
	MAGENTA=5,
	PINK=5,
	PURPLE=5,
	CYAN=6,
	SKY=6,
	WHITE=7,
	GRAY=60, 
	GREY=60, 
}
local function fprintsinglecolor(str)
	local newstr = str:sub(2,-2)
	local a = fprintcontrols[newstr]
	if a then
		return "\x1b["..a.."m"
	end
	local num = 30
	local bg = false
	if newstr:sub(1,3)=="BG " then
		num = num+10
		bg = true
		newstr = newstr:sub(4,-1)
	end
	if newstr:sub(1,1)=="#" then
		local r,g,b,threeletrs
		if #newstr == 4 then
			r,g,b = string.match(newstr,"(%x)(%x)(%x)",2)
			threeletrs=true
		elseif #newstr == 7 then
			r,g,b = string.match(newstr,"(%x%x)(%x%x)(%x%x)",2)
		end
		r,g,b = tonumber(r,16), tonumber(g,16), tonumber(b,16)
		if threeletrs then
			r,g,b = r*16, g*16, b*16
		end
		return "\x1b["..(bg and 48 or 38)..";2;"..r..";"..g..";"..b.."m"
	end
	if newstr:sub(1,7)=="BRIGHT " then
		num = num+60
		newstr = newstr:sub(8,-1)
	elseif newstr:sub(1,6)=="LIGHT " then
		num = num+60
		newstr = newstr:sub(7,-1)
	end
	local color = fprintcolors[newstr]
	if color then
		num = num+fprintcolors[newstr]
		return "\x1b["..num.."m"
	else
		local r,g,b = string.match(newstr,"(%d+),(%d+),(%d+)")
		return "\x1b["..(bg and 48 or 38)..";2;"..r..";"..g..";"..b.."m"
	end
end

local function fprint(...)
	local str = table.concat(({...}),"	[RESET]")
	str = string.gsub(str.."[RESET]","%[[^%]]*%]",fprintsinglecolor)
	print(str)
end


-- Example:
 fprint("[STRIKETHROUGH]hellorwrold[UNSTRIKETHROUGH] Hello, world! [BRIGHT RED][BOLD]IM EVIL MUHAHAHA[RESET] [BG #DEADBE]Im the [INVERT]deadbe color.","2nd arg, [BG BLUE]btw [BLINK]woah[RESET] [50,100,150]Im RGB=50,100,150 !")

return fprint