Last active
January 2, 2024 22:24
-
-
Save GammaGames/18005266b028843675d20728d3e5da44 to your computer and use it in GitHub Desktop.
Paginate helper library in Lua (for the Playdate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Paginate = {} | |
function Paginate.read(filename) | |
local lines = {} | |
local file = File.open(filename, File.kFileRead) | |
local line = file:readline() | |
while line do | |
table.insert(lines, line) | |
line = file:readline() | |
end | |
file:close() | |
return lines | |
end | |
function Paginate.wrap(lines, width) | |
local font = Graphics.getFont() | |
local spaceWidth = font:getTextWidth(" ") | |
local result = {} | |
for _, line in ipairs(lines) do | |
local currentWidth = 0 | |
local currentLine = "" | |
if line == "" then | |
table.insert(result, line) | |
else | |
for word in line:gmatch("%S+") do | |
local wordWidth = font:getTextWidth(word) | |
if currentWidth == 0 then | |
currentWidth = wordWidth | |
currentLine = word | |
else | |
local newLine = currentLine .. " " .. word | |
local newWidth = font:getTextWidth(newLine) | |
if newWidth >= width then | |
table.insert(result, currentLine) | |
currentWidth = wordWidth | |
currentLine = word | |
else | |
currentWidth += newWidth | |
currentLine = newLine | |
end | |
end | |
end | |
if currentWidth ~= 0 then | |
table.insert(result, currentLine) | |
end | |
end | |
end | |
return result | |
end | |
function Paginate.window(text, start_index, rows) | |
local result = {} | |
for index = 0, rows do | |
if start_index + index > #text then | |
break | |
end | |
table.insert(result, text[start_index + index]) | |
end | |
return table.concat(result, "\n") | |
end | |
function Paginate.paginate(lines, rows) | |
local result = {} | |
local currentLine = {} | |
for _, line in ipairs(lines) do | |
if line == "" then | |
if #currentLine ~= 0 then | |
table.insert(result, table.concat(currentLine, "\n")) | |
currentLine = {} | |
end | |
else | |
if #currentLine >= rows then | |
table.insert(result, table.concat(currentLine, "\n")) | |
currentLine = {line} | |
else | |
table.insert(currentLine, line) | |
end | |
end | |
end | |
if #currentLine ~= 0 then | |
table.insert(result, table.concat(currentLine, "\n")) | |
currentLine = {} | |
end | |
return result | |
end | |
function Paginate.process(text, width, height) | |
local lines = {} | |
for line in text:gmatch("[^\r\n]+") do | |
table.insert(lines, line) | |
end | |
local wrapped = Paginate.wrap(lines, width) | |
return Paginate.paginate(wrapped, Paginate.getRows(height)) | |
end | |
function Paginate.getRows(height) | |
local font = Graphics.getFont() | |
local leading = font:getLeading() | |
return height // (font:getHeight() + leading) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment