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

width and heigth

Started by ReBraLaCC, 22 September 2016 - 01:47 PM
ReBraLaCC #1
Posted 22 September 2016 - 03:47 PM
couldn't there be some function added like term.getWidth and term.getHeigth, i know that there is term.getSize()

but it would be easier to do it like this

string.rep("-",term.getWidth)

than to do it like


w,h = term.getSize()
string.rep("-",w)
Edited on 22 September 2016 - 01:48 PM
Lignum #2
Posted 22 September 2016 - 04:05 PM
You can do this:

local width = ({term.getSize()})[1]
local height = ({term.getSize()})[2]
However, that's not a very pleasant solution.

Keep in mind that if you pass term.getSize() as an argument, that is the same as passing both of its return values consecutively. Therefore:

string.rep("-", term.getSize())
will work, because that's the same as doing:

string.rep("-", ({term.getSize()})[1], ({term.getSize()})[2]) --# third argument is ignored, since there's no parameter for it

Though I agree that the introduction of term.getWidth and term.getHeight would make code like this more readable.
Lupus590 #3
Posted 22 September 2016 - 04:20 PM
or you can do this


local width, height = term.getSize()
--# or
local width = term.getSize()
local _, height =  term.getSize()

Using these tricks you can make your own getWidth and getHeight functions. Also, if it's possible for the end user (us) to do then it usually won't be made by dan200
SquidDev #4
Posted 22 September 2016 - 04:48 PM
Or of course:

local width = (term.getSize()) -- Limits it to one value to get width
local height = select(2, term.getSize()) -- Remove the 1st item in the vararg: so 51, 19 becomes 19.
Though separate methods would be nicer.
ReBraLaCC #5
Posted 22 September 2016 - 06:15 PM
You can do this:

local width = ({term.getSize()})[1]
local height = ({term.getSize()})[2]

I tried todo that and it didnt work…. hmm (version 1.79pr3?)
KingofGamesYami #6
Posted 22 September 2016 - 06:39 PM
That example should work on any version of Lua including LuaJ and any version of ComputerCraft. However, if you don't add the parenthesis it won't work.
Lyqyd #7
Posted 22 September 2016 - 07:49 PM
Personally, I don't see much value in adding another two functions just to independently get information that's already available as the first and second return values of an existing function. Should we also add getCursorX and getCursorY?
ebernerd #8
Posted 22 September 2016 - 07:56 PM
Personally, I don't see much value in adding another two functions just to independently get information that's already available as the first and second return values of an existing function. Should we also add getCursorX and getCursorY?

I mean, I may be speaking from ignorance here, but isn't that more efficient if you're only looking for one value? Why have the program search for two values when you only want one?

Albeit, in CC the performance difference is literally nothing, so I don't see an immediate reason to add it