File: ncurses_sugar.rb

package info (click to toggle)
ruby-ncurses 1.4.13-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 484 kB
  • sloc: ansic: 4,962; ruby: 1,099; makefile: 2
file content (342 lines) | stat: -rw-r--r-- 8,683 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# encoding: utf-8
# ncurses-ruby is a ruby module for accessing the FSF's ncurses library
# (C) 2002, 2003, 2004 Tobias Peters <t-peters@users.berlios.de>
# (C) 2004 Simon Kaczor <skaczor@cox.net>
# (C) 2005 2011 Tobias Herzke
#
# This module is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this module; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

# $Id: ncurses_sugar.rb,v 1.1 2011-05-30 23:05:50 t-peters Exp $

# Ncurses constants with leading underscore
def Ncurses._XOPEN_CURSES
  Ncurses::XOPEN_CURSES
end
def Ncurses._SUBWIN
  Ncurses::SUBWIN
end
def Ncurses._ENDLINE
  Ncurses::ENDLINE
end
def Ncurses._FULLWIN
  Ncurses::FULLWIN
end
def Ncurses._SCROLLWIN
  Ncurses::SCROLLWIN
end
def Ncurses._ISPAD
  Ncurses::ISPAD
end
def Ncurses._HASMOVED
  Ncurses::HASMOVED
end
def Ncurses._WRAPPED
  Ncurses::WRAPPED
end
def Ncurses._NOCHANGE
  Ncurses::NOCHANGE
end
def Ncurses._NEWINDEX
  Ncurses::NEWINDEX
end


module Ncurses
  module Destroy_checker; def destroyed?; @destroyed; end; end
  class WINDOW
    include Destroy_checker
    def method_missing(name, *args, &block)
      name = name.to_s
      if (name[0,2] == "mv")
        test_name = name.dup
        test_name[2,0] = "w" # insert "w" after"mv"
        if (Ncurses.respond_to?(test_name))
          return Ncurses.send(test_name, self, *args, &block)
        end
      end
      test_name = "w" + name
      if (Ncurses.respond_to?(test_name))
        return Ncurses.send(test_name, self, *args, &block)
      end
      Ncurses.send(name, self, *args, &block)
    end
    def respond_to?(name)
      name = name.to_s
      if (name[0,2] == "mv" && Ncurses.respond_to?("mvw" + name[2..-1]))
        return true
      end
      Ncurses.respond_to?("w" + name) || Ncurses.respond_to?(name)
    end
    def del
      Ncurses.delwin(self)
    end
    alias delete del
    def WINDOW.new(*args)
      Ncurses.newwin(*args)
    end
  end
  class SCREEN
    include Destroy_checker
    def del
      Ncurses.delscreen(self)
    end
    alias delete del
  end
  class MEVENT
    attr_accessor :id, :x,:y,:z, :bstate
  end
  GETSTR_LIMIT = 1024

  module Panel
    class PANEL; end
  end

  module Form
    class FORM
      attr_reader :user_object

      # This placeholder replaces the form_userptr function in curses
      def user_object=(obj)
        @user_object = obj
      end
    end

    class FIELD
      attr_reader :user_object

      # This placeholder replaces the field_userptr function in curses
      def user_object=(obj)
        @user_object = obj
      end
    end

    class FIELDTYPE
    end
  end

  module Menu
    class MENU
      attr_reader :user_object

      # This placeholder replaces the menu_userptr function in curses
      def user_object=(obj)
        @user_object = obj
      end
    end

    class ITEM
      attr_reader :user_object

      # This placeholder replaces the item_userptr function in curses
      def user_object=(obj)
        @user_object = obj
      end
    end
  end
end
def Ncurses.inchnstr(str,n)
  Ncurses.winchnstr(Ncurses.stdscr, str, n)
end
def Ncurses.inchstr(str)
  Ncurses.winchstr(Ncurses.stdscr, str)
end
def Ncurses.mvinchnstr(y,x, str, n)
  Ncurses.mvwinchnstr(Ncurses.stdscr, y,x, str, n)
end
def Ncurses.mvinchstr(y,x, str)
  Ncurses.mvwinchstr(Ncurses.stdscr, y,x, str)
end
def Ncurses.mvwinchnstr(win, y,x, str, n)
  if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    Ncurses::ERR
  else
    Ncurses.winchnstr(win,str,n)
  end
end
def Ncurses.mvwinchstr(win, y,x, str)
  maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
  return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
  Ncurses.mvwinchnstr(win, y,x, str, maxx[0]+1)
end
def Ncurses.winchstr(win, str)
  maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
  return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
  Ncurses.winchnstr(win, str, maxx[0]+1)
end

def Ncurses.getnstr(str,n)
  Ncurses.wgetnstr(Ncurses.stdscr, str, n)
end
def Ncurses.mvgetnstr(y,x, str, n)
  Ncurses.mvwgetnstr(Ncurses.stdscr, y,x, str, n)
end
def Ncurses.mvwgetnstr(win, y,x, str, n)
  if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    Ncurses::ERR
  else
    Ncurses.wgetnstr(win,str,n)
  end
end

def Ncurses.innstr(str,n)
  Ncurses.winnstr(Ncurses.stdscr, str, n)
end
def Ncurses.instr(str)
  Ncurses.winstr(Ncurses.stdscr, str)
end
def Ncurses.mvinnstr(y,x, str, n)
  Ncurses.mvwinnstr(Ncurses.stdscr, y,x, str, n)
end
def Ncurses.mvinstr(y,x, str)
  Ncurses.mvwinstr(Ncurses.stdscr, y,x, str)
end
def Ncurses.mvwinnstr(win, y,x, str, n)
  if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    Ncurses::ERR
  else
    Ncurses.winnstr(win,str,n)
  end
end
def Ncurses.mvwinstr(win, y,x, str)
  maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
  return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
  Ncurses.mvwinnstr(win, y,x, str, maxx[0]+1)
end
def Ncurses.winstr(win, str)
  maxy = []; maxx = []; getmaxyx(win, maxy,maxx)
  return Ncurses::ERR if (maxx[0] == Ncurses::ERR)
  Ncurses.winnstr(win, str, maxx[0]+1)
end

def Ncurses.mouse_trafo(pY, pX, to_screen)
  Ncurses.wmouse_trafo(Ncurses.stdscr, pY, pX, to_screen)
end

def Ncurses.getcurx(win)
  x = []; y = []; Ncurses.getyx(win, y,x); x[0]
end
def Ncurses.getcury(win)
  x = []; y = []; Ncurses.getyx(win, y,x); y[0]
end
def Ncurses.getbegx(win)
  x = []; y = []; Ncurses.getbegyx(win, y,x); x[0]
end
def Ncurses.getbegy(win)
  x = []; y = []; Ncurses.getbegyx(win, y,x); y[0]
end
def Ncurses.getmaxx(win)
  x = []; y = []; Ncurses.getmaxyx(win, y,x); x[0]
end
def Ncurses.getmaxy(win)
  x = []; y = []; Ncurses.getmaxyx(win, y,x); y[0]
end
def Ncurses.getparx(win)
  x = []; y = []; Ncurses.getparyx(win, y,x); x[0]
end
def Ncurses.getpary(win)
  x = []; y = []; Ncurses.getparyx(win, y,x); y[0]
end
def Ncurses.erase
  Ncurses.werase(Ncurses.stdscr)
end
def Ncurses.getstr(str)
  Ncurses.getnstr(str, Ncurses::GETSTR_LIMIT)
end
def Ncurses.mvgetstr(y,x, str)
  Ncurses.mvgetnstr(y,x, str, Ncurses::GETSTR_LIMIT)
end
def Ncurses.mvwgetstr(win, y,x, str)
  Ncurses.mvwgetnstr(win, y,x, str, Ncurses::GETSTR_LIMIT)
end
def Ncurses.wgetstr(win, str)
  Ncurses.wgetnstr(win, str, Ncurses::GETSTR_LIMIT)
end

def Ncurses.scanw(format, result)
  Ncurses.wscanw(Ncurses.stdscr, format, result)
end
def Ncurses.mvscanw(y,x, format, result)
  Ncurses.mvwscanw(Ncurses.stdscr, y,x, format, result)
end
def Ncurses.mvwscanw(win, y,x, format, result)
  if (Ncurses.wmove(win, y,x) == Ncurses::ERR)
    Ncurses::ERR
  else
    Ncurses.wscanw(win, format, result)
  end
end
def Ncurses.wscanw(win, format, result)
  str = ""
  if (Ncurses.wgetstr(win, str) == Ncurses::ERR)
    Ncurses::ERR
  else
    require "scanf.rb" # Use ruby's implementation of scanf
    result.replace(str.scanf(format))
  end
end

def Ncurses.mvprintw(*args)
  Ncurses.mvwprintw(Ncurses.stdscr, *args)
end
def Ncurses.mvwprintw(win, y,x, *args)
  if (Ncurses.wmove(win,y,x) == Ncurses::ERR)
    Ncurses::ERR
  else
    wprintw(win, *args)
  end
end
def Ncurses.printw(*args)
  Ncurses.wprintw(Ncurses.stdscr, *args)
end
def Ncurses.touchline(win, start, count)
  Ncurses.wtouchln(win, start, count, 1)
end
def Ncurses.touchwin(win)
  wtouchln(win, 0, getmaxy(win), 1)
end

module Ncurses
  Ncurses = self # for accessing Ncurses from a Module that includes Ncurses

  # Some users like to include ncurses names despite namespace pollution
  # This module is for them
  module Namespace
    def self.append_features(target)
      # include constants
      unless target.ancestors.member?(Ncurses)
        target.__send__(:include, Ncurses)
      end

      # make methods available
      unless target.respond_to?(:pre_Ncurses_method_missing)
        target.module_eval{
          alias pre_Ncurses_method_missing method_missing
          def method_missing(name, *args, &block)
            if Ncurses.respond_to?(name)
              Ncurses.send(name, *args, &block)
            else
              pre_Ncurses_method_missing(name, *args, &block)
            end
          end
        }
      end
    end
    def self.entend_object(object)
      class << object
        self
      end.__send__(:include, self)
    end
  end
end