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

How does one change background color for one line only?

Started by subzero22, 19 September 2016 - 09:06 AM
subzero22 #1
Posted 19 September 2016 - 11:06 AM
Basicly I want to make a progress bar kinda like I've seen in other programs where it's a solid line. As far as I know it's by changing the background color but I can't figure out how to do that for one line only and having 2 colors like that?
Sewbacca #2
Posted 19 September 2016 - 12:02 PM

local function floor (var)
	local decimal = var % 1
	local var = decimal < 0.3 and var - decimal or decimal > 0.7 and math.ceil(var) or var - decimal + 0.5
	return var - var % 1, var % 1 == 0.5 and true or false
end
loadingbar.animate = function (self)
	local half = '\149'
	local nFiled, bHalf = floor(self.percent * self.size * 0.01)
	self.term.setCursorPos(self.cursorPos[1], self.cursorPos[2])
	for i = 1, nFiled do
		self.term.blit(' ', 'f', self.fgCol)
	end
  
	if bHalf then
		term.blit(half, self.fgCol, self.bgCol)
		nFiled = nFiled + 1
	end
	for _ = nFiled + 1, self.size do
		self.term.blit(' ', '0', self.bgCol)
	end
end
createLoadingbar = function (term, x, y, size, fgCol, bgCol)
	return setmetatable({
		term = term or error('term expected'),
		size = size or 5,
		cursorPos = x and y and {x, y} or {term.getCursorPos()},
		fgCol = fgCol or 'f',
		bgCol = bgCol or '0',
		timeout = 0,
		percent = 0
	}, mt)
end

I coded, because i was bored, a (maybe bad coded) progress bar constructor.
It isn't full tested yet, but it should work. Maybe, it inspires you, how to code yourself one.
I hope I could help you =)

Sewbacca
Edited on 19 September 2016 - 05:22 PM
KingofGamesYami #3
Posted 19 September 2016 - 01:24 PM
Change the background color, move the cursor to where you wish to draw a line, and start writing spaces.

Here's a simple example:

term.clear()
term.setBackgroundColor( colors.orange )
term.setCursorPos( 1, 1 )
for i = 1, 10 do
  term.write( " " )
  sleep( 0.1 )
end
subzero22 #4
Posted 19 September 2016 - 05:23 PM
Thanks I got what I needed done. :)/>