This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
YoShIWoZ's profile picture

place text on monitor defined by the length of the string

Started by YoShIWoZ, 24 April 2014 - 08:40 AM
YoShIWoZ #1
Posted 24 April 2014 - 10:40 AM
Hello fellow computercrafters
I'm back with another roadblock that i'd love to get some assistance with.
I'm quite new to lua, so please forgive if my requets are silly.
So i'm making a program that does energy readings on my capacitor bank (enderio) and submits it to a monitor.
This is the code i'm using to write my data onto the monitor: (If you see something silly, and/or any easier way to do this please let me know)
if id == 5 then --3 is the Id of the main pc
   		 mon.setBackgroundColor(outputBgC)
			mon.setTextColor(outputTxtC)
			mon.setCursorPos(1,4)
			mon.write("			   ")
			mon.setCursorPos(1,4)
			mon.write("Stored:	 "..round(message["energy"]/1000000, 1).."M")
			mon.setCursorPos(1,5)
			mon.write("			   ")
			mon.setCursorPos(1,5)
			mon.write("Max:		"..round(message["energyMax"]/1000000, 1).."M")
			mon.setCursorPos(1,6)
			mon.write("			   ")
			mon.setCursorPos(1,6)
			mon.write("Percent:   "..round(message["energy"]/message["energyMax"]*100, 1).."%")
			mon.setCursorPos(1,7)
			mon.write("			   ")
			mon.setCursorPos(1,8)
			mon.write("			   ")
			mon.setCursorPos(1,9)
			mon.write("			   ")
			mon.setCursorPos(1,10)
			mon.write("			   ")
   	 end
The message["energy"] = getEnergyStored() on the capacitor bank
The message["energyMax"] = getMaxEnergyStored() on the capacitor bank
So the problem is that the "energy" can go up and down and i've made it so it will round it and give it 1 decimal.
So it goes from 90M to ie. 85.7M
Which will make the text go outside of my "Box" where my stats are written.
so my box looks like this everything inside the "box" has a different backgorund color then the rest of the program.


Sorry for my sketchy drawings, hope it makes it clear what i'm trying to do.
What i was thinking is that i could somehow m ake it read the string length in the "message["energy"]" and then set the cursor pos accordingly.
ie.

if message["energy"] string length == 5 then
mon.setCursorPos(7,2)
mon.write(message[energy])
if message["energy"] string legnth == 3 then
mon.setCursorPos(5,2)
mon.write(message[energy])
This is just an example. :)/>
Edited on 24 April 2014 - 08:55 AM
CometWolf #2
Posted 24 April 2014 - 10:53 AM
It's pretty simple, using the # symbol on a string returns the amount of characters. Using this, you can find the writing point to center it easily

term.setCursorPos((termX-#string)/2),1)
Or in your case, something like

term.setCursorPos(edgeOfBox-#string,1)
Edited on 24 April 2014 - 09:06 AM
YoShIWoZ #3
Posted 24 April 2014 - 11:19 AM
It's pretty simple, using the # symbol on a string returns the amount of characters. Using this, you can find the writing point to center it easily

term.setCursorPos((termX-#string)/2),1)
Or in your case, something like

term.setCursorPos(edgeOfBox-#string,1)
Thanks a ton for your help once again CometWolf!
Nifty trick with the # in front of the string to geth it's length!
I had to make a variable where i changed my int into a string to use the #. So my code ended up a bit diferent then your example. But it works now! =)
Here is what i changed it into to get it working, if anyone else should get in this situation:

if id == 5 then --3 is the Id of the main pc
   energyLength = tostring(round(message["energy"]/1000000, 1).."M")
   percentLength = tostring(round(message["energy"]/message["energyMax"]*100, 1).."%")
		 mon.setBackgroundColor(outputBgC)
	  mon.setTextColor(outputTxtC)
	  mon.setCursorPos(1,4)
	  mon.write("			   ")
	  if #energyLength == 3 then
	  mon.setCursorPos(1,4)
	  mon.write("Stored:	 "..round(message["energy"]/1000000, 1).."M")
	  elseif #energyLength == 5 then
	  mon.setCursorPos(1,4)
	  mon.write("Stored:   "..round(message["energy"]/1000000, 1).."M")
	  end
	  mon.setCursorPos(1,5)
	  mon.write("			   ")
	  mon.setCursorPos(1,5)
	  mon.write("Max:		"..round(message["energyMax"]/1000000, 1).."M")
	  mon.setCursorPos(1,6)
	  mon.write("			   ")
	  if #percentLength == 4 then
	  mon.setCursorPos(1,6)
	  mon.write("Percent:   "..round(message["energy"]/message["energyMax"]*100, 1).."%")
	  elseif #percentLength == 5 then
	  mon.setCursorPos(1,6)
	  mon.write("Percent:  "..round(message["energy"]/message["energyMax"]*100, 1).."%")
	  end
	  mon.setCursorPos(1,7)
	  mon.write("			   ")
	  mon.setCursorPos(1,8)
	  mon.write("			   ")
	  mon.setCursorPos(1,9)
	  mon.write("			   ")
	  mon.setCursorPos(1,10)
	  mon.write("			   ")
	 end

I have my number and text on same string, so i can't exactly just do setCursorPos(edge-#variable,y)
I do know though i could make my code more neat by having the energy in it's own mon.write and thereby be able to do the aboive. :)/>
Edited on 24 April 2014 - 09:26 AM