Insert newline?

Post Reply
Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Insert newline?

Post 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?

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Insert newline?

Post 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?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Insert newline?

Post by Vadi »

I think you need to call wrapLine() after you've inserted newlines into the buffer for it to get recognised.

Xavious
Posts: 7
Joined: Sat Nov 21, 2015 9:48 pm

Re: Insert newline?

Post 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!

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Insert newline?

Post 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?

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Insert newline?

Post 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)

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Insert newline?

Post 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

Post Reply