insertText() function made easy - pecho()

Share your scripts and packages with other Mudlet users.
Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

insertText() function made easy - pecho()

Post by Caled »

Code: Select all

-- pecho() allows easy use of insertText()
function pecho( mess, pos, col, line_offset, writeover)
	local linenum = getLineCount()

	if line_offset ~= nil then 
		linenum = linenum + line_offset 
	end

	moveCursor("main", pos, linenum)

	if writeover ~= nil then
		selectString( mess, 1)
		replace("")
	end

	fg(col) bg("black")
	insertText(mess)
	resetFormat()
	moveCursorEnd("main")
end
That one makes for easy use of insertText(). (The "p" in "pecho" is for "position".)
For example:
pecho( "Column 1", 5, "sea_green")
pecho("Column 2, 20, "red")
pecho("Column 3, 35, "yellow")

The optional line offset could be used when looping through an ordered table with ipairs, listing the values, across multiple columns. Eg, keys 1-20 in column 1, followed by 21-40 in column 2 etc.

I want to add an argument for directing to another window/console, but I'm not sure whether to put that optional argument before or after the line offset. I think the best way to have two optional arguments is to use type() to differentiate between them (since line offset will always be an integer, while windowname will be a string). I'll think about it - it shouldn't be difficult at any rate.

Code: Select all

function cecho(mess, fgcol, bgcol)
	if fgcol ~= nil then fg(fgcol) end
	if bgcol ~= nil then bg(bgcol) end
	echo(mess)
	resetFormat()
end
That one is really simple, and perhaps is sort of pointless, but I hate forgetting to use resetFormat() and the above function does that for me. C is for colour, obviously.

Edit: function updated with "writeover". Explanation in reply to this post.

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: insertText() function made easy - pecho()

Post by Caled »

Writeover
if you include the optional "writeover" argument to pecho() it will overwrite the existing text that is already ad that cursor position, providing that text is the same as the text you are attempting to insert.

I would like to change this so it writes over -any- existing text, but I don't yet know how to do this.

I figure I need to calculate the length of the string I am printing, then select the space between 'pos' and "pos + stringlength", and replace("") that selection. I'll get around to that at some point.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: insertText() function made easy - pecho()

Post by Heiko »

To replace any existing text you could try to use a combination of selectSection(), cursor column position and the length of the text to be inserted.

I'd like to include your set of echo functions to the global Lua API if you don't mind. Could you please add a little documentation for your functions to put in the API section of the manual?

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: insertText() function made easy - pecho()

Post by Caled »

Of course I don't mind, though I don't really think they are 'perfect' enough yet. I'm also wondering whether pecho() should default to the -1 line, or stay as it is. I am finding that I almost always use -1, so perhaps it should default to that and then in those occasional situations when it isn't needed, +1 could be used?

I've been meaning to look in global.lua and see how you do that nifty "send it to another window" thing, so I can duplicate it with these functions. I'm also keen to get the writeover working well. I'll play around with selectSection() and see how it does.

I will write something up for them soon though.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: insertText() function made easy - pecho()

Post by Heiko »

The send to another window functionality is implemented with c++ code as well as all other core functionality.

Post Reply