print("This colour is " .. term.setTextColour(colours.red) .. "red and this is " .. term.setTextColour(colours.yellow) .. "yellow.")
Changing colours of text in a single print/write?
Started by remiX, Nov 20 2012 09:51 AM
3 replies to this topic
#1
Posted 20 November 2012 - 09:51 AM
Is it possible without separate prints/writes?
#2
Posted 20 November 2012 - 10:16 AM
Technically yes, but not by default. I actually made a function like this for one of my programs a while ago. The concept of it is pretty simple, a function that can be called with a variable number of arguments, and it has a local curColor variable. When going through the arguments, if the current arg is a color, it sets curColor to that color, and if it's a string, it writes the string.
So then you could do
local function colorPrint(...)
local curColor
for i=1, #arg do -- arg is ...
if type(arg[i]) == 'number' then
curColor = arg[i]
else
if curColor then
term.setTextColor(curColor)
end
write(arg[i])
end
end
print() -- this is a print function, so it needs a new line.
end
So then you could do
colorPrint(colors.lightBlue, 'This is light blue, ', colors.yellow, 'and this is yellow.')
#3
Posted 20 November 2012 - 10:44 AM
That's awesome, thanks
/> I tried for quite a while to make a function like this
/> Is there a way you could use the function like this:
colorPrint(lightBlue, 'This is light blue, ', yellow, 'and this is yellow.')so you don't have to type colours. before the colour?
#4
Posted 20 November 2012 - 12:19 PM
Well, the most I can do without much effort is have you put the colors in quotes.
Here's the new function.
However it REQUIRES that every odd argument is a color name. So you can't do this:
You could also use the first function, while basically redefining every color as a global variable
or do the above using _G
But I'm definitely overthinking this ahahaha
colorPrint('lightBlue', 'This is light blue, ', 'yellow', 'and this is yellow.')
Here's the new function.
local function colorPrint(...)
for i=1, #arg do
if i%2 == 1 then
term.setTextColor(arg[i])
else
write(arg[i])
end
end
print() -- this is a print function, so it needs a new line.
end
However it REQUIRES that every odd argument is a color name. So you can't do this:
colorPrint('Some text', 'red', 'lightBlue')
You could also use the first function, while basically redefining every color as a global variable
local lightBlue = colors.lightBlue local red = colors.red local yellow = colors.yellow
or do the above using _G
for i, v in pairs(colors) do
if type(v) == 'number' then
_G[v] = v
end
end
But I'm definitely overthinking this ahahaha
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











