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

Colored text in terminal

Started by rockymc, 23 May 2015 - 09:26 PM
rockymc #1
Posted 23 May 2015 - 11:26 PM
Is it possible to color just a part of this string? Like, "white red", color the red word with the red color?
flaghacker #2
Posted 23 May 2015 - 11:37 PM
Sure:

term.write ("white")
term.setTextColor (colors.red)
term.write ("red")
rockymc #3
Posted 23 May 2015 - 11:54 PM
Sure:

term.write ("white")
term.setTextColor (colors.red)
term.write ("red")

Ain't no other way? I have a program that controls my BigReactors reactor, and instead of using:

print("REACTOR TEMPERATURE: "..reactorTemperature)

I use:

print(string.format("REACTOR TEMPERATURE: %d", reactorTemperature))

Hm… I think I should add support for color codes, then I substring these codes and set the terminal color.
Bomb Bloke #4
Posted 24 May 2015 - 12:41 AM
Any particular need to format everything together? Why not just:

term.setTextColor(colours.white)
write("REACTOR TEMPERATURE: "
term.setTextColor(colours.red)
print(tostring(reactorTemperature))

For what it's worth, CC 1.74 (still in beta) adds term.blit(text, textColCodes, bgColCodes), which does allow you to do what you're asking. But in this particular case, that wouldn't simplify things at all!

local line = string.format("REACTOR TEMPERATURE: %d", reactorTemperature)
term.blit(line, string.rep("0", 21) .. string.rep("e", #line - 21), string.rep("f", #line))
rockymc #5
Posted 24 May 2015 - 04:19 AM
I have a program setup that is just a BigReactors reactor monitor. It prints information about the reactor in any monitor attached to the computer.
However, I'm having a problem: the last written line on the monitor flickers. It is weird, all the other lines are working normally, but then the last one flickers.

Here's my code:


monitor.clear()
  monitor.setTextScale(0.5)
  monitor.setCursorPos(1, 1)
 
  monitor.setTextColor(colors.white)
  monitor.write("Reactor: ")
  if reactor.getActive() then
   monitor.setTextColor(colors.green)
   monitor.write("online")
  else
   monitor.setTextColor(colors.red)
   monitor.write("offline")
  end
  monitor.setTextColor(colors.white)
 
  monitor.setCursorPos(1, 2)
  monitor.write("Core Temperature: ")
  monitor.setTextColor(getTempColor(round(reactor.getFuelTemperature(), 2)))
  monitor.write(round(reactor.getFuelTemperature(), 2).." °C")
  monitor.setTextColor(colors.white)
 
  monitor.setCursorPos(1, 3)
  monitor.write("Casing Temperature: ")
  monitor.setTextColor(getTempColor(round(reactor.getCasingTemperature(), 2)))
  monitor.write(round(reactor.getCasingTemperature(), 2).." °C")
  monitor.setTextColor(colors.white)
 
  monitor.setCursorPos(1, 4)
  monitor.write("Energy Buffer: ".. reactor.getEnergyStored().." RF")
 
  monitor.setCursorPos(1, 5)
  monitor.write("Fuel: ")
  monitor.setTextColor(getColorForFuelAmount())
  monitor.write(string.format("%.2f%%", round((reactor.getFuelAmount() / reactor.getFuelAmountMax()) * 100, 2)))
  monitor.setTextColor(colors.white)
 
  monitor.setCursorPos(1, 6)
  monitor.write("Waste: ")
  monitor.write(string.format("%.2f%%", round((reactor.getWasteAmount() / reactor.getFuelAmount()) * 100, 2)))
  monitor.setTextColor(colors.white)
 
  monitor.setCursorPos(1, 7)
  monitor.write(string.format("Fuel Reactivity: %d%%", reactor.getFuelReactivity()))
 
  monitor.setCursorPos(1, 9)
  monitor.write("Capacitor Energy: ")
  monitor.setTextColor(getColorForCapacitorEnergy())
  monitor.write(capacitor.getEnergyStored().." RF")
 
  monitor.setCursorPos(1, 10)
  monitor.write("")
  monitor.setTextColor(colors.white)

Is there something in it which causes the flickering?
Bomb Bloke #6
Posted 24 May 2015 - 04:25 AM
This does not appear to be all of your code.

Presumably your problem is that you're running the above in a loop. You clear the screen, then start redrawing, one line at a time. That redrawing process isn't instant, and the fact that you're calling peripheral functions while you're doing it slows things down further.

Therefore, the amount of time it takes for the script to get around to redrawing the last line after clearing it, happens to be enough to make it noticeable that the line's blank. So it flickers.

One way to reduce the effect is to use a series of monitor.clearLine() calls, one after each monitor.setCursorPos() call, instead of a single monitor.clear() call.
rockymc #7
Posted 24 May 2015 - 03:04 PM
The error message that I get when I run my program is this: reactor:72: 5
This is my code:

local reactorInfo = {
  isActive = false,
  coreTemp = 0.00,
  casingTemp = 0.00,
  energyBuffer = 0,
  fuelAmount = 0,
  maxFuelAmount = 0,
  fuelPercentage = 0.00,
  fuelReactivity = 0,
  wasteAmount = 0.00,
  wastePercentange = 0.00,
  nControlRods = 0,
  controlRods = {
  }
}

local function getReactorInfo()
  reactorInfo.isActive = reactor.getActive()
  reactorInfo.coreTemp = round(reactor.getFuelTemperature(), 2)
  reactorInfo.casingTemp = round(reactor.getCasingTemperature(), 2)
  reactorInfo.energyBuffer = reactor.getEnergyStored()
  reactorInfo.fuelAmount = reactor.getFuelAmount()
  reactorInfo.maxFuelAmount = reactor.getFuelAmount()
  reactorInfo.fuelPercentage = round( (reactorInfo.fuelAmount / reactorInfo.maxFuelAmount) * 100, 2)
  reactorInfo.fuelReactivity = reactor.getFuelReactivity()
  reactorInfo.wasteAmount = reactor.getWasteAmount()
  reactorInfo.wastePercentage = round( (reactorInfo.wasteAmount / reactorInfo.fuelAmount) * 100, 2 )
  reactorInfo.nControlRods = reactor.getNumberOfControlRods()
	if (reactorInfo.nControlRods > 0) then
	  for i=1,reactorInfo.nControlRods do
		reactorInfo.controlRods[i] = reactor.getControlRodLevel(i)
	  end
	end
end

Is there anything wrong with it?

EDIT: I'm using BigReactors.
Edited on 24 May 2015 - 01:40 PM
Creator #8
Posted 24 May 2015 - 04:18 PM
You don't need 0.00, 0 is enough.
KingofGamesYami #9
Posted 24 May 2015 - 06:07 PM
I don't see the round function defined anywhere. I also see less than 72 lines of code, please post the entire code.
Lyqyd #10
Posted 24 May 2015 - 06:43 PM
Threads merged. Please stick to one thread for all questions about a given program. Feel free to edit the topic title to reflect the nature of the current question.

One issue with the snippet of code you posted is that the BigReactors control rods are numbered 0 - n-1, not 1 - n.
rockymc #11
Posted 24 May 2015 - 10:10 PM
This is the round function:

local function round(num, dec)
  local shift = 10^(dec or 2)
  return math.floor(num * shift + 0.5) / shift
end

Found it somewhere here.

@Lyqyd: thanks for merging them.
rockymc #12
Posted 25 May 2015 - 08:39 PM
Okay, I've been rewriting the program, and I came across a problem.

I'm using string formatting to format a big float number to 3 decimal cases. However, it doesn't work, and I even tried using the lua command. Here's what you need to try:


print(string.format("%.3f", 3.14159))

Am I the only one with this problem (formatting doesn't work)?
Lyqyd #13
Posted 25 May 2015 - 10:18 PM
Nope, known bug in LuaJ. You'll have to manually format it, unfortunately. Thankfully, it's pretty easy:


function decimalFormat(num, digits)
  local str = tostring(num)
  if not string.match(str, "%.") then str = str.."." end
  str = str..string.rep("0", digits)
  return string.match(str, "^(%d+%."..string.rep("%d", digits)..")")
end