Created
April 26, 2011 20:39
-
-
Save stephenprater/943080 to your computer and use it in GitHub Desktop.
eh?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DebugMachineCache | |
remove_const :COMMANDS rescue nil#TODO remove this | |
# assemble format strings for movement commands | |
movmts = Hash[[:hpa,:vpa,:cup,:home,:setaf].zip( | |
[`tput hpa`, `tput vpa`, `tput cup`,`tput home`,`tput setaf`].collect do |fmt| | |
fmt.split(/(%[pPg][\d\w])|(%.)/).each_with_object '' do |i,m| | |
if i == '%d' | |
m << '%d' | |
elsif i[0] != '%' | |
m << i | |
end | |
end | |
end)] | |
COMMANDS = { | |
save: `tput sc`, | |
restore: `tput rc`, | |
reset: `tput sgr0`, | |
smacs: `tput smacs`, | |
rmacs: `tput rmacs`, | |
left: `tput cub1`, | |
right: `tput cuf1`, | |
up: `tput cuu1`, | |
down: `tput cud1`, | |
test: 'test', | |
setaf: lambda do |color| | |
movmts[:setaf] % color | |
end, | |
mrcup: lambda do |row,col| | |
hdir = col > 0 ? :right : :left | |
vdir = row > 0 ? :down : :up | |
[[hdir,col],[vdir,row]].inject '' do |memo, dir| | |
memo << (COMMANDS[dir[0]] * dir[1].abs) | |
end | |
end, | |
moveto: lambda do |*args| | |
# pass it either a [row,col] array or a row:x, col:x opts hash. | |
row,col = *({ :row => nil, :col => nil}.merge(args.first).values rescue args) | |
cmd = (col and row) ? :cup : (col ? :hpa : (row ? :vpa : :home)) | |
movmts[cmd] % [row, col] | |
end | |
} | |
COMMANDS.each_pair do |m,b| | |
c = b.is_a?(Proc) ? b : lambda { b } | |
define_method m, &c | |
end | |
end | |
r = DebugMachineCache.new | |
r.save # => #<Proc> - should be '\e[7' or whatever | |
r.moveto(0,1) # => \e[1;2H # correct |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment