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

Change color on table based on if statement

Started by Dwarfy, 23 January 2015 - 09:23 PM
Dwarfy #1
Posted 23 January 2015 - 10:23 PM
Cant figure out how to change the color of table text, i have if statements turning on and off lights, i want those to change the value of the relating number so 1 is the number 1=Entrance lights for example. so i want the menu item to be displayed in green or red depending on if its lit up or not. anyone know how to do this? i want the text in the table to change to red og green depending on the if statement.



local function addcol(col, side)
  rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side),col))
end

local function remcol(col,side)
  rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side),col))
end

while true do

term.clear()
term.setCursorPos(1,1)

local menu1 = {}

menu1[1] = "What do you want to do?"
menu1[2] = "1=Entrance lights"
menu1[3] = "2=Machineroom lights"
menu1[4] = "3=Tool forge lights"
menu1[5] = "4=Smeltery lights"
menu1[6] = "5=Computer lights"

print(menu1[1])
print(menu1[2])
print(menu1[3])
print(menu1[4])
print(menu1[5])
print(menu1[6])

input = io.read()

state = rs.getBundledOutput("top")

if input == "1" then
  test = colors.test(state, colors.purple)
  if not test then
	addcol(colors.purple,"top")
  else
	remcol(colors.purple,"top")
  end
else
  print("Choose from the menu")
end
if input == "2" then
  test = colors.test(state, colors.red)
  if not test then
	addcol(colors.red,"top")
  else
	remcol(colors.red,"top")
  end
else
  print("Choose from the menu")
end
if input == "3" then
  test = colors.test(state, colors.green)
  if not test then
	addcol(colors.green,"top")
  else
	remcol(colors.green,"top")
  end
else
  print("Choose from the menu")
end
if input == "4" then
  test = colors.test(state, colors.brown)
  if not test then
	addcol(colors.brown,"top")
  else
	remcol(colors.brown,"top")
  end
else
  print("Choose from the menu")
end
if input == "5" then
  test = colors.test(state, colors.blue)
  if not test then
	addcol(colors.blue,"top")
  else
	remcol(colors.blue,"top")
  end
else
  print("Choose from the menu")
end
end

Here is an example i made just using photoshop of how i want it, either the text to be green and red or a box behinde it. thanks :)/>

Quintuple Agent #2
Posted 23 January 2015 - 11:53 PM
Ok, Had to do a little bit of a code rewrite to make it work and also clean up a few things
Spoiler

local function addcol(col, side)
  rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side),col))
end
local function remcol(col,side)
  rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side),col))
end
local menu1 = {  --color# will be whatever color matching the menu number.
["topMsg"]="What do you want to do?",   --topMsg will be whatever is displayed at the top
"1=Entrance lights", ["color1"]=colors.purple,  --You can do [1]="1=Entrance lights" if you wanted, but it is unneeded.
"2=Machineroom lights", ["color2"]=colors.red,
"3=Tool forge lights", ["color3"]=colors.green,
"4=Smeltery lights", ["color4"]=colors.brown,
"5=Computer lights", ["color5"]=colors.blue
}

while true do
term.clear()
term.setCursorPos(1,1)
state = rs.getBundledOutput("top")
term.setTextColor(colors.white)
print(menu1["topMsg"]) --Print the top message
for i,msg in ipairs(menu1) do --print all the messages in the list, ipairs only returns table indexes that are numbers
if colors.test(state, menu1["color"..i]) then term.setTextColor(colors.green) else term.setTextColor(colors.red) end   --If the redstone signal is on it will be green,else red
print(msg)
end
term.setTextColor(colors.white)
input = io.read()
input=tonumber(input)   --turn the user input into a number
if input~=nil and input>=1 and input<=#menu1 then  --if what was entered was a number and it is between the amount of options in the table run the test
  test = colors.test(state, menu1["color"..input])
  if not test then
		addcol(menu1["color"..input],"top")
  else
		remcol(menu1["color"..input],"top")
  end
else
  print("Choose from the menu")
  sleep(1)
end

end

I have not got a chance to fully test it, but I did do a little bit of testing and everything seems to work.

Edit: I think I just thought of another way that would make it even simpler
Edited on 23 January 2015 - 10:59 PM
Quintuple Agent #3
Posted 24 January 2015 - 12:01 AM
Ok, little bit better code
Spoiler

local function addcol(col, side)
  rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side),col))
end
local function remcol(col,side)
  rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side),col))
end
local menu1 = {
["topMsg"]="What do you want to do?",  --Message to be displayed at the top
{"1=Entrance lights",colors.purple},  --First is the message, second is the color being checked
{"2=Machineroom lights",colors.red},
{"3=Tool forge lights",colors.green},
{"4=Smeltery lights",colors.brown},
{"5=Computer lights",colors.blue}
}

while true do
term.clear()
term.setCursorPos(1,1)
state = rs.getBundledOutput("top")
term.setTextColor(colors.white)
print(menu1["topMsg"])	  --Print the top message
for i,msg in ipairs(menu1) do	--print each message in menu1, ipairs only returns table indexes that are numbers
if colors.test(state, msg[2]) then term.setTextColor(colors.green) else term.setTextColor(colors.red) end  --If the color is active then print green, else print red
print(msg[1])
end
term.setTextColor(colors.white)
input = io.read()
input=tonumber(input)	  --Convert the input to a number

if input~=nil and input>=1 and input<=#menu1 then  --if what was entered was a number and it is between the amount of options in the table run the test
  test = colors.test(state, menu1[input][2])
  if not test then
		addcol(menu1[input][2],"top")
  else
		remcol(menu1[input][2],"top")
  end
else
  print("Choose from the menu")
  sleep(1)
end

end

Edit: Forgot to take out the commented code I had

And if you wanted boxes instead, just change all the setTextColor to setBackgroundColor, but where it says setTextColor(colors.white) change that to colors.black
Edited on 23 January 2015 - 11:07 PM
Dwarfy #4
Posted 24 January 2015 - 12:12 AM
Thank you so so so so so much, mate! Been looking for this all day! Going to study your code as much as i can and try to understand everything, first day with lua :)/> but i think i understand most of it :)/>
Quintuple Agent #5
Posted 24 January 2015 - 12:22 AM
Ok, happy to help. if you have any questions feel free to ask.
Edited on 23 January 2015 - 11:23 PM
Dwarfy #6
Posted 24 January 2015 - 12:25 AM
Ok, happy to help. if you have any questions feel free to ask.

Where would i go about adding a break in there like adding another table entry saying press Q to exit or something, would this now work as the input is beeing set to number ?
Quintuple Agent #7
Posted 24 January 2015 - 12:56 AM
Ahhh, you broke everything!

I or someone else here might think up something.

Edit: This should do the trick

Spoiler

local function addcol(col, side)
  rs.setBundledOutput(side, colors.combine(rs.getBundledOutput(side),col))
end
local function remcol(col,side)
  rs.setBundledOutput(side, colors.subtract(rs.getBundledOutput(side),col))
end
local menu1 = {
["topMsg"]="What do you want to do?",  --Message to be displayed at the top
{"1=Entrance lights",colors.purple},  --First is the message, second is the color being checked
{"2=Machineroom lights",colors.red},
{"3=Tool forge lights",colors.green},
{"4=Smeltery lights",colors.brown},
{"5=Computer lights",colors.blue}
["backMsg"]="Q=Go Back"
}

while true do
term.clear()
term.setCursorPos(1,1)
state = rs.getBundledOutput("top")
term.setTextColor(colors.white)
print(menu1["topMsg"])	  --Print the top message
for i,msg in ipairs(menu1) do	--print each message in menu1, ipairs only returns table indexes that are numbers
if colors.test(state, msg[2]) then term.setTextColor(colors.green) else term.setTextColor(colors.red) end  --If the color is active then print green, else print red
print(msg[1])
end
term.setTextColor(colors.white)
print(menu1["backMsg"])	  --Print the back message
input = io.read()
local input=string.lower(input)	   --Make everything lowercase
local newInput=tonumber(input)	  --Convert the input to a number

if newInput~=nil and newInput>=1 and newInput<=#menu1 then  --if what was entered was a number and it is between the amount of options in the table run the test
  test = colors.test(state, menu1[newInput][2])
  if not test then
		addcol(menu1[newInput][2],"top")
  else
		remcol(menu1[newInput][2],"top")
  end
elseif input=="q" then
--GoBack!
else
  print("Choose from the menu")
  sleep(1)
end
end
Edited on 24 January 2015 - 12:04 AM