lib/rbcurse/extras/stdscrwindow.rb in rbcurse-1.2.0 vs lib/rbcurse/extras/stdscrwindow.rb in rbcurse-1.3.0

- old
+ new

@@ -7,88 +7,139 @@ # Window class which is the main purpose of this. So as to provide ask, agree and say. # # We should be able to pass this window to bottomline and have one global bottomline # created once (by window class ?) # + # FFI: 2011-09-9 The change to FFI has affected this a lot since I do not get window + # methods in rbcurse for stdscr whereas suprisingly if i run the samples, i get them + # all. class StdscrWindow attr_reader :width, :height, :top, :left def initialize - @window = Ncurses.stdscr + #@window_pointer = FFI::NCurses.initscr + @window_pointer = Ncurses.initscr # FFIWINDOW + $log.debug "STDSCR window pointer is #{@window_pointer.class}" + #$log.debug "STDSCR window pointer mehtods #{@window_pointer.public_methods}" $error_message_row ||= Ncurses.LINES-1 $error_message_col ||= 1 init_vars end def init_vars - Ncurses::keypad(@window, true) + #Ncurses::keypad(@window_pointer, true) + Ncurses.keypad(@window_pointer.pointer, true) @stack = [] end ## # Ncurses - def method_missing(meth, *args) - @window.send(meth, *args) + + # taken from Window + def method_missing(name, *args) + name = name.to_s + if (name[0,2] == "mv") + test_name = name.dup + test_name[2,0] = "w" # insert "w" after"mv" + if (@window_pointer.respond_to?(test_name)) # FFIPOINTER + return @window_pointer.send(test_name,*args) + end + + if (FFI::NCurses.respond_to?(test_name)) + return FFI::NCurses.send(test_name, @window_pointer, *args) + end + end + test_name = "w" + name + # FFI responds but the pointer does not !!! bottomline 1045 + if (@window_pointer.respond_to?(test_name)) # FFIPOINTER + return @window_pointer.send(test_name,*args) + end + if (FFI::NCurses.respond_to?(test_name)) + return FFI::NCurses.send(test_name, @window_pointer, *args) + end + # what if it does not respond, can go into loop and give stack overflow + FFI::NCurses.send(name, @window_pointer, *args) end + def respond_to?(name) + name = name.to_s + if (name[0,2] == "mv" && FFI::NCurses.respond_to?("mvw" + name[2..-1])) + return true + end + FFI::NCurses.respond_to?("w" + name) || FFI::NCurses.respond_to?(name) + end + #@window.send(meth, *args) + # FFI WARNING XXX I think these methods one window_pointer will fail + # since somehow stdscr is not getting these methods here. def print(string, width = width) return unless visible? - @window.waddnstr(string.to_s, width) + @window_pointer.waddnstr(string.to_s, width) end def print_yx(string, y = 0, x = 0) - @window.mvwaddnstr(y, x, string, width) + @window_pointer.mvwaddnstr(y, x, string, width) end def print_empty_line return unless visible? - @window.printw(' ' * width) + @window_pointer.printw(' ' * width) end def print_line(string) print(string.ljust(width)) end def puts(*strings) print(strings.join("\n") << "\n") end - def refresh - @window.refresh + # FFI 2011-09-9 commented off so it goes to method missing + def XXXrefresh + @window_pointer.refresh end def color=(color) @color = color - @window.color_set(color, nil) + @window_pointer.color_set(color, nil) end def highlight_line(color, y, x, max) - @window.mvchgat(y, x, max, Ncurses::A_NORMAL, color, nil) + @window_pointer.mvchgat(y, x, max, FFI::NCurses::A_NORMAL, color, nil) end def ungetch(ch) - Ncurses.ungetch(ch) + FFI::NCurses.ungetch(ch) end + # this used to work fine in ncursesruby + # but somehow in ffi, stdscr does not have most methods, i am unable to figure + # this out. C-c will crash this. def getch - c = @window.getch - #if c == Ncurses::KEY_RESIZE + c = @window_pointer.getch # FFI NW stdscr must get key not some window + #raise "Ncurses.stdscr does not have getch" if !Ncurses.stdscr.respond_to? :getch + #$log.debug " XXXX before calling getch" + #c = Ncurses.stdscr.getch # FFIW if you use the FFIWINDOW + #c = FFI::NCurses.getch # FFI 2011-09-9 # without FFIWINDOW + #$log.debug " XXXX after calling getch #{c}" + #if c == FFI::NCurses::KEY_RESIZE + return c rescue Interrupt => ex 3 # is C-c end # returns control, alt, alt+ctrl, alt+control+shift, F1 .. etc # ALT combinations also send a 27 before the actual key # Please test with above combinations before using on your terminal # added by rkumar 2008-12-12 23:07 def getchar + FFI::NCurses.raw # FFI required so that getch does not crash on C-c while 1 - ch = getch + ch = getch #$log.debug "window getchar() GOT: #{ch}" if ch != -1 if ch == -1 # the returns escape 27 if no key followed it, so its SLOW if you want only esc if @stack.first == 27 #$log.debug " -1 stack sizze #{@stack.size}: #{@stack.inspect}, ch #{ch}" @@ -172,30 +223,30 @@ # @param c - col # @param string - text to print # @param color - color pair # @ param att - ncurses attribute: normal, bold, reverse, blink, # underline - def printstring(r,c,string, color, att = Ncurses::A_NORMAL) + def printstring(r,c,string, color, att = FFI::NCurses::A_NORMAL) prv_printstring(r,c,string, color, att ) end ## name changed from printstring to prv_prinstring - def prv_printstring(r,c,string, color, att = Ncurses::A_NORMAL) + def prv_printstring(r,c,string, color, att = FFI::NCurses::A_NORMAL) #$log.debug " #{@name} inside window printstring r #{r} c #{c} #{string} " - att = Ncurses::A_NORMAL if att.nil? + att = FFI::NCurses::A_NORMAL if att.nil? case att.to_s.downcase when 'normal' - att = Ncurses::A_NORMAL + att = FFI::NCurses::A_NORMAL when 'underline' - att = Ncurses::A_UNDERLINE + att = FFI::NCurses::A_UNDERLINE when 'bold' - att = Ncurses::A_BOLD + att = FFI::NCurses::A_BOLD when 'blink' - att = Ncurses::A_BLINK # unlikely to work + att = FFI::NCurses::A_BLINK # unlikely to work when 'reverse' - att = Ncurses::A_REVERSE + att = FFI::NCurses::A_REVERSE end attron(Ncurses.COLOR_PAIR(color) | att) # we should not print beyond window coordinates # trying out on 2009-01-03 19:29 @@ -239,9 +290,9 @@ sz = $error_message_size || Ncurses.COLS printstring(r, c, "%-*s" % [sz, " "], color) $error_message_clear_pending = false end ## - def get_window; @window; end + def get_window; @window_pointer; end def to_s; @name || self; end end end