I am having issues with a text API that I am writing and am looking for input from more experienced coders. The problem I am currently facing is with my centering method. If be used to center text to the full screen width it works great, if I am trying to center text in a smaller portion of the screen that is offset towards either edge, it fails miserably. API-in-progress below:
--[[
Text utilities API
by jaspercayne
A collection of functions to work with text
]]--
local textTools = {}
local component = require("component")
local term = require("term")
local text = require("text")
local gpu = component.gpu
local screenWidth, screenHeight = gpu.getResolution()
-- Calculates appropriate amount of padding to center a string
function textTools.centerText(target, fromx, tox)
local calculatedPadding = ((fromx+tox)/2) + (string.len(target)/2)
local centeredText = tostring(text.padLeft(target, calculatedPadding))
term.write(centeredText)
end
-- Aligns a string to the right of the screen
function textTools.alignRight(target)
local calculatedPadding = screenWidth -- string.len(target)
local alignedText = tostring(text.padLeft(target, calculatedPadding))
term.write(alignedText)
end
-- Wraps a given string to the width of the screen
function textTools.wrapToScreen(target)
local wrappedText = tostring(text.wrap(target,string.len(target),screenWidth))
term.write(wrappedText)
end
return textTools
I am certain that the main issue is from trying to pull the absolute x,y coords and derive a center point from that, but my brain just isn't putting together what the centerText function should look like... Help? Anyone?
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
I am having issues with a text API that I am writing and am looking for input from more experienced coders. The problem I am currently facing is with my centering method. If be used to center text to the full screen width it works great, if I am trying to center text in a smaller portion of the screen that is offset towards either edge, it fails miserably. API-in-progress below:
I am certain that the main issue is from trying to pull the absolute x,y coords and derive a center point from that, but my brain just isn't putting together what the centerText function should look like... Help? Anyone?
Link to post
Share on other sites