Jump to content




Colored text in terminal

help

12 replies to this topic

#1 rockymc

  • Members
  • 103 posts

Posted 23 May 2015 - 10:26 PM

Is it possible to color just a part of this string? Like, "white red", color the red word with the red color?

#2 flaghacker

  • Members
  • 655 posts

Posted 23 May 2015 - 10:37 PM

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


#3 rockymc

  • Members
  • 103 posts

Posted 23 May 2015 - 10:54 PM

View Postflaghacker, on 23 May 2015 - 10:37 PM, said:

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.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 23 May 2015 - 11:41 PM

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))


#5 rockymc

  • Members
  • 103 posts

Posted 24 May 2015 - 03: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?

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 24 May 2015 - 03: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.

#7 rockymc

  • Members
  • 103 posts

Posted 24 May 2015 - 02: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 by rockymc, 24 May 2015 - 02:40 PM.


#8 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 24 May 2015 - 03:18 PM

You don't need 0.00, 0 is enough.

#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 24 May 2015 - 05: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.

#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 24 May 2015 - 05: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.

#11 rockymc

  • Members
  • 103 posts

Posted 24 May 2015 - 09: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.

#12 rockymc

  • Members
  • 103 posts

Posted 25 May 2015 - 07: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)?

#13 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 25 May 2015 - 09: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users