Skip to content

Instantly share code, notes, and snippets.

@devemouse
Created January 4, 2011 14:15
Show Gist options
  • Save devemouse/764803 to your computer and use it in GitHub Desktop.
Save devemouse/764803 to your computer and use it in GitHub Desktop.
simple script for aiding with exercises
#!/usr/bin/env ruby
require 'yaml'
RED = "1;31"
GREEN = "1;32"
YELLOW = "1;33"
unless RUBY_PLATFORM.include?('linux')
begin
require 'Win32/Console/ANSI' #if RUBY_PLATFORM =~ /win32/
rescue LoadError
raise 'You must gem install win32console to use color on Windows'
end
end
class OPT
attr_accessor :test
def initialize
test = false
end
end
options = OPT.new
if ARGV.length >0 then
options.test = true
end
$exaidstat = File.join(ENV['HOME'], ".exaidstat")
if ((!ENV['EXAIDSTAT'].nil?) && (File.exists?(ENV['EXAIDSTAT']))) then
$exaidstat = ENV['EXAIDSTAT']
end
def clear
print "\e[2J\e[H"
end
def bigprint(num = 0, opt = {:color => GREEN, :font =>nil})
hist = File.new($exaidstat).readlines.map {|el|
if /(.*), (.*)/ =~ el then
$2.to_i
end
}
graph = draw_graph( hist, '*', 1)
opt[:font][0].length.times do |i|
print (' ' + opt[:font][(num/10) % 100][i] + ' ' + opt[:font][num % 10][i]).color(opt[:color])
print ' '
print graph[i].to_s
if i == (graph.length - num)
puts '*'.color(GREEN)
else
puts ' '.color('40')
end
end
end
def draw_graph(histogram, char="#", scale=2)
max = histogram.max/scale
out = Array.new
max.times do |el|
out[el] = ""
out[el] << "%2d " % [(max - el)*scale] if ((max - el)%scale)
histogram.each do |val|
if( (val/scale == (max - el)) || val == 0)
out[el] << char
else
out[el] << ' '
end
end
end
out
end
class String
def color(color)
return "\e[" + color + "m" + self + "\e[0m"
end
end
repeats = 0
color = GREEN
old = repeats
hist = Array.new
font = YAML::load(DATA).to_a
400.times do
clear
if old > repeats then
color = RED
elsif (old-repeats).abs > 1 then
color = YELLOW
else
color = GREEN
end
bigprint(repeats, {:color => GREEN,:font => font})
puts
repeats.times {print '#'}
puts
print "Enter to repeat: "
old = repeats
char = STDIN.gets
if char.chomp == '' then
repeats += 1
elsif char.chomp == 'q' then
clear
break
elsif tmp = char.chomp.to_i then
if repeats + tmp >0 then
repeats += tmp
end
end
hist.push(repeats)
end
content = File.new($exaidstat).readlines
content.push "%s, %d" % [Time.now.strftime("%Y-%m-%d %H:%M"), repeats] if repeats > 0
hist = content.map {|el|
if /(.*), (.*)/ =~ el then
$2.to_i
end
}
File.new($exaidstat,"w").puts content unless options.test
puts
puts "after %s repetitions:" % hist.length.to_s.color(YELLOW)
puts
puts draw_graph(hist, '*', 1) unless hist.empty?
__END__
---
- - " 000000000 "
- " 00:::::::::00 "
- " 00:::::::::::::00 "
- 0:::::::000:::::::0
- 0::::::0 0::::::0
- 0:::::0 0:::::0
- 0:::::0 0:::::0
- 0:::::0 000 0:::::0
- 0:::::0 000 0:::::0
- 0:::::0 0:::::0
- 0:::::0 0:::::0
- 0::::::0 0::::::0
- 0:::::::000:::::::0
- " 00:::::::::::::00 "
- " 00:::::::::00 "
- " 000000000 "
- - " 1111111 "
- " 1::::::1 "
- "1:::::::1 "
- "111:::::1 "
- " 1::::1 "
- " 1::::1 "
- " 1::::1 "
- " 1::::l "
- " 1::::l "
- " 1::::l "
- " 1::::l "
- " 1::::l "
- 111::::::111
- 1::::::::::1
- 1::::::::::1
- "111111111111"
- - " 222222222222222 "
- "2:::::::::::::::22 "
- "2::::::222222:::::2 "
- "2222222 2:::::2 "
- " 2:::::2 "
- " 2:::::2 "
- " 2222::::2 "
- " 22222::::::22 "
- " 22::::::::222 "
- " 2:::::22222 "
- "2:::::2 "
- "2:::::2 "
- 2:::::2 222222
- 2::::::2222222:::::2
- 2::::::::::::::::::2
- "22222222222222222222"
- - " 333333333333333 "
- "3:::::::::::::::33 "
- 3::::::33333::::::3
- 3333333 3:::::3
- " 3:::::3"
- " 3:::::3"
- " 33333333:::::3 "
- " 3:::::::::::3 "
- " 33333333:::::3 "
- " 3:::::3"
- " 3:::::3"
- " 3:::::3"
- 3333333 3:::::3
- 3::::::33333::::::3
- "3:::::::::::::::33 "
- " 333333333333333 "
- - " 444444444 "
- " 4::::::::4 "
- " 4:::::::::4 "
- " 4::::44::::4 "
- " 4::::4 4::::4 "
- " 4::::4 4::::4 "
- " 4::::4 4::::4 "
- 4::::444444::::444
- 4::::::::::::::::4
- 4444444444:::::444
- " 4::::4 "
- " 4::::4 "
- " 4::::4 "
- " 44::::::44"
- " 4::::::::4"
- " 4444444444"
- - "555555555555555555 "
- "5::::::::::::::::5 "
- "5::::::::::::::::5 "
- "5:::::555555555555 "
- "5:::::5 "
- "5:::::5 "
- "5:::::5555555555 "
- "5:::::::::::::::5 "
- "555555555555:::::5 "
- " 5:::::5"
- " 5:::::5"
- 5555555 5:::::5
- 5::::::55555::::::5
- " 55:::::::::::::55 "
- " 55:::::::::55 "
- " 555555555 "
- - " 66666666 "
- " 6::::::6 "
- " 6::::::6 "
- " 6::::::6 "
- " 6::::::6 "
- " 6::::::6 "
- " 6::::::6 "
- " 6::::::::66666 "
- "6::::::::::::::66 "
- "6::::::66666:::::6 "
- 6:::::6 6:::::6
- 6:::::6 6:::::6
- 6::::::66666::::::6
- " 66:::::::::::::66 "
- " 66:::::::::66 "
- " 666666666 "
- - "77777777777777777777"
- 7::::::::::::::::::7
- 7::::::::::::::::::7
- 777777777777:::::::7
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- " 7::::::7 "
- "77777777 "
- - " 888888888 "
- " 88:::::::::88 "
- " 88:::::::::::::88 "
- 8::::::88888::::::8
- 8:::::8 8:::::8
- 8:::::8 8:::::8
- " 8:::::88888:::::8 "
- " 8:::::::::::::8 "
- " 8:::::88888:::::8 "
- 8:::::8 8:::::8
- 8:::::8 8:::::8
- 8:::::8 8:::::8
- 8::::::88888::::::8
- " 88:::::::::::::88 "
- " 88:::::::::88 "
- " 888888888 "
- - " 999999999 "
- " 99:::::::::99 "
- " 99:::::::::::::99 "
- 9::::::99999::::::9
- 9:::::9 9:::::9
- 9:::::9 9:::::9
- " 9:::::99999::::::9"
- " 99::::::::::::::9"
- " 99999::::::::9 "
- " 9::::::9 "
- " 9::::::9 "
- " 9::::::9 "
- " 9::::::9 "
- " 9::::::9 "
- " 9::::::9 "
- " 99999999 "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment