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

Formatting / aligning text and objects on screen.

Question

Forgive my lack of competence with lua and programming in general.

I am trying to display information about my power systems ** in the centre of a 5x1 screen and I am struggling to achieve this without simply setting the text at a position on the screen, and I would much rather have the centre of the text string centered on 'width/2' and 'height/2' as the power level updates every 0.1 second and the string is different lengths in each update.

**

while true do
	
	energy = bank.getEnergyStored(4)
	capacity = bank.getMaxEnergyStored(4)
	
	percentcalc = (energy / capacity) * 100

	digits = 1
	shift = 10 ^ digits
	percent = math.floor(percentcalc*shift + 0.5) / shift

	term.clear()

	print("Power = "..energy.."/"..capacity.." (RF) - "..percent.."%"

	os.sleep(0.1)

end

Thanks for any help

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 1

You can use GPU component to get current screen resolution and use gpu.set() to center text on the screen.

For example:

local com = require('component')
local gpu = com.gpu

local width, height = gpu.getResolution()
local text = "Some info to be centered"

gpu.set(width / 2 - #text / 2, height / 2, text)

 

Link to post
Share on other sites

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.