[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Replacing multiple values in an omitted line, along with coloring repectively

Replacing multiple values in an omitted line, along with coloring repectively

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by Giled   (7 posts)  [Biography] bio
Date Fri 08 Aug 2014 02:06 PM (UTC)
Message
My trigger:
<triggers>
<trigger
custom_colour="7"
enabled="y"
group="People"
match="* black man with a mohawk *"
name="Ardon"
omit_from_output="y"
script="Color_Names"
send_to="12"
sequence="100"
>
<send>
</send>
</trigger>
</triggers>

(I have one for each person's description to replace)

The text I'm trying to replace:
The black man with a mohawk swiftly dodges the brown eyed woman's pierces.


I'd like to get it to only color the description and adding the name of the person, resulting in:
The black man with a mohawk (Name1) swiftly dodges the brown eyed woman's (Name2) pierces.

What it does right now is adds the last trigger it matches to the front of the line:
[Name2] The black man with a mohawk swiftly dodges the brown eyed woman's pierces.

So there's not a problem with the trigger other than recreating it to do exactly what I want.

Additional Info:
The script language is VB and the client version is 4.84
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Fri 08 Aug 2014 02:36 PM (UTC)

Amended on Fri 08 Aug 2014 02:57 PM (UTC) by Fiendish

Message
Quote:
The script language is VB

You should switch to Lua. :)

Multiple coloring on the same line requires use of either AnsiNote ( http://www.mushclient.com/scripts/doc.php?function=AnsiNote ), Simulate (also takes ANSI formatted input like AnsiNote) ( http://www.mushclient.com/scripts/doc.php?function=Simulate ), or multiple calls to ColourTell ( http://www.mushclient.com/scripts/doc.php?function=ColourTell ) with a final ColourNote ( http://www.mushclient.com/scripts/doc.php?function=ColourNote ) at the end. You cannot get multiple colors on a single line with a simple color trigger.

To use AnsiNote or Simulate you have to first compose all of your different pieces into a single string with the format: ANSI_codes text ANSI_codes text ANSI_codes text...
The documentation for ANSI ( http://www.mushclient.com/scripts/doc.php?function=ANSI ) will guide you for how to create that. You have to be somewhat careful with Simulate. I've always had to make sure to end Simulated text with \r\n otherwise the line endings mess up.

To use ColourTell/ColourNote, you do each section of your output line that has its own color. For example,
ColourTell("red", "blue", "The black man with a mohawk")
ColourTell("white", "green", Name1)
ColourTell("red", "blue", "swiftly dodges the brown eyed woman's")
ColourTell("white", "green", Name2)
ColourNote("red", "blue", "pierces")

Now here's where it gets complicated.

It sounds like you want to have simple triggers for each character that can all fire on, and substitute text in, the same line, which is unlikely to work for you because of MUSHclient's poor substitution support (it's not impossible, but you're here asking for help, so let's just say that it's far too complicated to get working this way).

What you can do instead is store a table of all of your character descriptions ("black man with a mohawk", "brown eyed woman", etc.) and then do string search for each of them, find which ones exist in your text, add then names where appropriate, and then display the whole thing.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Giled   (7 posts)  [Biography] bio
Date Reply #2 on Fri 08 Aug 2014 02:40 PM (UTC)

Amended on Fri 08 Aug 2014 02:43 PM (UTC) by Giled

Message
Fiendish said:

You should switch to Lua. :)


I'd be happy to if that'd make this trigger easier, I just know next to nothing about Lua.

I can play around with using the ANSI syntax, I'm just not sure how to have the omitted output changed by multiple triggers?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Fri 08 Aug 2014 03:04 PM (UTC)

Amended on Fri 08 Aug 2014 05:39 PM (UTC) by Fiendish

Message
Giled said:

Fiendish said:

You should switch to Lua. :)

I'd be happy to if that'd make this trigger easier

It probably won't. See the second part of my comment above (edited after you posted).

Quote:

I can play around with using the ANSI syntax, I'm just not sure how to have the omitted output changed by multiple triggers?

Yeah. A lot of people on Aardwolf who switch over from zMUD ask me how to do this. My answer is always that MUSHclient doesn't support it natively. The only way to cleanly get multiple subs on a line is to write a plugin that does custom re-parsing of every line according to rules stored by the plugin. The problem is that plugin contents are then not world visible, so they're difficult for a player to inspect (of relevance to me because I distribute plugins to thousands of other players). You then need to add extra functionality to the plugin in order to see and manipulate what things you have substituted for what.
Read this forum thread for more information, though.
http://www.mushclient.com/forum/bbshowpost.php?id=11073

Maybe your request will be the nudge I need to finally write this plugin. I'm worried though that handing every single line of output to the script engine might be frustratingly non-performant.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Fri 08 Aug 2014 09:08 PM (UTC)
Message
Giled said:


What it does right now is adds the last trigger it matches to the front of the line:
[Name2] The black man with a mohawk swiftly dodges the brown eyed woman's pierces.

So there's not a problem with the trigger other than recreating it to do exactly what I want.


It would be helpful to see your current script, although I agree that Lua is the preferred language these days. I wouldn't even attempt it in VB.

Your request is not fully defined here.

Quote:

I'd like to get it to only color the description ...


In what colour? The original colour? Or one of your choosing?

Is the line currently coloured or monochrome?

Quote:

... and adding the name of the person ...


How many people are on a line? You seem to show two, but could it be more? Or just one?

Do the lines ever wrap? Eg.


The black man with a mohawk swiftly dodges the brown eyed 
woman's pierces.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Fri 08 Aug 2014 09:40 PM (UTC)

Amended on Fri 08 Aug 2014 09:41 PM (UTC) by Nick Gammon

Message
This plugin might work for you:

Template:saveplugin=Name_Replacer To save and install the Name_Replacer plugin do this:
  1. Copy between the lines below (to the Clipboard)
  2. Open a text editor (such as Notepad) and paste the plugin into it
  3. Save to disk on your PC, preferably in your plugins directory, as Name_Replacer.xml
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Name_Replacer.xml (which you just saved in step 3) as a plugin
  7. Click "Close"



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Name_Replacer"
   author="Nick Gammon"
   id="1da8552e375d743024b3e4d0"
   language="Lua"
   purpose="Substitutes names in incoming data"
   date_written="2014-08-09 08:15"
   requires="4.80"
   version="1.0"
   >

</plugin>

<!--  Script  -->

<script>
<![CDATA[

names = { 
 ["black man with a mohawk"] = "Name1",
 ["brown eyed woman"] = "Name2",
 ["crazy guy"] = "Name3",
 ["shuffling zombie"] = "Name4",
 
 -- more here ...
 
 } -- end of table 


-- replace a string (eg. "black man with a mohawk") with itself followed by
-- the person's name from the names table, in brackets.

function fixup (what)
  local replacement = names [what]
  
  -- give up if not found
  if not replacement then 
    return nil 
  end
  return what .. " (" .. replacement .. ")"
end -- fixup

function OnPluginPacketReceived (s)
  
  -- search for all names in the table and call fixup function if found
  for k, v in pairs (names) do
    s = string.gsub (s, k, fixup)
  end -- for each name
    
  return s
end  -- OnPluginPacketReceived

]]>
</script>

</muclient>


This works at the packet level to substitute on incoming data. Thus existing colours should be preserved. There are a few caveats with it, one being that for it to work a string (eg. "black man with a mohawk") has to appear in a single packet and not be broken over multiple ones, which may or may not happen, and if it happens may or may not happen often.

There are ways of working around that, but for simplicity see how this goes.

The text to match on, and the replacements, are in the table above, in bold. Change that as you see fit.

I am assuming here that what you match on will not have regular expression "magic" characters in it, such as brackets, or the "%" symbol. If it does, that will be more work to work around.

My test data:


The black man with a mohawk swiftly dodges the brown eyed woman's pierces.

I'd like to get it to only color the description and adding the name of the crazy guy resulting in a shuffling zombie.


Output shown:


The black man with a mohawk (Name1) swiftly dodges the brown eyed woman (Name2)'s pierces.
I'd like to get it to only color the description and adding the name of the crazy guy (Name3) resulting in a shuffling zombie (Name4).


I should warn you also that since this alters the incoming packet (before trigger matching) any triggers now have to match the modified line (if you have such triggers).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Giled   (7 posts)  [Biography] bio
Date Reply #6 on Fri 08 Aug 2014 09:49 PM (UTC)
Message
Nick Gammon said:

Giled said:


What it does right now is adds the last trigger it matches to the front of the line:
[Name2] The black man with a mohawk swiftly dodges the brown eyed woman's pierces.

So there's not a problem with the trigger other than recreating it to do exactly what I want.


It would be helpful to see your current script, although I agree that Lua is the preferred language these days. I wouldn't even attempt it in VB.

Your request is not fully defined here.


Apologies, here's the function for coloring names:
Sub Color_Names (strTriggerName, trig_line, arrWildCards)
Dim name, sdesc, first, rest
If ISNull(strTriggerName) or strTriggername = "" Then
strTriggerName = "Unknown"
End If
First = Left(strTriggerName,1)
First = UCASE(First)
rest = RIGHT(strTriggername,LEN(strTriggerName)-1)
name = "[" & First & rest & "]"
sdesc = trig_line
World.ColourTell "red","black", name
World.ColourTell "white", "black", " " & sdesc & VbCrLf
End Sub

Nick Gammon said:

Quote:

I'd like to get it to only color the description ...


In what colour? The original colour? Or one of your choosing?

Is the line currently coloured or monochrome?


I've just used red for the time being, the lines are monochrome currently.

Nick Gammon said:

Quote:

... and adding the name of the person ...


How many people are on a line? You seem to show two, but could it be more? Or just one?

Do the lines ever wrap? Eg.


The black man with a mohawk swiftly dodges the brown eyed 
woman's pierces.




There are usually only two, but there can be more (if that's a problem, I'm happy to have it only match the first two instances.

The lines do wrap like that on occasion, yes.
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Fri 08 Aug 2014 09:59 PM (UTC)

Amended on Fri 08 Aug 2014 10:00 PM (UTC) by Nick Gammon

Message
My plugin above might go a fair way towards what you want. It doesn't colour things, but you could add that in to the "fixup" function.

For example, to colour the replacement name in red:


function fixup (what)
  local replacement = names [what]
  
  -- give up if not found
  if not replacement then 
    return nil 
  end
  return what .. " (" .. ANSI (31) .. replacement .. ANSI (37) .. ")"
end -- fixup


That injects the ANSI code for red (31), the replacement name, and then the ANSI code for white (37).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Giled   (7 posts)  [Biography] bio
Date Reply #8 on Mon 11 Aug 2014 09:13 AM (UTC)

Amended on Mon 11 Aug 2014 09:25 AM (UTC) by Giled

Message
Nick Gammon said:

My plugin above might go a fair way towards what you want. It doesn't colour things, but you could add that in to the "fixup" function.

For example, to colour the replacement name in red:


function fixup (what)
  local replacement = names [what]
  
  -- give up if not found
  if not replacement then 
    return nil 
  end
  return what .. " (" .. ANSI (31) .. replacement .. ANSI (37) .. ")"
end -- fixup


That injects the ANSI code for red (31), the replacement name, and then the ANSI code for white (37).


This works amazingly well... Exactly what I was after! It seems some patterns don't match and fire off, which I assume maybe is that the packet isn't coming in as expected? Either way, it's something I can accept.

EDIT: In testing further it seems like it's only the descriptions with a hyphen are being skipped.
E.G. marred, dusky-toned male
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #9 on Mon 11 Aug 2014 02:29 PM (UTC)

Amended on Mon 11 Aug 2014 02:33 PM (UTC) by Fiendish

Message
Quote:
In testing further it seems like it's only the descriptions with a hyphen are being skipped.
E.G. marred, dusky-toned male

Hyphens are magic characters in Lua patterns. ( http://www.lua.org/pil/20.2.html )
string.gsub won't catch them literally without escaping them.

Quote:
Some characters, called magic characters, have special meanings when used in a pattern. The magic characters are

( ) . % + - * ? [ ^ $

The character `%´ works as an escape for those magic characters. So, '%.' matches a dot; '%%' matches the character `%´ itself.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Giled   (7 posts)  [Biography] bio
Date Reply #10 on Mon 11 Aug 2014 02:43 PM (UTC)
Message
Does it need to be escaped in the array of names, or in the incoming string to match against the array?
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #11 on Mon 11 Aug 2014 04:59 PM (UTC)
Message
In the array of names.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Giled   (7 posts)  [Biography] bio
Date Reply #12 on Mon 11 Aug 2014 05:37 PM (UTC)
Message
Got it. Thank you all so much for the help. This is perfect.
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #13 on Mon 11 Aug 2014 07:58 PM (UTC)
Message
Glad it works, although I am a bit surprised the hyphen one matches. The function "fixup" looks up the replacement by looking up the original string, I would have thought that the "%" symbol would throw that out.

Still, if it works, great!

If not, there are ways of working around that ...

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,982 posts)  [Biography] bio   Forum Administrator
Date Reply #14 on Mon 11 Aug 2014 08:14 PM (UTC)

Amended on Mon 11 Aug 2014 08:15 PM (UTC) by Nick Gammon

Message
This modified version should work with any string for the character.

Template:saveplugin=Name_Replacer To save and install the Name_Replacer plugin do this:
  1. Copy between the lines below (to the Clipboard)
  2. Open a text editor (such as Notepad) and paste the plugin into it
  3. Save to disk on your PC, preferably in your plugins directory, as Name_Replacer.xml
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Name_Replacer.xml (which you just saved in step 3) as a plugin
  7. Click "Close"



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Name_Replacer"
   author="Nick Gammon"
   id="1da8552e375d743024b3e4d0"
   language="Lua"
   purpose="Substitutes names in incoming data"
   date_written="2014-08-09 08:15"
   date_modified="2014-08-12 07:12"
   requires="4.80"
   version="2.0"
   >

</plugin>

<!--  Script  -->

<script>
<![CDATA[

-- returns a function which does the correct replacement
function funcMaker (replacement)
  return function (what)
    return what .. " (" .. ANSI (31) .. replacement .. ANSI (37) .. ")"
  end -- fixup
end -- function funcMaker

names = { 
 ["black man with a mohawk"]  = funcMaker ("Name1"),
 ["brown eyed woman"]         = funcMaker ("Name2"),
 ["crazy guy"]                = funcMaker ("Name3"),
 ["shuffling zombie"]         = funcMaker ("Name4"),
 ["marred, dusky-toned male"] = funcMaker ("Name5"),
 
 -- more here ...
 
 } -- end of table 

-- fix up search strings
namesFixed = {}
for k, v in pairs (names) do
  namesFixed [string.gsub (k, "[%%%]%^%-$().[*+?]", "%%%1")] = v
end -- for

function OnPluginPacketReceived (s)
  for k, v in pairs (namesFixed) do
    s = string.gsub (s, k, v)
  end -- for each name
  return s
end  -- OnPluginPacketReceived

]]>
</script>

</muclient>


It uses a function-which-returns-a-function (funcMaker) to remember what the replacement string is, removing the need to look it up again. This therefore works with strings with hyphens etc. in them.

There is code too to add the "%" symbols for you, so just enter what you actually see on the screen, without adding "%" before hyphens.

Example:


The black man with a mohawk swiftly dodges the brown eyed woman's pierces.

I'd like to get it to only color the description and adding the name of the crazy guy resulting in a shuffling zombie.

I see a marred, dusky-toned male here.


Results:


The black man with a mohawk (Name1) swiftly dodges the brown eyed woman (Name2)'s pierces.

I'd like to get it to only color the description and adding the name of the crazy guy (Name3) resulting in a shuffling zombie (Name4).

I see a marred, dusky-toned male (Name5) here.


This version colours the replacement names red, but you can choose other colours, see:

http://www.gammon.com.au/scripts/doc.php?function=ANSI

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


37,341 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]