Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Message
| I have been asked about this many times, so here at last is a plugin that will simplify sending text to the world window (like a world.note) with imbedded colour codes.
The general technique is to break up the supplied text at the tilde character (~) and look for a colour code after that character. eg.
~R bright red ~G bright green
~r dull red ~g dull green
~rG dull red on bright green ~YM yellow on bright magenta
You can specify the foreground and background colour (first letter, second letter). If either is a period (.) they are unchanged, so you can change the foreground, background or both.
The exact codes are in the subroutine below. Generally they match the first letter of the colour (excepting K for black), where the upper-case colour is the bright version (Y = bright yellow) and the lower-case colour is the dull version (y = dull yellow).
- K = bright black (gray), k = black
- R = bright red, r = dark red
- G = bright green, g = dark green
- Y = bright yellow, y = dark yellow
- B = bright blue, b = dark blue
- M = bright magenta, m = magenta
- C = bright cyan, c = dark cyan
- W = bright white, w = dark white (silver)
If you want you could edit it to add other codes of your own.
There is also a "save" (#) and "restore" (^) code to remember earlier colours (note, these do not nest).
You can copy from below the line below, paste into a notepad window, and save as ColourNote.xml, or just download it from:
http://www.mushclient.com/plugins/ColourNote.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<!-- Saved on Wednesday, October 23, 2002, 12:48 PM -->
<!-- MuClient version 3.26 -->
<!-- Plugin "ColourNote" generated by Plugin Wizard -->
<!--
You can edit the lookup_colour function to add more colour codes.
-->
<muclient>
<plugin
name="ColourNote"
author="Nick Gammon"
id="8f86e2da6eea3806f1836050"
language="JScript"
purpose="Lets you send a string with colour codes to the world window"
date_written="2002-10-23 12:46:12"
requires="3.23"
version="1.0"
>
<description trim="y">
<![CDATA[
This is designed to be called from a script like this:
world.CallPlugin "8f86e2da6eea3806f1836050", _
"colournote", _
"~R red ~G green ~B blue ~RG red-on-green"
To simplify things, make a "stub" routine in your main script file, like this:
// example in Jscript:
function ColourNote (text)
{
world.CallPlugin ("8f86e2da6eea3806f1836050", "colournote", text);
}
' example in VBscript:
sub ColourNote (text)
world.CallPlugin "8f86e2da6eea3806f1836050", "colournote", text
end sub
Then you can just do colour text like this:
ColourNote ("~R This is red ~G This is green ~B This is blue");
ColourNote:help - this help
]]>
</description>
</plugin>
<!-- Script -->
<script>
<![CDATA[
function lookup_colour (which)
{
var c;
// add more codes below if you want to process different letters
// Reserved characters are: space, and: . # ^ ~
switch (which)
{
case "K": c = "gray"; break;
case "k": c = "black"; break;
case "R": c = "red"; break;
case "r": c = "maroon"; break;
case "G": c = "lime"; break;
case "g": c = "green"; break;
case "Y": c = "yellow"; break;
case "y": c = "olive"; break;
case "B": c = "blue"; break;
case "b": c = "navy"; break;
case "M": c = "magenta"; break;
case "m": c = "purple"; break;
case "C": c = "aqua"; break;
case "c": c = "teal"; break;
case "W": c = "white"; break;
case "w": c = "silver"; break;
default: c = ""; break; // unknown ones are no change
} // end of switch on which colour code
return c;
} // end of lookup_colour
/*
This sends a coloured message to the world window.
Colour codes are like this: ~fb
~ = colour code delimiter
f = foreground colour (eg. R = bright red, g = dull green)
b = background colour (same meanings)
If the background colour is a space, then it is assumed to be
unchanged. eg. ~R red ~G green ~B blue
Otherwise, it is processed as the background colour,
eg. ~RB red-on-blue ~.G red-on-green ~yc dark-yellow on dark-cyan
Special colours:
. = no change (eg. to change background only)
# = save colours (save current foreground/background colour)
^ = restore colours (restore to saved colour)
eg.
colournote ("This will be a ~R red ~G green ~B blue.");
*/
function colournote (msg)
{
var msg_array, // split up line
stext, // what we are sending
fcolour, // foreground colour code
bcolour, // background colour code
foreground, // converted foreground colour
background, // converted background colour
save_foreground, // saved foreground colour
save_background // saved background colour
// save colours for later restore
save_foreground = world.RGBColourToName (world.NoteColourFore ());
save_background = world.RGBColourToName (world.NoteColourBack ());
foreground = save_foreground;
background = save_background;
// convert into an array
msg_array = msg.split ("~");
// first element in array (before tilde) is just shown as is
world.Tell (msg_array [0]);
for (i = 1; i < msg_array.length; i++)
{
fcolour = msg_array [i].substring (0, 1); // foreground colour
bcolour = msg_array [i].substring (1, 2); // background colour
stext = msg_array [i].substring (2); // rest is message
// get foreground colour, handle special cases, lookup others
switch (fcolour)
{
case ".": break; // colour unchanged
case "#": save_foreground = foreground; break;
case "^": foreground = save_foreground; break;
default: foreground = lookup_colour (fcolour); break;
} // end of switch on fcolour
// get background colour, handle special cases, lookup others
switch (bcolour)
{
case " ":
case ".": break; // colour unchanged
case "#": save_background = background; break;
case "^": background = save_background; break;
default: background = lookup_colour (bcolour); break;
} // end of switch on fcolour
//
// now display the text in the appropriate colour
//
world.ColourTell (foreground, background, stext);
} // end of loop on each colour change
world.Note (""); // finish off line
} // end of colournote
]]>
</script>
<!-- Plugin help -->
<aliases>
<alias
script="OnHelp"
match="ColourNote:help"
enabled="y"
>
</alias>
</aliases>
<script>
<![CDATA[
function OnHelp (sName, sLine, wildcards)
{
world.Note (world.GetPluginInfo (world.GetPluginID, 3));
}
]]>
</script>
</muclient>
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|