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

screen logging API

Started by louitzie, 11 May 2012 - 09:13 PM
louitzie #1
Posted 11 May 2012 - 11:13 PM
here is the code


local native = term.native
local device = {}
local screen = {}
local w,h=native.getSize()

local function scroll(n)
if n>0 then
for i=1,n do
for j=1,h-1 do
screen[j]=screen[j+1]
end
screen[h]=string.rep(" ",w)
end
elseif n<0 then
for i=1,math.abs(n) do
screen[h]=string.rep(" ",w)
for j=2,h do
screen[j+1]=screen[j]
end
end
end
end

device.write = function (text)
local x,y=native.getCursorPos()
if screen[y]==nil then
screen[y]=string.rep(" ",w)
end
str=string.sub(screen[y],1,x-1)..text..string.sub(screen[y],x+string.len(text))
if y>h then
scroll(1)
y=h
end
screen[y]=string.sub(str,1,w)
while string.len(str)>w do
y=y+1
if y>h then
scroll(1)
y=h
end
str=string.sub(str,w+1)
screen[y]=string.sub(str,1,w)
end
native.write(text)
end

device.clear = function ()
for i=1,h do
screen[i]=string.rep(" ",w)
end
native.clear()
end

device.clearLine = function ()
native.clearLine()
end

device.getCursorPos = function ()
return native.getCursorPos()
end

device.setCursorPos = function (x,y)
native.setCursorPos(x,y)
end

device.setCursorBlink = function (:)/>/>
native.setCursorBlink(:)/>/>
end

device.getSize = function ()
return native.getSize()
end

device.scroll = function (n)
scroll(n)
native.scroll(n)
end

function getLogger()
return device
end

function set(obj)
native=obj
w,h=native.getSize()
end

function getLine(line)
return screen[line] or string.rep(" ",w)
end

function save(file)
f=io.open(file,"w")
for i=1,h do
f:write((getLine(i) or string.rep(" ",w)).."\n")
end
f:close()
end

to start type in lua or other program

os.loadAPI("filename")
term.redirect(filename.getLogger())

you can also use set function to insert monitor handle to log al whats on a monitor

example:

filename.set(peripheral.wrap("right")) //monitor on the right side
Blocky_Balboa #2
Posted 04 August 2012 - 10:33 PM
This looks great! i've been looking for something exactly like this for a server :P/>/>
Thanks. you just saved me a headache:)
wilcomega #3
Posted 07 August 2012 - 02:46 PM
very nice, on the wiki it says that it only works for monitors. but i would be nice if you could explain whats going on. is it like redirecting the print function or something?
wilcomega #4
Posted 08 August 2012 - 08:36 PM
i am going to make teamviewer lol