fg() and bg()

Share your scripts and packages with other Mudlet users.
Post Reply
Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

fg() and bg()

Post by Phoenix » Fri Oct 07, 2011 10:39 pm

fg() and bg(), until now, did not check to see if the colour inside them was a valid color in the color_table. If you used an invalid colour, you'd get an error such as this:
<./mudlet-lua/lua/GUIUtils.lua:651: attempt to index field '?' (a nil value)>
Needless to say, this was rather uninformative... I couldn't figure out, at all, what damned colour I was using that wasn't defined. :idea: Behold: New and improved, it writes to the error log TELLING you which it doesn't use.
Edited as per the following posts. Was using an assert with concat, now uses an if statement
Code: [show] | [select all] lua
function fg(colorName)
   if color_table[colorName] then
      setFgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
   else 
      error("fg: "..colorName.." is not a valid color!")
   end
end

function bg(colorName)
   if color_table[colorName] then
      setBgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
   else
      error("bg: "..colorName.." is not a valid color!")
   end
end
Last edited by Phoenix on Wed Oct 19, 2011 11:44 am, edited 1 time in total.

User avatar
Vadi
Posts: 4736
Joined: Sat Mar 14, 2009 3:13 pm

Re: fg() and bg()

Post by Vadi » Sat Oct 08, 2011 12:10 am

This has been improved in the latest code: https://github.com/Beliaar/mudlet-lua/b ... s.lua#L645

The color name isn't listed though because it always has to be concatenated, even if the assertion didn't hit (so it's not really efficient).

Phoenix
Posts: 92
Joined: Tue Feb 15, 2011 3:23 am

Re: fg() and bg()

Post by Phoenix » Tue Oct 11, 2011 4:27 am

A non-specific way to figure it out was already there - by looking up the exact lines, the meaning of the error was clear. However, this doesn't tell you what the error to search for in your script is, especially if you use fg() and bg() a lot in conjunction with links, normal echos, and such.

I'm wondering, would if color_table[colorName] then <code> else error() end be more efficient, as the concat wouldn't need to happen unless the if failed? Something like this:
Code: [show] | [select all] lua
function fg(colorName)
   if color_table[colorName] then
      setFgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
   else
      error("fg: ".. colorName.." is not a valid color!")
   end
end
From my tests, seems that this ought to be the same speed as an assert with no concatenation, or possibly even a bit faster (hard to tell).

edit - While I can see that yes, this should prove faster, as no concatenation is happening unless the 'else' hits, in practice I am unable to see any difference between the two methods.

At 20 seconds of code-crunching both ways (stripping out the setFgColor bit, just the if vs assert), I'm unable to see any difference. Both get around 22,500 iterations of code on the loop, before my 20 seconds of crunch expires. I'm not seeing how a concatenation makes any practical difference in speed on my laptop, though I know theoretically it should make some minor bit.

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: fg() and bg()

Post by Denarii » Wed Oct 12, 2011 8:37 pm

Add this and the same changes for bg().

User avatar
Vadi
Posts: 4736
Joined: Sat Mar 14, 2009 3:13 pm

Re: fg() and bg()

Post by Vadi » Tue Jan 08, 2013 4:37 am

Thanks for the feedback. Next update after 2.1 will have bg() and fg() report the wrong color names used.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests