Page 1 of 1

Insert newline?

Posted: Fri Jun 19, 2015 3:11 am
by Nyyrazzilyss
The below line of text arrives from the mud. The first part being the prompt line, the latter half being an unknown line of text. I'd like to insert a newline between the unknown line of text and the prompt line *IF* the mud hasn't done so.

The line also contains ansi colours, that i'd still like to be displayed.

< 794h/794H 910p/910P 106v/106V P: std > You are 0 XP (0.00%) away from your next level.

Suggestions?

Re: Insert newline?

Posted: Thu Dec 03, 2015 5:58 pm
by Nyyrazzilyss
Just came back to this -

local linecheck = string.find(matches[2], "^< .* > ")

if linecheck ~= nil then
linecheck = string.find(matches[2], "> ")+1

if linecheck ~= string.len(matches[2]) then
-- printing on same line as prompt
selectSection(linecheck, string.len(matches[2])-linecheck)
copy()
replace( "\n" )
-- echo("\n")

--appendBuffer()
paste()
end
end

I've tried combinations of the above a couple different ways to get a newline inserted. It just continually restores the selection I deleted back to the same (incorrect) line prefixed by a control code - I never actually get a new line/the text moved to that line. Suggestions?

Re: Insert newline?

Posted: Mon Dec 07, 2015 8:28 pm
by Vadi
I think you need to call wrapLine() after you've inserted newlines into the buffer for it to get recognised.

Re: Insert newline?

Posted: Tue Dec 08, 2015 3:39 am
by Xavious
Trying to do this I used the following regular expression as a trigger:

^(< \d+h\/\d+H \d+p\/\d+P \d+v\/\d+V P: std >) (.*)$

I then used the following Lua code:
Code: [show] | [select all] lua
local prompt = matches[2]
local other = matches[3]

selectCurrentLine()
replace(prompt.."\n"..other)
Getting it to match colors is a bit more complicated. I got it to do a partial match by using two added statements in the previous code block:
Code: [show] | [select all] lua
local prompt = matches[2]
local other = matches[3]

selectCurrentLine()
setBgColor(getBgColor())
setFgColor(getFgColor())
replace(prompt.."\n"..other)
In any case this is a start. Hope this helps!

Re: Insert newline?

Posted: Tue Dec 08, 2015 7:01 pm
by Nyyrazzilyss
Vadi wrote:I think you need to call wrapLine() after you've inserted newlines into the buffer for it to get recognised.
wrapLine() does look to be what i'll need to be using, however:

I can't call it with no arguments, it does require window name + line number. I can figure out what the line number is, but what's the window name for the main window?

(edit)

I'm trying to use "main" as the window name.

In attempting to figure out the line number: I was displaying the results from getLines to check and make sure i'm working on the correct line. Am I using it incorrectly, or does it only work with previous lines (not the current line being received)?

For example,

display( getLines(getLineNumber(), getLineNumber()) )

just shows {}

I'd also assume once I get this working i'm going to need to feed the latter half of the line back into the trigger engine to be processed.

Someone had mentioned I should also take a look into 'broken packets', and it's likely where the inital problem i'm seeing is coming about from - Sound familiar?

Re: Insert newline?

Posted: Mon Dec 14, 2015 11:20 pm
by Nyyrazzilyss
While it appears like it -should- work, still not quite - I can't get the newline to display.

if linecheck ~= nil then
linecheck = string.find(matches[2], "> ")+1

if linecheck ~= string.len(matches[2]) then
-- printing on same line as prompt
selectSection(linecheck, string.len(matches[2])-linecheck)
copy()

replace( "\n" )
wrapLine("main", getLastLineNumber("main") )

paste()
end
end

If I comment out the wrapLine command, a control character is printed on the line where the \n would be. If I uncomment that line, the control code is not there, however, it's just pasting the selection back to the original line (it never wraps/inserts the newline)

--edit--

I took a look at TBuffer::wrapLine, and it appears it's supposed to return how many new lines were inserted. With a \n inserted into the line i'm trying to wrap, while it does remove the symbol it also just returns nil (and never inserts a newline)

Re: Insert newline?

Posted: Tue Dec 15, 2015 3:19 am
by Nyyrazzilyss
I just noticed a posting on my github with a working solution for inserting newlines.

if moveCursor(string.find(line, ">")+#(">")-1, getLineNumber()) then
insertText("\n")
end