Class: RubyCurses::StdscrWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/extras/stdscrwindow.rb

Overview

an attempt to make a window based on stdscr for bottom line printing so the entire application can have one pointer, regardless of whether through App or otherwise. This still provides the full getchar operation, copied from 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.

NOTE:
WARNING: Pls do not use this class. This was just experimentation. Using stdscr
mucks up the display and overwrites objects on the screen. I will remove this class
at some time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStdscrWindow

Returns a new instance of StdscrWindow.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 24

def initialize

  #@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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

taken from Window



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 47

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

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



22
23
24
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 22

def height
  @height
end

#leftObject (readonly)

Returns the value of attribute left.



22
23
24
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 22

def left
  @left
end

#topObject (readonly)

Returns the value of attribute top.



22
23
24
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 22

def top
  @top
end

#widthObject (readonly)

Returns the value of attribute width.



22
23
24
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 22

def width
  @width
end

Instance Method Details

#clearObject



215
216
217
218
219
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 215

def clear
  # return unless visible?
  move 0, 0
  puts *Array.new(height){ ' ' * (width - 1) }
end

#clear_error(r = $error_message_row, color = $datacolor) ⇒ Object

raise “DEPRECATED CLEANUP use global method of same name”



298
299
300
301
302
303
304
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 298

def clear_error r = $error_message_row, color = $datacolor
  return unless $error_message_clear_pending
  c = $error_message_col || (Ncurses.COLS-text.length)/2 
  sz = $error_message_size || Ncurses.COLS
  printstring(r, c, "%-*s" % [sz, " "], color)
  $error_message_clear_pending = false
end

#color=(color) ⇒ Object



110
111
112
113
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 110

def color=(color)
  @color = color
  @window_pointer.color_set(color, nil)
end

#get_windowObject



306
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 306

def get_window; @window_pointer; end

#getchObject

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.



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 126

def getch
  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

#getcharObject

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 143

def getchar 
  FFI::NCurses.raw # FFI required so that getch does not crash on C-c
  while 1 
    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}"
        case @stack.size
        when 1
          @stack.clear
          return 27
        when 2 # basically a ALT-O, this will be really slow since it waits for -1
          ch = 128 + @stack.last
          @stack.clear
          return ch
        when 3
          $log.debug " SHOULD NOT COME HERE getchar()"
        end
      end
      @stack.clear
      next
    end
    # this is the ALT combination
    if @stack.first == 27
      # experimental. 2 escapes in quick succession to make exit faster
      if ch == 27
        @stack.clear
        return ch
      end
      # possible F1..F3 on xterm-color
      if ch == 79 or ch == 91
        #$log.debug " got 27, #{ch}, waiting for one more"
        @stack << ch
        next
      end
      #$log.debug "stack SIZE  #{@stack.size}, #{@stack.inspect}, ch: #{ch}"
      if @stack == [27,79]
        # xterm-color
        case ch
        when 80
          ch = KEY_F1
        when 81
          ch = KEY_F2
        when 82
          ch = KEY_F3
        when 83
          ch = KEY_F4
        end
        @stack.clear
        return ch
      elsif @stack == [27, 91]
        if ch == 90
          @stack.clear
          return KEY_BTAB # backtab
        end
      end
      # the usual Meta combos. (alt)
      ch = 128 + ch
      @stack.clear
      return ch
    end
    # append a 27 to stack, actually one can use a flag too
    if ch == 27
      @stack << 27
      next
    end
    return ch
  end
end

#highlight_line(color, y, x, max) ⇒ Object



115
116
117
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 115

def highlight_line(color, y, x, max)
  @window_pointer.mvchgat(y, x, max, FFI::NCurses::A_NORMAL, color, nil)
end

#init_varsObject



36
37
38
39
40
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 36

def init_vars
  #Ncurses::keypad(@window_pointer, true)
  Ncurses.keypad(@window_pointer.pointer, true)
  @stack = []
end

FFI WARNING XXX I think these methods one window_pointer will fail since somehow stdscr is not getting these methods here.



82
83
84
85
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 82

def print(string, width = width)
  return unless visible?
  @window_pointer.waddnstr(string.to_s, width)
end


91
92
93
94
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 91

def print_empty_line
  return unless visible?
  @window_pointer.printw(' ' * width)
end

added by rk 2008-11-29 19:01 Since these methods write directly to window they are not advised since clearing previous message we don’t know how much to clear. Best to map error_message to a label.

2010-09-13 00:22 WE should not use these any longer.
Application should create a label and map a Variable named
$errormessage to it. We should only update the Variable

CLEANUP DEPRECATED



274
275
276
277
278
279
280
281
282
283
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 274

def print_error_message text=$error_message
  raise "DEPRECATED CLEANUP use global method of same name"
  r = $error_message_row || Ncurses.LINES-1
  c = $error_message_col || (Ncurses.COLS-text.length)/2 

  $log.debug "got ERROR MESSAGE #{text} row #{r} "
  clear_error r, $datacolor
  printstring r, c, text, color = $promptcolor
  $error_message_clear_pending = true
end


96
97
98
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 96

def print_line(string)
  print(string.ljust(width))
end

added by rk 2008-11-29 19:01 CLEANUP DEPRECATED



286
287
288
289
290
291
292
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 286

def print_status_message text=$status_message
  raise "DEPRECATED CLEANUP use global method of same name"
  r = $status_message_row || Ncurses.LINES-1
  clear_error r, $datacolor
  # print it in centre
  printstring r, (Ncurses.COLS-text.length)/2, text, color = $promptcolor
end


87
88
89
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 87

def print_yx(string, y = 0, x = 0)
  @window_pointer.mvwaddnstr(y, x, string, width)
end

#printstring(r, c, string, color, att = FFI::NCurses::A_NORMAL) ⇒ Object

added by rk 2008-11-29 19:01 I usually use this, not the others ones here @ param att - ncurses attribute: normal, bold, reverse, blink, underline

Parameters:

  • r
    • row

  • c
    • col

  • string
    • text to print

  • color
    • color pair



234
235
236
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 234

def printstring(r,c,string, color, att = FFI::NCurses::A_NORMAL)
    prv_printstring(r,c,string, color, att )
end

#prv_printstring(r, c, string, color, att = FFI::NCurses::A_NORMAL) ⇒ Object

name changed from printstring to prv_prinstring



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 239

def prv_printstring(r,c,string, color, att = FFI::NCurses::A_NORMAL)

  #$log.debug " #{@name} inside window printstring r #{r} c #{c} #{string} "
  att = FFI::NCurses::A_NORMAL if att.nil? 
  case att.to_s.downcase
  when 'normal'
    att = FFI::NCurses::A_NORMAL
  when 'underline'
    att = FFI::NCurses::A_UNDERLINE
  when 'bold'
    att = FFI::NCurses::A_BOLD
  when 'blink'
    att = FFI::NCurses::A_BLINK    # unlikely to work
  when '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 
  width = Ncurses.COLS
  # the next line won't ensure we don't write outside some bounds like table
  #string = string[0..(width-c)] if c + string.length > width
  #$log.debug "PRINT len:#{string.length}, #{Ncurses.COLS}, #{r}, #{c} w: #{@window} "
  mvprintw(r, c, "%s", string);
  attroff(Ncurses.COLOR_PAIR(color) | att)
end

#puts(*strings) ⇒ Object



100
101
102
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 100

def puts(*strings)
  print(strings.join("\n") << "\n")
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 71

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

#to_sObject



307
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 307

def to_s; @name || self; end

#ungetch(ch) ⇒ Object



119
120
121
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 119

def ungetch(ch)
  FFI::NCurses.ungetch(ch)
end

#visible?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 222

def visible?
  @visible
end

#XXXrefreshObject

FFI 2011-09-9 commented off so it goes to method missing



105
106
107
# File 'lib/rbcurse/extras/stdscrwindow.rb', line 105

def XXXrefresh
  @window_pointer.refresh
end