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

Gaa's Useful Functions

Started by xgaarocha, 23 February 2012 - 02:07 AM
xgaarocha #1
Posted 23 February 2012 - 03:07 AM
[left]
This is a repository with all of the functions me (xgaarocha) and my friend (bravejuliano) created and are useful outside of our in-development Operational System. Bear in mind we have a "If it works it's good for now" policy currently, which means we will make it work first, finish the OS then optimize things. We don't guarantee 100% security and stability of our functions.



> Functions List_[/left]
  1. Delay
  2. Loading/Progress Bar



[left]Delay[/left]
[left]Function: Creates an interval with seconds/minutes/hours and can show a countdown.[/left]
[left]Usage: interval(s, m, h, tleft) (Eg.: interval (0, 2, 0, "yes") > Shows a countdown of 2 minutes)[/left]
[left]Parameters: s,m,h = seconds, minutes, hours / rleft = "yes" enables coutdown, leave blank or "no" to disable it.[/left]
Spoiler

function delay(s, m, h, tleft)
-- Define parameters and vars
s = s or 1
m = m or 0
h = h or 0
tleft = tleft or "no"
delay = s + m*60 + h*3600

-- Check for negative numbers
if s < 0 then error("Delay time can only be positive") end
if m < 0 then error("Delay time can only be positive") end
if h < 0 then error("Delay time can only be positive") end
if s+m+h == 0 then error("Delay time can only be positive") end
-- Delay without time left
if tleft == "no" then
sleep(delay)
end
-- Delay with time left
if tleft == "yes" then
while delay ~= 0 do
h = math.floor(delay/3600)
m = math.floor(delay/60 - h*60)
s = math.floor(delay -h*3600 -m*60)
print("[", h, ":", m, ":", s, "]")
sleep(1)
delay = delay - 1
term.clear()
term.setCursorPos( 1, 1 )
end
end
end


[left]Loading/Progress Bar
Function: Creates a bar that fills-up in a determined amount of time.[/left]
Usage: loadbar(s, char, size) (Eg.: loadbar(10, #, 10, "yes") > Creates a bar that fills up in 10 seconds with 10 #)
Parameters: s = duration in seconds / char = the character the bar is made of / size = bar's size, leave blank for auto


Spoiler

function loadbar(s, char, size)
	-- Define parameters
	s = s or 5
		size = size or swidth
	char = char or "#"
	local xpos,ypos = term.getCursorPos()
	local delay = s/size
	local start = 1
	while start < size  do
	term.setCursorPos(start, ypos)
	print (char)
	sleep(delay)
	start = start + 1
	end
end
xgaarocha #2
Posted 26 February 2012 - 06:59 AM
250 view an 0 replies? Come on :)/>/>
If you have any suggestions on how to improve these functions just tell '-'
vvenaya #3
Posted 26 February 2012 - 07:43 AM
nice coding xgaarocha. well done.

one tip:

if s<0 then error("something")

can be rewritten as:

assert(s>=0,"something")
TheNotch3000 #4
Posted 26 February 2012 - 09:44 AM
looks nice! I will surely use this. :D/>/>
Bladedemon70 #5
Posted 26 February 2012 - 09:04 PM
Sorry for the noob question but for the loadbar do you replace all the paramater in the () or which ones do you need to replace

Nevermind i got it
xgaarocha #6
Posted 29 February 2012 - 05:28 PM
nice coding xgaarocha. well done.

one tip:

if s<0 then error("something")

can be rewritten as:

assert(s>=0,"something")

Oh, nice :unsure:/>/>/> Thanks