Posted by
| Nick Gammon
Australia (23,100 posts) Bio
Forum Administrator |
Message
| I gather you want to match on 4 words, one of which might be in bold, and report which one that is? The words may or may not be exactly "left right front back"?
First, normal trigger matching lets you specify the colour, boldness etc. of the match text, but that only applies to the first matching character.
It would be OK if you wanted to find "front" in bold, wherever it is on the line, but I gather you are looking for something slightly different.
First, let's explore what you can do with a trigger and calling a script in Lua.
If this is your trigger:
<triggers>
<trigger
custom_colour="4"
enabled="y"
match="left right front back"
name="testtrigger"
script="MatchBold"
sequence="100"
>
</trigger>
</triggers>
Then you can write a small script to show what is being received:
function tprint (t, indent, done)
done = done or {}
indent = indent or 0
for key, value in pairs (t) do
Tell (string.rep (" ", indent)) -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
Note (tostring (key), ":");
tprint (value, indent + 2, done)
else
Tell (tostring (key), "=")
print (value)
end
end
end
function MatchBold (thename, theoutput, wildcards, line)
Note ("Trigger " .. thename .. " fired.")
Note ("Matching line was: " .. theoutput)
Note ("Wildcards ...")
table.foreach (wildcards, print)
Note ("Style runs ...")
tprint (line)
end -- of MatchBold
The "tprint" function recursively displays entries in a Lua table, and is supplied as part of the MUSHclient distribution in the exampscript.lua file.
Testing this with the word "front" in bold gives this result:
Trigger testtrigger fired.
Matching line was: left right front back
Wildcards ...
0=left right front back
Style runs ...
1:
textcolour=12632256
backcolour=0
length=11
style=0
text=left right
2:
textcolour=16777215
backcolour=0
length=5
style=1
text=front
3:
textcolour=12632256
backcolour=0
length=5
style=0
text= back
This gives us a big clue as to what we need to do to report the word in bold, walk through the table and find the entry with a style of 1 (the bold bit). Of course, we can do other tests too, like testing the foreground/background colour, and other style bits. For reference, style bits are (bold=1, underline=2, blink=4) or'ed together (eg. bold underline would be 6).
Now we can simply check in a loop for which line entry is the bold one, by replacing the script function called with this one:
function MatchBold (thename, theoutput, wildcards, line)
for k, v in pairs (line) do
if v.style == 1 then
Note ("Bold word is " .. v.text)
end -- if
end -- for
end -- of MatchBold
Running this on my test line gives:
Bold word is front
Of course, if you want to match other words, then simply modify the trigger accordingly, the script will stay the same.
The beauty of this approach is that by scripting, you can identify words that match a tight criteria (eg. red bold italic), and throw in other tests as well (eg. displaying the words that do not match as well).
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|