Pattern matching a variable

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

Pattern matching a variable

Post by jwrobbs »

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: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Pattern matching a variable

Post by Vadi »

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

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

Re: Pattern matching a variable

Post by jwrobbs »

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: 1142
Joined: Wed Apr 03, 2013 2:19 am

Re: Pattern matching a variable

Post by Jor'Mox »

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: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Pattern matching a variable

Post by Vadi »

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: 21
Joined: Sat Sep 23, 2017 1:26 am

Re: Pattern matching a variable

Post by jwrobbs »

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.

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

Re: Pattern matching a variable

Post by jwrobbs »

Jor'Mox wrote:
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):
So I'm giving Mudlet another try and I'm giving this problem another try. But it still doesn't make sense.

I have a variable with 1 value (target = bob). I want that to have a specific style. I tried pasting that script into a Lua function and it gives me errors. You can't put the script in the place of the pattern. And you have to have a pattern so adding the code at the bottom doesn't work either.

What am I missing?

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

Re: Pattern matching a variable

Post by Jor'Mox »

What you would do is make a script, with the code like so:
Code: [show] | [select all] lua
local highlight_strings = {"string one","string two","string three"}
function do_highlight()
    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()
end
And then put this as the pattern: do_highlight()

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

Re: Pattern matching a variable

Post by jwrobbs »

Now I think it makes sense.

You set the trigger to function and call the function from the pattern. The function itself goes in scripts.

Thanks for the help!

Post Reply