This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Geforce Fan's profile picture

Buffer API issues

Started by Geforce Fan, 22 March 2015 - 01:12 AM
Geforce Fan #1
Posted 22 March 2015 - 02:12 AM
I've been working on a buffer API, but it's having issues.
It looks totally messed up and crashes for some reason, and I can't figure out why.

It's decently large(~250 lines) and requires one of my APIs. Run these two commands:
 pastebin run 6JU1nY50 4 update
pastebin get KuZcEbnz buffer

I'm using this code to test it:
shell.run'clear'
os.loadAPI"library/buffer"
local buf = buffer.newBuf(51,19)
buf.makeActive(1,1)
local na = term.current()
term.redirect(buf)
--term.getCursorPos()
term.setCursorBlink(true)
term.setCursorPos(1,1)
--print("Hi")
shell.run'shell'
--sleep(2)
--print"Bye!"
--sleep(2)
--read()
--term.redirect(na)
--b.blit(1,1)


Edit: I've fixed the crashing thanks to Blomb Bloke, but now when I run "help" while running the shell in my buffer, it says,
"bios:150:Redirect object is missing method getSize", even though running term.getSize() in lua is fine.
Edit2: I've fixed this buffer system.
Thanks for your help!
Edited on 25 March 2015 - 01:50 AM
Bomb Bloke #2
Posted 22 March 2015 - 09:09 AM
On line 83, you define b.isColor as term.isColor. You should be building a function to reference self.t.isColor() instead.

On line 116 (in your "write" function), you're adding the length of the string to write to "self.x" - but you haven't actually written that string yet, so when you go to move the cursor in preparation to do said writing (line 137), it's already ending up after where the text should go.
Geforce Fan #3
Posted 22 March 2015 - 06:29 PM
Thanks!
My buffer actually works now!
I'm having more troubles now, reuploading the code, and I've edited the OP
Edited on 22 March 2015 - 07:18 PM
Bomb Bloke #4
Posted 23 March 2015 - 01:20 PM
The error is triggered when the help script calls textutils.pagedPrint(), which attempts to build a temporary redirect object by copying the contents of whatever term.current() returns.

term.current() (which, as far as I'm aware, you can't readily override), returns your actual buffer - you get all the "tracking variables" available "in" your object (eg isActive, lazy, etc), and you don't get the meta-tabled functions available "through" your object (eg getSize, clearLine, write etc).

I'd been wondering why Dan rebuilt and placed new function instances into every "window" object he built. Guess this is why.

So, change this:

    for k,v in pairs(B)/> do
    	if type(v)=="function" then
        	meta[k]=wrap(v,buffer)
    	else
        	meta[k]=v
     	end
   end
   setmetatable(buffer,{__index=meta})

… to something like this:

    for k,v in pairs(B)/> do
    	if type(v)=="function" then
        	buffer[k]=wrap(v,buffer)
    	else
        	buffer[k]=v
     	end
   end
MKlegoman357 #5
Posted 23 March 2015 - 04:45 PM
This looks like a 'bug' of textutils, as Dan didn't account that pairs() only returns the actual values of the table, discarding the __index metamethod, although many people use it to define their custom redirect objects, like we see here.
Lyqyd #6
Posted 23 March 2015 - 06:02 PM
It could be a bug there, or in the custom redirect objects. It depends on what Dan intends the correct definition of a redirect object to be; whether a valid redirect object needs to actually contain function pointers at those keys, or simply needs to allow functions to be indexed from it at certain keys. The textutils code suggests the former, though that is obviously not conclusive, and the term.redirect code only requires the latter (though changing it to use rawget would make it require actual function pointers).