An Easier Way to Make Colored Links

Share your scripts and packages with other Mudlet users.
Post Reply
Jor'Mox
Posts: 1089
Joined: Wed Apr 03, 2013 2:19 am

An Easier Way to Make Colored Links

Post by Jor'Mox » Thu Oct 25, 2018 8:16 pm

I wrote a function that uses cecho to write out text, with the added ability to put links in the middle of it (via cinsertLink), using a <link> tag. Just put the tags around whatever you want to make a link (as described in the comments at the top), and it will do it all the rest of the work for you.
Code: [show] | [select all] lua
-- colorLinks function provides an easy way to intersperse links into colored text
-- Link tag format: <link: code here ; hint here ; color>text of link</link>

function colorLinks(win, str)
	if not str then
		str, win = win, "main"
	end
	str = str:split("</link>")
	for _, w in ipairs(str) do
		local before, info, link = w:match("(.*)<link: ([^>]+)>(.*)")
		if before then
			cecho(win, before)
			info = info:split(" ; ")
			if info[3] then
				if color_table[info[3]] then
					link = string.format("<%s>%s<reset>", info[3], link)
				else
					info[3] = nil
				end
			end
			cechoLink(win, link, info[1], info[2] or "", info[3] and true)
		else
			cecho(win, w)
		end
	end
end

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests