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

Reduce setTextColour() into a function like text(textcolour, text)?

Started by Fero, 21 May 2013 - 04:46 AM
Fero #1
Posted 21 May 2013 - 06:46 AM
Trying to make this code smoother. All it is for is to display information on an Advance Monitor.

Currently I have four functions in which the only purpose of them is to change the Font Colour. These four functions take up 10 lines of code each and isn't easy for others to read how to change the font without to much fuss.

Sorry for any terminology I don't use, I made this script from looking at other peoples examples and codes from different projects.

Currently text is displayed on the montor via the calls like:

header1("text to print on monitor") <– Pink centered text
header2("text to print on monitor") <– Purple centered text
text("text to print on monitor") <– White centered text
warn("text to print on monitor") <– Red centered text



Ideally, I would like to be able to use words to use to change the font colours: For example:

text("header1","text to print on monitor")
text("warn","text to print on monitor")

Where these words are defined at the start of the code:

header1 = <whatever code is needed here>
warn = <whatever code is needed here>

- The Code -

local mon = peripheral.wrap("back")

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

–Function: Center Text–
local text = function(text)
local maxw, maxh = mon.getSize()
local curx, cury = mon.getCursorPos()
mon.setTextColour(1)
mon.setCursorPos((maxw-#text)/2,cury)
mon.write(text)
mon.setCursorPos(curx,cury+1)
end
–Function: End–

–Function: Center Main Header Text–
local header1 = function(text)
local maxw, maxh = mon.getSize()
local curx, cury = mon.getCursorPos()
mon.setTextColour(64)
mon.setCursorPos((maxw-#text)/2,cury)
mon.write(text)
mon.setCursorPos(curx,cury+1)
end
–Function: End–

–Function: Center Main Header Text–
local header2 = function(text)
local maxw, maxh = mon.getSize()
local curx, cury = mon.getCursorPos()
mon.setTextColour(1024)
mon.setCursorPos((maxw-#text)/2,cury)
mon.write(text)
mon.setCursorPos(curx,cury+1)
end
–Function: End–

–Function: Center Text–
local text = function(text)
local maxw, maxh = mon.getSize()
local curx, cury = mon.getCursorPos()
mon.setTextColour(1)
mon.setCursorPos((maxw-#text)/2,cury)
mon.write(text)
mon.setCursorPos(curx,cury+1)
end
–Function: End–

–Function: Center Warning Text–
local warn = function(text)
local maxw, maxh = mon.getSize()
local curx, cury = mon.getCursorPos()
mon.setTextColour(16384)
mon.setCursorPos((maxw-#text)/2,cury)
mon.write(text)
mon.setCursorPos(curx,cury+1)
end
–Function: End–


warn("PLEASE READ")
text("")
header1("This is important information for all staff")
Lyqyd #2
Posted 21 May 2013 - 02:59 PM
Split into new topic.


header1 = colors.red
text = colors.white

function printText(col, text)
  term.setTextColor(col)
  print(text)
end

printText(header1, "Text goes here")
printText(text, "Body text goes here")