Filtering the items in trigger/timer/alias/variable lists

Posted by Nick Gammon on Sat 17 Jun 2006 04:08 AM — 3 posts, 11,893 views.

Australia Forum Administrator #0
Shadowfyr made a suggestion in this thread: http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=6468

Basically it was to have a method of filtering the entries shown in trigger (etc.) lists in the MUSHclient GUI.

I have added that capability into version 3.75. It is especially intended for users that have hundreds of items, and under some circumstances it can be tedious to locate a particular item (eg. a single trigger that has a missing subroutine in the script file).

For example, he wanted to find triggers that did a "send to script". He suggested some sort of dialog box with lots of options on it, but I couldn't see how that could work in more complex examples (eg. send to script *and* enabled) so I thought it would be easier if you could simply script the condition.

How it works is you provide a filter function written in Lua (regardless of whatever your scripting language for your main world file is, if any). This is provided the key for each item (eg. trigger name). You then do GetTriggerInfo (GetAliasInfo etc.) to find details about that item and decide whether or not to show it. For example, to find all triggers that did a "send to script" ...


function filter (s)
  return GetTriggerInfo (s, 15) == 12 
end -- filter


This returns true if "send to" field is 12 (script).

Now you can make more complex checks by using a longer "if" statement. For example, this will show items which are send to script AND enabled:


function filter (s)
  return GetTriggerInfo (s, 15) == 12 and  -- send to script
         GetTriggerInfo (s, 8)  -- enabled
end -- filter


In the variables list you might filter out by name or content. Here is an example of only displaying MXP variables:


function filter (s)
  return string.find (s, "^mxp_") ~= nil
end -- filter


Another example, this filters out so that only aliases which had a bad script name are shown:


function filter (s)
  return not GetAliasInfo (s, 27) and  -- script not found
             GetAliasInfo (s, 3) ~= "" -- but name given
end -- filter

Australia Forum Administrator #1
Below is a fancier example. This uses utils.listbox to let you choose the type of filtering on-the-fly. How this works is that the listbox pops up as soon as filtering is enabled, and selects the correct filter function.


function send_to_script (s)
  return GetTriggerInfo (s, 15) == 12 and -- send to script
         GetTriggerInfo (s, 8)            -- enabled
end -- send_to_script 

function enabled (s)
  return GetTriggerInfo (s, 8)  -- enabled
end -- enabled 

function badscript (s)
  return not GetTriggerInfo (s, 34) and  -- script not found
             GetTriggerInfo (s, 4) ~= "" -- but name given
end -- badscript 

function temporary (s)
  return GetTriggerInfo (s, 23)  -- temporary
end -- temporary 

function matched (s)
  return GetTriggerInfo (s, 21) > 0 -- match count
end -- matched 

-- if they cancel, show everything
function everything (s) 
  return true
end -- everything 

-- choose which function to use
result = utils.listbox ("Choose type of filtering", "Triggers",
           { 
           send_to_script = "Send to script and enabled",
           enabled = "Enabled items",
           badscript = "Script name not found",
           temporary = "Temporary triggers",
           matched = "Ones that matched something",
           },
           "badscript") -- default

-- use that function
filter = _G [result] or everything

Amended on Sat 17 Jun 2006 05:15 AM by Nick Gammon
Australia Forum Administrator #2
An example of filtering triggers/timers/aliases along the above lines is provided in the file Example_Filters.lua which ships with MUSHclient.