Pattern matching a variable

Post Reply
jwrobbs
Posts: 6
Joined: Sat Sep 23, 2017 1:26 am

Pattern matching a variable

Post by jwrobbs » Sat Sep 23, 2017 2:03 am

Thanks in advance for the help. I'm guessing this is obvious and simple to you experienced folks. And I'm searching for the wrong terms.

I have a variable full of strings. I want to make them red.

Simple text highlight triggers are no problem. Regex is a work in progress. But I can't figure out where/how to insert the variable.

Thanks again,
Josh

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

Re: Pattern matching a variable

Post by Vadi » Sat Sep 23, 2017 6:17 am

How about an alternative approach - check out https://wiki.mudlet.org/w/Manual:Script ... _target.3F. Does that work?

jwrobbs
Posts: 6
Joined: Sat Sep 23, 2017 1:26 am

Re: Pattern matching a variable

Post by jwrobbs » Sat Sep 23, 2017 12:10 pm

I saw that and don't understand it.

How do you highlight Mud output with an alias? Triggers respond to Mud output. Aliases respond to user input. Right?

What is "id"? Is that a built in function? I tried using a function, but I still needed a way to trigger it.

Thanks for the help,

Josh

Jor'Mox
Posts: 806
Joined: Wed Apr 03, 2013 2:19 am

Re: Pattern matching a variable

Post by Jor'Mox » Sat Sep 23, 2017 1:39 pm

So, assuming you don't have too many things you want to highlight, you could do something like this, put into a function called via a Lua function type trigger (which calls the function for each line coming from the game):
Code: [show] | [select all] lua
local highlight_strings = {"string one","string two","string three"}
local index
for i,v in ipairs(highlight_strings) do
	index = 1
	while selectString(v,index) > -1 do
		fg("red")
		index = index + 1
	end
end
deselect()
resetFormat()
Or, for a more comprehensive strategy, using triggers to highlight each target string (which will be faster, but the speed will only really be noticed if you have a large enough number of strings to highlight), you can do something like this:
Code: [show] | [select all] lua
highlight = highlight or {}
highlight.ids = highlight.ids or {}
highlight.strings = highlight.strings or {"string one", "string two", "string three"}

function highlight.reset_highlights()
    for i,v in ipairs(highlight.ids) do
        killTrigger(v)
    end
    for i,v in ipairs(highlight.strings) do
        table.insert(highlight.ids,tempTrigger(v, [[highlight.highlight_string("]] .. v .. [[")]])
    end
    killAlias(highlight.alias)
    highlight.alias = tempAlias([[^add highlight (.*)]],[[highlight.add_highlight(matches[2])]])
end

function highlight.highlight_string(str)
    local index = 1
    local color = "red"
    while selectString(str,index) > -1 do
        fg(color)
        index = index + 1
    end
    deselect()
    resetFormat()
end

function highlight.add_highlight(str)
    table.insert(highlight.strings,str)
    table.insert(highlight.ids,tempTrigger(str, [[highlight.highlight_string("]] .. str .. [[")]])
end

registerAnonymousEventHandler("sysConnectionEvent","highlight.reset_highlights")
This creates an "add highlight" alias that you can use to add a new string to highlight (though it won't be saved when you quit, so you will want to add it to the list manually if you want to keep that highlight around), and it will take care of making the necessary triggers whenever you connect to the game.

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

Re: Pattern matching a variable

Post by Vadi » Sat Sep 23, 2017 3:23 pm

jwrobbs wrote:
Sat Sep 23, 2017 12:10 pm
I saw that and don't understand it.

How do you highlight Mud output with an alias? Triggers respond to Mud output. Aliases respond to user input. Right?

What is "id"? Is that a built in function? I tried using a function, but I still needed a way to trigger it.

Thanks for the help,

Josh
In addition to Jor'Mox, if you look closely at that alias, you'll see that it uses tempTrigger() - so the alias actually creates a temporary trigger, which then when matched, highlights the word.

jwrobbs
Posts: 6
Joined: Sat Sep 23, 2017 1:26 am

Re: Pattern matching a variable

Post by jwrobbs » Thu Sep 28, 2017 2:16 am

Jor'Mox wrote:
Sat Sep 23, 2017 1:39 pm
a Lua function type trigger (which calls the function for each line coming from the game):
Bingo!

While I'm sure all of this is very helpful (I need to dig through it a bit), the fact that the function checks every line is the key bit I was missing.

Thanks a million.

Looks like I'll need to spend some time on Code Combat figuring out Lua. Thanks again.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests