How to get the colorized text in a trigger

Posted by godefroi on Tue 27 Jul 2004 10:06 PM — 9 posts, 35,273 views.

#0
I have a trigger that matches a line, and I want to add some text to the line for output.

For example, say the text that comes in is:
A janitor barely clings to life.

I want to replace it with:
A janitor barely clings to life. (1%-7%)


I can do that with a omit_from_output trigger and a world.note(), but that way I loose all color information associated with the line.

I found a post that described a method for reconstructing a line using world.GetStyleInfo() and others, but that depends on the line being in the window. If I omit_from_output, it isn't.

I had had high hopes for GetRecentLines(), because it includes lines even if they have been omitted from output by a trigger, but, alas, they don't contain the ANSI codes.

the OnPluginPacketReceived() callback seems to give me color information, but that's not much use, because I'd have to regexp match every packet and save the matches to use later in the trigger, hoping everything lined up right.

Is there any way to get color information for a line that was omitted from output?

Mark
Amended on Tue 27 Jul 2004 11:17 PM by godefroi
Australia Forum Administrator #1
It can be done but it is slightly fiddly. There are a couple of problems:

If you do a script in "send to script" the line hasn't been omitted yet, but if you do a colournote to redisplay the line, notes done in "send to script" for omitted lines, are also omitted.

If you do it in a script in the script file, the line has already been omitted, so GetLineInfo and GetStyleInfo won't work.

However what *would* work would be this:

  1. In the "send" box put some scripting, and make it "send to script".
  2. The scripting would call GetLineInfo and GetStyleInfo to remember the styles on the line, and their colours. These would be saved in a variable somewhere, eg. a VBscript variable. You would probably need a variable per style run. Another approach would be to use the MUSHclient "array" script calls to put each style run's info into an array.
  3. The trigger is omitted from output
  4. The trigger also calls a script (put a script name in the script box). This is called after the earlier scripting, and recalls the style information from the variable you set a moment ago, and uses that to redraw the line the way you want.

#2
Here's what I ended up with:

one trigger, that looks like this:

<trigger
  enabled="y"
  omit_from_output="y"
  sequence="100"
  regexp="n"
  send_to="12"
  match="abc"
  script="write_line">
    <send>save_style();</send>
</trigger>


and scripts that look like this:

<script><![CDATA[
var line = null;

function save_style()
{
	line = "";
	
	var lcount = world.GetLinesInBufferCount();
	var scount = world.GetLineInfo(lcount, 11);

	for( var i = 1; i <= scount; i++ )
		line = line + get_ansi_for_rgb_fg(world.GetStyleInfo(lcount, i, 14)) + world.GetStyleInfo(lcount, i, 1);
	
	line += " (1% - 7%)";
}


function write_line()
{
	if( line )
	{
		world.AnsiNote(line);
		line = null;
	}
}


function get_ansi_for_rgb_fg(rgb)
{
	for( var i = 1; i <= 8; i++ )
	{
		if( world.NormalColour(i) == rgb )
			return ANSI(0) + ANSI(i + 29);
	}

	for( var i = 1; i <= 8; i++ )
	{
		if( world.BoldColour(i) == rgb )
			return ANSI(1) + ANSI(i + 29);
	}

	return null;
}
]]></script>

#3
One final thing, though.

I wish there was an easy, efficient way for a trigger to get the line it matched on INCLUDING the ANSI codes.

There's several requests for the ability to do this kind of thing in the forum, so I'm wondering if there's plans to implement this kind of thing?

Mark
#4
I second this motion. Colors are being used more and more often to distinguish whether things are on/off, active/not, etc. Being able to easily detect and use them would be a great asset. Since there's already a function to log sessions as HTML colour, would this enhancement be feasible?
Australia Forum Administrator #5
This request has been added as suggestion #522.

The problem with it is that, using MXP, colour in lines is not necessarily an ANSI code. The HTML logging uses HTML colour codes (eg. <font color="#123456"> ) which can represent *any* RGB colour, not just the 16 ANSI colours.

However I agree that the above method is fiddly, and it would be nice if there was an easier work-around.
#6
How about a way to get just a raw line?

For those MUDs with MXP the MXP codes will be there, and for those with ANSI, the ANSI codes will be there, and if they're mixed, then it's the scripter who has to figure it out.

Mark
USA #7
Just return all HTML style colors. If its is ansi, then return the 'string' version instead, that way even if the script coder uses different definitions for say red, all they have to do is see <font color=red> and look up the color that actually refers to in their ansi settings. Trying to deal with the raw data, which in the case of ansi would contain non-printables is a much bigger deal, especially if you also include something like 'world.HTMLNote', in order to simplify outputing the modified (or your own) lines. I mean the existing colournote system works nicely for complex things that are built on the fly, but it is really clunky and silly for simple content when the client already understands HTML color tags. But that is just my opinion.
Australia Forum Administrator #8
Quote:

I wish there was an easy, efficient way for a trigger to get the line it matched on INCLUDING the ANSI codes


See the new feature in version 3.54 of MUSHclient. In particular this post:

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4912&page=2