Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
jaspercayne

Problems centering text

Question

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?

Link to post
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

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.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.