Posted 09 September 2012 - 05:22 AM
local maxX, maxY = term.getSize()
maxX = maxX-2
function elementOf(table, object)
for index,value in pairs(table) do
if object == value then
return true
end
end
return false
end
local wordSeparators = {" ", "/", "\\", "-"}
function drawPage(sPage)
local lines = {}
local function iterChars(string)
return function(s, c)
c = c+1
local char = string.sub(string, c, c)
if #char == 0 then
char = nil
return nil
else
char = nil
return c, string.sub(c, c)
end
end, string, 0
end
local bufferLine = ""
local bufferWord = ""
for pos, char in iterChars(sPage) do
local len = string.len(bufferLine)+string.len(bufferWord)+1
if len >= maxX then
table.insert(lines, bufferLine)
bufferLine = ""
end
if elementOf(wordSeparators, char) then
bufferLine = bufferLine..bufferWord..char
elseif char == "\n" then
bufferLine = bufferLine..bufferWord
table.insert(lines, bufferLine)
bufferLine = ""
else
bufferWord = bufferWord..char
end
end
local function redraw(startLine)
local drawtext = table.concat(lines, "\n", startLine, startLine+maxY-1)
term.clear()
term.setCursorPos(1, 1)
write(drawtext)
end
local cursorPos = 1
redraw(cursorPos)
while true do
local event, key = os.pullEvent("key")
if key == keys.up and cursorPos > 1 then
cursorPos = cursorPos-1
redraw(cursorPos)
elseif key == keys.down and cursorPos < (#lines-maxY+1) then
cursorPos = cursorPos+1
redraw(cursorPos)
end
end
end
drawPage([[<snip long lorem ipsum>]])
I have no idea why this is broken… As far as I know the only string that table.concat accepts is the separator and I explicitly declared that.