Permalink
Browse files

Cleanly exit on interrupt (ctrl-c).

  • Loading branch information...
1 parent 7020eeb commit 4583f8f936601c72e9847a194ea6efdfb69616fd @pda committed Jan 15, 2012
Showing with 13 additions and 1 deletion.
  1. +13 −1 asc.rb
View
14 asc.rb
@@ -5,17 +5,22 @@ def initialize
reset
end
def reset
+ @run = true
@world = World.new(120)
@screen = Screen.new(120, 40, @world)
end
def run
- loop do
+ Signal.trap(:INT) do
+ @run = false
+ end
+ while @run
start_time = Time.new.to_f
unless @world.tick
reset
end
render start_time
end
+ on_exit
end
def render start_time
@world.buildings.each do |building|
@@ -27,6 +32,9 @@ def render start_time
end
@screen.render start_time
end
+ def on_exit
+ @screen.on_exit
+ end
end
class Screen < Struct.new(:width, :height, :world)
@@ -65,6 +73,10 @@ def render start_time
print buffer
create_frame_buffer
end
+ def on_exit
+ print "\033[0m" # reset colours
+ print "\x1B[?25h" # re-enable cursor
+ end
end
class Framebuffer

0 comments on commit 4583f8f

Please sign in to comment.