]> git.cworth.org Git - sup/commitdiff
improve ncurses key handling for sigwinch and sigint
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 5 Aug 2009 19:40:02 +0000 (15:40 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 5 Aug 2009 19:52:21 +0000 (15:52 -0400)
see comments. adding a nodelay call before each getch means we don't need a
select() call, which means that we can get sigwinch and sigint signals from
ncurses.

bin/sup
lib/sup/buffer.rb

diff --git a/bin/sup b/bin/sup
index 1febefdd5bc0e06e033f6da7a86d1f5bdf2e710f..b32ad75b71a45e5005b0de9d00e02a7c13cfd2a6 100755 (executable)
--- a/bin/sup
+++ b/bin/sup
@@ -218,29 +218,26 @@ begin
   end
 
   until Redwood::exceptions.nonempty? || SuicideManager.die?
-    c = 
-       begin
-         Ncurses.nonblocking_getch
-       rescue Exception => e
-         if e.is_a?(Interrupt)
-           raise if BufferManager.ask_yes_or_no("Die ungracefully now?")
-           bm.draw_screen
-           nil
-         end
-       end
+    c = begin
+      Ncurses.nonblocking_getch
+    rescue Interrupt => e
+      raise if BufferManager.ask_yes_or_no "Die ungracefully now?"
+      BufferManager.draw_screen
+      nil
+    end
+
     next unless c
     bm.erase_flash
 
-    action =
-      begin
-        if bm.handle_input c
-          :nothing
-        else
-          bm.resolve_input_with_keymap c, global_keymap
-        end
-      rescue InputSequenceAborted
+    action = begin
+      if bm.handle_input c
         :nothing
+      else
+        bm.resolve_input_with_keymap c, global_keymap
       end
+    rescue InputSequenceAborted
+      :nothing
+    end
     case action
     when :quit_now
       break if bm.kill_all_buffers_safely
index 5f52d1dff53ab62f80c008a7a25b6a84505e308c..7939e2739f579d760ff153dc34276f35f280b45c 100644 (file)
@@ -25,14 +25,13 @@ module Ncurses
   def mutex; @mutex ||= Mutex.new; end
   def sync &b; mutex.synchronize(&b); end
 
-  ## magically, this stuff seems to work now. i could swear it didn't
-  ## before. hm.
   def nonblocking_getch
-    if IO.select([$stdin], nil, nil, 1)
-      Ncurses.getch
-    else
-      nil
-    end
+    ## INSANITY
+    ## it is NECESSARY to call nodelay EVERY TIME otherwise a single ctrl-c
+    ## will turn a blocking call into a nonblocking one. hours of my life
+    ## wasted on this trivial bullshit: 3.
+    Ncurses.nodelay Ncurses.stdscr, false
+    Ncurses.getch
   end
 
   module_function :rows, :cols, :curx, :nonblocking_getch, :mutex, :sync
@@ -70,7 +69,7 @@ class Buffer
   def content_height; @height - 1; end
   def content_width; @width; end
 
-  def resize rows, cols 
+  def resize rows, cols
     return if cols == @width && rows == @height
     @width = cols
     @height = rows