]> git.cworth.org Git - sup/blobdiff - lib/sup/buffer.rb
protect getch from ctrl-c's outside of the main event loop
[sup] / lib / sup / buffer.rb
index 4b53fed697b037656cdec013a64da1bb5e78f59c..df5d23c455fbaeb9fa53a5d9d66305249d9d6f17 100644 (file)
@@ -35,7 +35,14 @@ module Ncurses
     end
   end
 
-  module_function :rows, :cols, :curx, :nonblocking_getch, :mutex, :sync
+  ## pretends ctrl-c's are ctrl-g's
+  def safe_nonblocking_getch
+    nonblocking_getch
+  rescue Interrupt
+    KEY_CANCEL
+  end
+
+  module_function :rows, :cols, :curx, :nonblocking_getch, :safe_nonblocking_getch, :mutex, :sync
 
   remove_const :KEY_ENTER
   remove_const :KEY_CANCEL
@@ -233,14 +240,20 @@ EOS
   ## have to change this. but it's not clear that we will ever actually
   ## do that.
   def roll_buffers
-    @buffers.last.force_to_top = false
-    raise_to_front @buffers.first
+    bufs = rollable_buffers
+    bufs.last.force_to_top = false
+    raise_to_front bufs.first
   end
 
   def roll_buffers_backwards
-    return unless @buffers.length > 1
-    @buffers.last.force_to_top = false
-    raise_to_front @buffers[@buffers.length - 2]
+    bufs = rollable_buffers
+    return unless bufs.length > 1
+    bufs.last.force_to_top = false
+    raise_to_front bufs[bufs.length - 2]
+  end
+
+  def rollable_buffers
+    @buffers.select { |b| !b.system? || @buffers.last == b }
   end
 
   def handle_input c
@@ -377,7 +390,7 @@ EOS
     draw_screen
 
     until mode.done?
-      c = Ncurses.nonblocking_getch
+      c = Ncurses.safe_nonblocking_getch
       next unless c # getch timeout
       break if c == Ncurses::KEY_CANCEL
       begin
@@ -553,7 +566,7 @@ EOS
     end
 
     while true
-      c = Ncurses.nonblocking_getch
+      c = Ncurses.safe_nonblocking_getch
       next unless c # getch timeout
       break unless tf.handle_input c # process keystroke
 
@@ -606,7 +619,7 @@ EOS
     ret = nil
     done = false
     until done
-      key = Ncurses.nonblocking_getch or next
+      key = Ncurses.safe_nonblocking_getch or next
       if key == Ncurses::KEY_CANCEL
         done = true
       elsif accept.nil? || accept.empty? || accept.member?(key)