Prompt newline

Posted by Rmf113 on Mon 30 Jul 2012 11:23 PM — 5 posts, 17,369 views.

#0
Hi,

I've read all the previous posts regarding solutions for adding a newline after the prompt but I'm still unable to find a successful fix for my problem. Hopefully someone can point me in the right direction.

I want to trigger a newline on a prompt for filtering purposes. The prompt is very simple...


|1|*  


I took the Plugin (below) from an old post and modified the string.gsub to read


return (string.gsub (s, "\n%|1%|%*", "%1\n"))


However this is unsuccessful. Am I correct in thinking % is the correct escape character here rather than backslash? I did also try without escape characters but it won't trigger.


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Saturday, March 21, 2009, 11:42 PM -->
<!-- MuClient version 4.37 -->

<!-- Plugin "Add_NewLine_To_Prompt" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Add_NewLine_To_Prompt"
   author="Nick Gammon"
   id="3862dd7f2ba50abfe50d4844"
   language="Lua"
   purpose="Forces a newline after a prompt line"
   date_written="2009-03-21 23:38:18"
   requires="4.37"
   version="1.0"
   >

</plugin>


<!--  Script  -->


<script>
<![CDATA[
function OnPluginPacketReceived (s)
  return (string.gsub (s, "%|1%|%*", "%1\n"))
end -- function OnPluginPacketReceived
]]>
</script>


</muclient>


The packet debug for the line looks as follows:


...[0;30;44m|1|.   0d 0a 1b 5b 30 3b 33 30 3b 34 34 6d 7c 31 7c 1b
[1;37;40m.[0;34;   5b 31 3b 33 37 3b 34 30 6d 1b 5b 30 3b 33 34 3b
40m.[1;34;40m*.[   34 30 6d 1b 5b 31 3b 33 34 3b 34 30 6d 2a 1b 5b 
0;34;40m.[1;37;4   30 3b 33 34 3b 34 30 6d 1b 5b 31 3b 33 37 3b 34
0m         	   30 6d


Code tags added. - Moderator
Amended on Tue 31 Jul 2012 05:54 AM by Nick Gammon
Australia Forum Administrator #1
You can see that after the newline (hex 0A) there is other stuff:


.[0;30;44m


The string.gsub needs to match that too. Or put in a wildcard, eg.


return (string.gsub (s, "\n.*%|1%|%*", "%1\n"))
#2
Thanks, that now triggers correctly. However I realise this is not going to achieve what I want as the newline is only cosmetic client side. What I need is a Carriage return /r to reset the prompt.

I thought I could use something simple like below to put a /r in front of the newline when sending my next line but this doesn't work.


function OnPluginCommandEntered (sText)
  return string.gsub (sText, "\n", "\r\n")
end -- function 


Basically what I'm trying to do is create a second window for watching another player. Much of this is fairly easy to setup as it is simply a trigger on the prompt at the beginning of the line to filter the output to the correct window.

My output has a basic prompt of *
The watched player output has a prompt of |1|*

Hence I just filter lines starting |1|* to the second dummy window.

The only problem I have is that when some watched data is sent in, my prompt line is still showing |1|* and so my command goes to the second window. This also happens the other way round... When I send a command, the first command from the watched player comes in with the standard * prompt and so goes to my main output window.

I therefore need a way to effectively reset the prompt before the line is filtered to the correct window by issuing a carriage return.

is OnPluginCommandEntered the correct function to use for this?
#3
Decided to take a different approach by just using string.match on the packet and then sending a carriage return if the prompt line packet is found.

Need to test but it should work to revert the prompt for my commands. Not sure yet how I can do this in reverse (switch to the |1| prompt for watched users command) without initiating an infinite loop.



function OnPluginPacketReceived (s)

if string.match (s, "%[0;30;44m%|1%|.%[1;37;40m.%[0;34;40m.%[1;34;40m%*.%[0;34;40m.%[1;37;40m$") then
	Send ("\r")
end -- if
  return (s)
end -- function OnPluginPacketReceived

Amended on Wed 01 Aug 2012 03:45 PM by Rmf113
#4
Ok I'm getting there now. Went back to the beginning and started from scratch. What happens now is

-I record in a variable the last command that I sent out.
-I then check each incoming line for the prompt
-When prompt is matched, I check the line to see if it matches my last command sent out.
if it does, then it's a local command and I simply add a carriage return in front to revert the prompt.
if it does not, then it's a command from the remote player and so I will need to add the |1| to the front of the line if not present


<variables>
  <variable name="snoopcheck">N</variable>
  <variable name="commcheck">empty</variable>
</variables>

<!--  Script  -->


<script>
<![CDATA[


function OnPluginCommandEntered (sText)
  local snoopy = GetVariable ("snoopcheck")
-- only apply if actively snooping
  if string.match (snoopy, "Y") then 	
	newcommand = "\r\n" .. sText
	-- Note ("add CR")   	
  	return newcommand
  else 
	return sText
  end -- if
end -- function

function OnPluginSent (sText)
SetVariable ("commcheck", sText)
end -- function 

function OnPluginLineReceived (sText)
  local origin = GetVariable ("commcheck")
  test1 = sText

if (string.match (test1, "^%*") or string.match (test1, "^%|1%|%*"))  then -- line is a command
	-- Note ("Command issued")

if string.match (sText, origin) then
	--	Note ("Command was sent locally")
		world.SetVariable ("commcheck", "empty")
		-- No need to do anything else as OnPluginCommandEntered will issue /r to revert prompt
	else 
	--	Note ("Command was sent remotely")
		world.SetVariable ("commcheck", "empty")
		-- <code to add |1| to beginning of line if currently on standard prompt>
	end -- if
else 
 -- Note ("Not a command")
end -- if
	return true  -- display it
end -- function

]]>
</script>