Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Created June 11, 2016 18:08
Show Gist options
  • Save thomasjslone/d7f65c7ab8f89c892abae99e56f73558 to your computer and use it in GitHub Desktop.
Save thomasjslone/d7f65c7ab8f89c892abae99e56f73558 to your computer and use it in GitHub Desktop.
Basic Ruby Terminal Animations, write or print over previously written lines.
@state1 = "O"
@state2 = " "
@canvas = @state1.to_s
@visable = true
@blink = 0.250
blinker = Thread.new { loop do
if @visable
@canvas = @state2.to_s
@visable = false
else
@canvas = @state1.to_s
@visable = true
end
sleep @blink.to_f
end
}
loop do
if @visable
l = " "
else
l = ""
end
puts " State: " + @visable.to_s + l.to_s + " Rate: " + @blink.to_s + " seconds per cycle"
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep @blink.to_f
end
@states = ["A","B","C","D","E","F","G","H"]
@canvas = " "
@index = 0
@index_l = @states.length.to_i - 1
@blink = 0.250
@direction = 0
blinker = Thread.new { loop do
@canvas = @states[@index].to_s
if @direction == 0
if @index.to_i == @index_l.to_i
@index = 0
else
@index += 1
end
elsif @direction == 1
if @index.to_i == 0
@index = @index_l.to_i
else
@index -= 1
end
end
sleep @blink.to_f
end
}
loop do
if @index.to_s.length == 1
l = " "
elsif @index.to_s.length == 2
l = " "
else
l = ""
end
puts " State: " + @index.to_s + l.to_s + " Rate: " + @blink.to_s + " seconds per cycle"
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep @blink.to_f
end
@perc = 0
t = Thread.new { loop do ; @perc += 1 ; sleep 0.050 ; end }
until @perc == 100
puts "Downloading: (" + @perc.to_s + "%)"
print "\r\e[A"
sleep 0.050
end
t.kill
puts "Downloading: (100%)"
print "\r\e[A"
puts "\nDownload Complete"
@perc = 0
t = Thread.new { loop do ; @perc += 1 ; sleep 0.050 ; end }
until @perc == 100
puts "Downloading: (" + @perc.to_s + "%)"
print "\r\e[A"
sleep 0.050
end
t.kill
puts "Downloading: (100%)"
print "\r\e[A"
puts "\nDownload Complete"
@perc = 0
t = Thread.new { loop do ; @perc += 1 ; sleep 0.050 ; end }
until @perc == 100
puts "Downloading: (" + @perc.to_s + "%)"
print "\r\e[A"
sleep 0.050
end
t.kill
puts "Downloading: (100%)"
print "\r\e[A"
puts "\nDownload Complete"
puts "Press enter to exit."
gets
@wrapper = "|"
@dot = "O"
@space = " "
@width = 25
@progress = 0
@growth_rath = 0.2
@grid = ""
@width.times { @grid << " " }
@canvas = @wrapper.to_s + @grid.to_s + @wrapper.to_s
thread = Thread.new { loop do
if @progress.to_i <= @width.to_i
d = @grid.to_s.split("")
d[@progress] = @dot.to_s
@grid = d.join("").to_s
@canvas = @wrapper.to_s + @grid.to_s + @wrapper.to_s
@progress += 1
else
break
end
sleep 0.2
end
}
loop do
puts " Progress: " + "(" + @progress.to_s + "/" + @width.to_s + ")"
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep 0.2
end
@str = ""
@width = 25
@space = " "
@pixel = "O"
@wrapper = "|"
@index_l = @width - 1
@spacer = ""
@direction = 0
@clock = 0.025 ## 40 fps
@width.times { @spacer << @space.to_s}
@i = 0
thread = Thread.new {
loop do
s = @spacer.split("")
s[@i.to_i] = @pixel.to_s
if @direction == 0
if @i.to_i == 0
ri = @index_l.to_i
else
ri = @i - 1
end
elsif @direction == 1
if @i.to_i == @index_l.to_i
ri = 0
else
ri = @i + 1
end
end
s[ri.to_i] = @space.to_s
@spacer = s.join("").to_s
@canvas = @wrapper.to_s + @spacer.to_s + @wrapper.to_s
if @direction == 0
if @i.to_i == @index_l.to_i
@i = @index_l.to_i - 1
@direction = 1
else
@i += 1
end
elsif @direction == 1
if @i.to_i == 0
@i = 1
@direction = 0
else
@i -= 1
end
end
sleep @clock.to_f
end
}
loop do
if @i.to_s.length == 1
pl = @i.to_s + " "
else
pl = @i.to_s + " "
end
puts " Pos:" + pl.to_s
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep @clock.to_f
end
@wrapper = "|"
@width = 25
@width_l = @width - 1
@spacer = ""
@space = " "
@dot = "O"
@width.times { @spacer << @space.to_s }
@pos = 0
@delay = 0.025
@canvas = @wrapper.to_s + @spacer.to_s + @wrapper.to_s
t = Thread.new { loop do
s = @spacer.split("")
s[@pos.to_i] = @dot.to_s
if @pos.to_i == 0
ri = @width_l.to_i
else
ri = @pos - 1
end
s[ri.to_i] = @space.to_s
@spacer = s.join("").to_s
@canvas = @wrapper.to_s + @spacer.to_s + @wrapper.to_s
if @pos.to_i == @width_l.to_i
@pos = 0
else
@pos += 1
end
sleep @delay.to_f
end
}
thread2 = Thread.new { loop do
if @pos.to_s.length == 1
pl = @pos.to_s + " "
elsif @pos.to_s.length == 2
pl = @pos.to_s + " "
else
pl = @pos.to_s
end
puts " Pos:" + pl.to_s
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep @delay.to_f
end
}
loop do ## this loop will keep the consol from closing when this file is clicked
end
@wrapper = "|"
@width = 25
@width_l = @width - 1
@delay = 20
@fps = 0.050
@direction = 0
@str = "Hello World, I am Ruby and I can do lots! I can display animations in the consol, I can edit files and do math and make system calls. My oh my what can't I do. Compilemy code to cpu specific bytecodes and create an exe? I can do that too, I can write files with text but I am also byte wise which means you could program me to encode mp3 files in pure ruby, if you had the math guts. I have been talking about my self long enough, its time to reset the marqee."
@spacer = ""
@space = " "
@delay_spacer = ""
@delay.times { @delay_spacer << @space }
@vspacer = @spacer.to_s + @delay_spacer + @str.to_s
@width.times { @spacer << @space.to_s}
thread = Thread.new { loop do
s = @vspacer.split("")
if @direction == 0
m = s[1..-1].join("").to_s
m << s[0].to_s
elsif @direction == 1
m = s[-1].to_s
m << s[0..-2].join("").to_s
end
@vspacer = m.to_s
@spacer = @vspacer.to_s.split("")[0..@width_l.to_i].join("").to_s
@canvas = @wrapper.to_s + @spacer.to_s + @wrapper.to_s
sleep @fps.to_f
end
}
thread2 = Thread.new { loop do
# puts " Whole String: \"" + @vspacer.to_s + "\"" + " Progresion Rate: " + @fps.to_s + " seconds per cycle. Delay Width: " + @delay.to_s
puts @canvas.to_s
print "\r\e[A"# + "\r\e[A"
sleep @fps.to_f
end
}
loop do
end
@timer = [0,0,0,0]
@canvas = ""
@ticker = 0.001 ## wont run at 0.001?!?! any more suddenly, wtf thread programming is pretty terrible in ruby, ill give you that
thread = Thread.new { loop do
if @timer[3] == 1000
@timer[3] = 0
if @timer[2] == 59
@timer[2] = 0
if @timer[1] == 59
@timer[1] = 0
@timer[0] += 1
else
@timer[1] += 1
end
else
@timer[2] += 1
end
else
@timer[3] += 1
end
if @timer[-1].to_s.length == 1
ms = "00" + @timer[-1].to_s
elsif @timer[-1].to_s.length == 2
ms = "0" + @timer[-1].to_s
elsif @timer[-1].to_s.length == 3
ms = @timer[-1].to_s
end
@canvas = @timer[0..-2].join(":").to_s + ":" + ms.to_s
sleep 0.001
end
}
loop do
puts " Timer.rb"
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep 0.100
end
## this timer had to be adjusted to run well in threads, turns out a thread that does a somewhat
## complicated loop one thousand times per second is still too heavy to run on your average desktop pc
## this timer increments its millisecond counter by 250 four times per second, which is much more managable
## running in two threads
## Note: still not accurate. this timer out races the non-threaded timer
@timer = [0,0,0,0] # hours # minutes # seconds # milliseconds
@canvas = "" # this is where the screen will wait to be drawn to the consol window
@ticker = 0.250 # this controls much long of a second to wait before repeating a loop
thread = Thread.new { loop do
if @timer[3] == 1000 ## timer count up branch
@timer[3] = 0
if @timer[2] == 59
@timer[2] = 0
if @timer[1] == 59
@timer[1] = 0
@timer[0] += 1
else
@timer[1] += 1
end
else
@timer[2] += 1
end
else
@timer[3] += 250
end
if @timer[-1].to_s.length == 1 ## keep millisecond slot three digits at all times for astetics
ms = "00" + @timer[-1].to_s
elsif @timer[-1].to_s.length == 2
ms = "0" + @timer[-1].to_s
elsif @timer[-1].to_s.length == 3
ms = @timer[-1].to_s
end
@canvas = @timer[0..-2].join(":").to_s + ":" + ms.to_s
sleep @ticker.to_f
end
}
thread2 = Thread.new { loop do ##
puts " Timer.rb"
puts @canvas.to_s
print "\r\e[A" + "\r\e[A"
sleep 0.250
end
}
loop do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment