Module: RNDK

Defined in:
lib/rndk/core/utils.rb,
lib/rndk.rb,
lib/rndk/entry.rb,
lib/rndk/graph.rb,
lib/rndk/label.rb,
lib/rndk/radio.rb,
lib/rndk/scale.rb,
lib/rndk/button.rb,
lib/rndk/dialog.rb,
lib/rndk/fscale.rb,
lib/rndk/scroll.rb,
lib/rndk/slider.rb,
lib/rndk/viewer.rb,
lib/rndk/fslider.rb,
lib/rndk/version.rb,
lib/rndk/calendar.rb,
lib/rndk/itemlist.rb,
lib/rndk/scroller.rb,
lib/rndk/template.rb,
lib/rndk/alphalist.rb,
lib/rndk/buttonbox.rb,
lib/rndk/core/draw.rb,
lib/rndk/core/color.rb,
lib/rndk/core/markup.rb,
lib/rndk/core/screen.rb,
lib/rndk/core/widget.rb,
lib/rndk/core/display.rb,
lib/rndk/core/traverse.rb,
lib/rndk/core/widget_bind.rb,
lib/rndk/core/quick_widgets.rb

Overview

RNDK String Markup

RNDK has special formatting commands which can be included in any String which add highlights, justification, or even colors to a basic String.

These attributes, once set, remain in effect until changed explicitly, or until the end of the string.

Colors

RNDK has the capability to display colors in almost any text inside a Widget.

To turn on colors, the function #initCDKColor has to be called. When this function is called 64 color pairs are created. Normally the color pairs are accessed via the COLOR_PAIR macro. You can still do this, but creating a string with multiple colors gets terribly difficult. That is why the color commands were created.

The color settings are stored directly in the string. When the widget is created or activated, the string is converted to take advan- tage of any color commands in the string. To turn on a color pair insert into the string; where XX is a numeric value from 0 to 64. Color pair 0 is the standard default color pair for the screen. To turn off a color pair use the format command <!XX> where XX is a numeric value from 0 to 64.

For example:

"</31>This line should have a yellow foreground and a cyan background.<!31>"
"</05>This line should have a white  foreground and a blue background.<!05>"
"</26>This line should have a yellow foreground and a red  background.<!26>"
"<C>This line should be set to whatever the screen default is."

Attributes

RNDK also provides attribute commands which allow different character attributes to be displayed in a Widget.

To use a character attribute the format command is </X> where X is one of several command characters. To turn a attribute off use the command <!X>.

Here's the command characters supported:

B:: Bold U:: Underline K:: Blink R:: Reverse S:: Standout D:: Dim N:: Normal

For example:

"</B/31>Bold text        yellow foreground / blue background.<!31>"
"</U/05>Underlined text  white  foreground / blue background.<!05>"
"</K/26>Blinking text    yellow foreground / red  background.<!26>"
"<C>This line uses the screen default colors."

Justification

Justification commands can left justify, right justify, or center a string of text.

A format command must be at the beginning of the string.

To use a justification format in a string the command <X> is used.

Format commands:

:: Left Justified. Default if not stated. :: Centered text. :: Right justified. :: Indent the line X characters. :: Bullet. X is the bullet string to use. :: Links in a file where X is the filename. This works only with the viewer widget.

For example:

"<R></B/31>This line should have a yellow foreground and a blue background.<!31>"
"</U/05>This line should have a white  foreground and a blue background.<!05>"
"<B=+>This is a bullet."
"<I=10>This is indented 10 characters."
"<C>This line should be set to whatever the screen default is."

The bullet format command can take either a single character or a string. The bullet in the above example would look like

+ This is a bullet.

but if we were to use the following command instead

<B=***>This is a bullet.

it would look like

*** This is a bullet.

Special Drawing Characters

RNDK has a set of special drawing characters which can be inserted into any ASCII file. In order to use a special character the format command <#XXX> is used.

Special character commands:

<#UL>:: Upper Left Corner <#UR>:: Upper Right Corner <#LL>:: Lower Left Corner <#LR>:: Lower Right Corner

<#LT>:: Left Tee <#RT>:: Right Tee <#TT>:: Top Tee <#BT>:: Bottom Tee

<#HL>:: Horizontal Line <#VL>:: Vertical Line

<#PL>:: Plus Sign <#PM>:: Plus or Minus Sign <#DG>:: Degree Sign <#CB>:: Checker Board <#DI>:: Diamond <#BU>:: Bullet <#S1>:: Scan line 1 <#S9>:: Scan line 9

<#LA>:: Left Arrow <#RA>:: Right Arrow <#TA>:: Top Arrow <#BA>:: Bottom Arrow

The character formats can be repeated using an optional numeric repeat value. To repeat a character add the repeat count within parentheses to the end of the character format.

The following example draws 10 horizontal-line characters:

<#HL(10)>

And the most complex example until now. Guess what it does:

"<C><#UL><#HL(26)><#UR>"
"<C><#VL></R>This text should be boxed.<!R><#VL>"
"<C><#LL><#HL(26)><#LR>"
"<C>While this is not."

Defined Under Namespace

Modules: Color, Display, Draw, Traverse Classes: Alphalist, Button, Buttonbox, Calendar, Dialog, Entry, Fscale, Fslider, Graph, Itemlist, Label, Radio, Scale, Screen, Scroll, Scroller, Slider, Template, Viewer, Widget

Constant Summary collapse

RNDK_PATHMAX =

Some nice global constants.

256
L_MARKER =
'<'
R_MARKER =
'>'
LEFT =
9000
RIGHT =
9001
CENTER =
9002
TOP =
9003
BOTTOM =
9004
HORIZONTAL =
9005
VERTICAL =
9006
FULL =
9007
NONE =
0
ROW =
1
COL =
2
MAX_BINDINGS =
300
MAX_ITEMS =
2000
MAX_ButtonS =
200
REFRESH =

Aliasing the default keybindings for Widget interaction.

Example: RNDK.CTRL('L') is Ctrl+L or C-l

RNDK.CTRL('L')
PASTE =
RNDK.CTRL('V')
COPY =
RNDK.CTRL('Y')
ERASE =
RNDK.CTRL('U')
CUT =
RNDK.CTRL('X')
BEGOFLINE =
RNDK.CTRL('A')
ENDOFLINE =
RNDK.CTRL('E')
BACKCHAR =
RNDK.CTRL('B')
FORCHAR =
RNDK.CTRL('F')
TRANSPOSE =
RNDK.CTRL('T')
NEXT =
RNDK.CTRL('N')
PREV =
RNDK.CTRL('P')
DELETE =
"\177".ord
KEY_ESC =
"\033".ord
KEY_RETURN =
"\012".ord
KEY_TAB =
"\t".ord
ALL_SCREENS =

All the screens eventually created.

[]
ALL_WIDGETS =

All the widgets eventually created.

[]
VERSION =
"1.0.0"
VERSION_MAJOR =
VERSION.split('.')[0]
VERSION_MINOR =
VERSION.split('.')[1]
VERSION_PATCH =
VERSION.split('.')[2]

Class Method Summary collapse

Class Method Details

.alignxy(window, xpos, ypos, box_width, box_height) ⇒ Object

Aligns a box on the given window with the width and height given.

x and y position values are like RNDK::CENTER, RNDK::LEFT, RNDK::RIGHT.

xpos, ypos is an Array with exactly one value, an integer.

box_width, box_height is an integer.



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
# File 'lib/rndk.rb', line 204

def RNDK.alignxy (window, xpos, ypos, box_width, box_height)

  # Handling xpos
  first = Ncurses.getbegx window
  last  = Ncurses.getmaxx window

  gap = 0 if (gap = (last - box_width)) < 0

  last = first + gap

  case xpos[0]
  when LEFT   then xpos[0] = first
  when RIGHT  then xpos[0] = first +  gap
  when CENTER then xpos[0] = first + (gap / 2)
  else
    xpos[0] = last  if xpos[0] > last
    xpos[0] = first if xpos[0] < first
  end

  # Handling ypos
  first = Ncurses.getbegy window
  last  = Ncurses.getmaxy window

  gap = 0 if (gap = (last - box_height)) < 0

  last = first + gap

  case ypos[0]
  when TOP    then ypos[0] = first
  when BOTTOM then ypos[0] = first +  gap
  when CENTER then ypos[0] = first + (gap / 2)
  else
    ypos[0] = last  if ypos[0] > last
    ypos[0] = first if ypos[0] < first
  end
end

.baseName(pathname) ⇒ Object

Returns the filename portion of the given pathname, i.e. after the last slash.

For now this function is just a wrapper for File.basename, being kept for portability.



80
81
82
# File 'lib/rndk/core/utils.rb', line 80

def RNDK.baseName (pathname)
  File.basename(pathname)
end

.beepObject

Beeps then flushes the stdout stream.

Normally it emits an audible bell - if that's not possible it flashes the screen.



102
103
104
105
# File 'lib/rndk.rb', line 102

def RNDK.beep
  Ncurses.beep
  $stdout.flush
end

Sets on/off the blinking cursor.

Returns:

  • The cursor's previous state.



243
244
245
246
247
248
249
250
251
252
# File 'lib/rndk.rb', line 243

def RNDK.blink_cursor option
  ret = false
  if option
    ret = Ncurses.curs_set 1
  else
    ret = Ncurses.curs_set 0
  end

  if ret == 1 then true else false end
end

.char2Chtype(string, to, align) ⇒ Object

Takes a String full of format markers and translates it into a chtype array.

This is better suited to curses because curses uses chtype almost exclusively



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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/rndk/core/markup.rb', line 162

def RNDK.char2Chtype(string, to, align)
  to << 0
  align << LEFT
  result = []

  if string.size > 0
    used = 0

    # The original code makes two passes since it has to pre-allocate space but
    # we should be able to make do with one since we can dynamically size it
    adjust = 0
    attrib = RNDK::Color[:normal]
    last_char = 0
    start = 0
    used = 0
    x = 3

    # Look for an alignment marker.
    if string[0] == L_MARKER
      if string[1] == 'C' && string[2] == R_MARKER
        align[0] = CENTER
        start = 3
      elsif string[1] == 'R' && string[2] == R_MARKER
        align[0] = RIGHT
        start = 3
      elsif string[1] == 'L' && string[2] == R_MARKER
        start = 3
      elsif string[1] == 'B' && string[2] == '='
        # Set the item index value in the string.
        result = [' '.ord, ' '.ord, ' '.ord]

        # Pull out the bullet marker.
        while x < string.size and string[x] != R_MARKER
          result << (string[x].ord | RNDK::Color[:bold])
          x += 1
        end
        adjust = 1

        # Set the alignment variables
        start = x
        used = x
      elsif string[1] == 'I' && string[2] == '='
        from = 3
        x = 0

        while from < string.size && string[from] != Ncurses.R_MARKER
          if RNDK.digit?(string[from])
            adjust = adjust * 10 + string[from].to_i
            x += 1
          end
          from += 1
        end

        start = x + 4
      end
    end

    while adjust > 0
      adjust -= 1
      result << ' '
      used += 1
    end

    # Set the format marker boolean to false
    inside_marker = false

    # Start parsing the character string.
    from = start
    while from < string.size
      # Are we inside a format marker?
      if !inside_marker
        if string[from] == L_MARKER &&
            ['/', '!', '#'].include?(string[from + 1])
          inside_marker = true
        elsif string[from] == "\\" && string[from + 1] == L_MARKER
          from += 1
          result << (string[from].ord | attrib)
          used += 1
          from += 1
        elsif string[from] == "\t"
          begin
            result << ' '
            used += 1
          end while (used & 7).nonzero?
        else
          result << (string[from].ord | attrib)
          used += 1
        end
      else
        case string[from]
        when R_MARKER
          inside_marker = false
        when '#'
          last_char = 0
          case string[from + 2]
          when 'L'
            case string[from + 1]
            when 'L'
              last_char = Ncurses::ACS_LLCORNER
            when 'U'
              last_char = Ncurses::ACS_ULCORNER
            when 'H'
              last_char = Ncurses::ACS_HLINE
            when 'V'
              last_char = Ncurses::ACS_VLINE
            when 'P'
              last_char = Ncurses::ACS_PLUS
            end
          when 'R'
            case string[from + 1]
            when 'L'
              last_char = Ncurses::ACS_LRCORNER
            when 'U'
              last_char = Ncurses::ACS_URCORNER
            end
          when 'T'
            case string[from + 1]
            when 'T'
              last_char = Ncurses::ACS_TTEE
            when 'R'
              last_char = Ncurses::ACS_RTEE
            when 'L'
              last_char = Ncurses::ACS_LTEE
            when 'B'
              last_char = Ncurses::ACS_BTEE
            end
          when 'A'
            case string[from + 1]
            when 'L'
              last_char = Ncurses::ACS_LARROW
            when 'R'
              last_char = Ncurses::ACS_RARROW
            when 'U'
              last_char = Ncurses::ACS_UARROW
            when 'D'
              last_char = Ncurses::ACS_DARROW
            end
          else
            case [string[from + 1], string[from + 2]]
            when ['D', 'I']
              last_char = Ncurses::ACS_DIAMOND
            when ['C', 'B']
              last_char = Ncurses::ACS_CKBOARD
            when ['D', 'G']
              last_char = Ncurses::ACS_DEGREE
            when ['P', 'M']
              last_char = Ncurses::ACS_PLMINUS
            when ['B', 'U']
              last_char = Ncurses::ACS_BULLET
            when ['S', '1']
              last_char = Ncurses::ACS_S1
            when ['S', '9']
              last_char = Ncurses::ACS_S9
            end
          end

          if last_char.nonzero?
            adjust = 1
            from += 2

            if string[from + 1] == '('
              # check for a possible numeric modifier
              from += 2
              adjust = 0

              while from < string.size && string[from] != ')'
                if RNDK.digit?(string[from])
                  adjust = (adjust * 10) + string[from].to_i
                end
                from += 1
              end
            end
          end
          (0...adjust).each do |x|
            result << (last_char | attrib)
            used += 1
          end
        when '/'
          mask = []
          from = RNDK.encodeAttribute(string, from, mask)
          attrib |= mask[0]
        when '!'
          mask = []
          from = RNDK.encodeAttribute(string, from, mask)
          attrib &= ~(mask[0])
        end
      end
      from += 1
    end

    if result.size == 0
      result << attrib
    end
    to[0] = used
  else
    result = []
  end
  return result
end

.char_of(chtype) ⇒ Object



153
154
155
# File 'lib/rndk/core/markup.rb', line 153

def RNDK.char_of chtype
  (chtype.ord & 255).chr
end

This function checks to see if a link has been requested



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rndk/core/utils.rb', line 52

def RNDK.checkForLink (line, filename)
  f_pos = 0
  x = 3
  if line.nil?
    return -1
  end

  # Strip out the filename.
  if line[0] == L_MARKER && line[1] == 'F' && line[2] == '='
    while x < line.size
      if line[x] == R_MARKER
        break
      end
      if f_pos < RNDK_PATHMAX
        filename << line[x]
        f_pos += 1
      end
      x += 1
    end
  end
  return f_pos != 0
end

.chtype2Char(string) ⇒ Object

This returns a string from a chtype array Formatting codes are omitted.



395
396
397
398
399
400
401
402
403
404
405
# File 'lib/rndk/core/markup.rb', line 395

def RNDK.chtype2Char(string)
  newstring = ''

  unless string.nil?
    string.each do |char|
      newstring << RNDK.char_of(char)
    end
  end

  return newstring
end

.chtype2String(string) ⇒ Object

This returns a string from a chtype array Formatting codes are embedded



409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/rndk/core/markup.rb', line 409

def RNDK.chtype2String(string)
  newstring = ''
  unless string.nil?
    need = 0
    (0...string.size).each do |x|
      need = RNDK.decodeAttribute(newstring, need,
                                 x > 0 ? string[x - 1] : 0, string[x])
      newstring << string[x]
    end
  end

  return newstring
end

.cleanChar(s, len, character) ⇒ Object

This sets a blank string to be len of the given character.



132
133
134
# File 'lib/rndk.rb', line 132

def RNDK.cleanChar(s, len, character)
  s << character * len
end

.cleanChtype(s, len, character) ⇒ Object



136
137
138
# File 'lib/rndk.rb', line 136

def RNDK.cleanChtype(s, len, character)
  s.concat(character * len)
end

.cmpStrChstr(str, chstr) ⇒ Object

Compare a regular string to a chtype string



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/rndk/core/markup.rb', line 363

def RNDK.cmpStrChstr(str, chstr)
  i = 0
  r = 0

  if str.nil? && chstr.nil?
    return 0
  elsif str.nil?
    return 1
  elsif chstr.nil?
    return -1
  end

  while i < str.size && i < chstr.size
    if str[r].ord < chstr[r]
      return -1
    elsif str[r].ord > chstr[r]
      return 1
    end
    i += 1
  end

  if str.size < chstr.size
    return -1
  elsif str.size > chstr.size
    return 1
  else
    return 0
  end
end

.CTRL(char) ⇒ Object

Key value when pressing Ctrl+char.



66
67
68
# File 'lib/rndk.rb', line 66

def RNDK.CTRL(char)
  char.ord & 0x1f
end

.decodeAttribute(string, from, oldattr, newattr) ⇒ Object

The reverse of encodeAttribute Well, almost. If attributes such as bold and underline are combined in the same string, we do not necessarily reconstruct them in the same order. Also, alignment markers and tabs are lost.



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/rndk/core/markup.rb', line 495

def RNDK.decodeAttribute (string, from, oldattr, newattr)
  table = {
    'B' => RNDK::Color[:bold],
    'D' => RNDK::Color[:dim],
    'K' => RNDK::Color[:blink],
    'R' => RNDK::Color[:reverse],
    'S' => RNDK::Color[:standout],
    'U' => RNDK::Color[:underline]
  }

  result = if string.nil? then '' else string end
  base_len = result.size
  tmpattr = oldattr & RNDK::Color[:extract]

  newattr &= RNDK::Color[:extract]
  if tmpattr != newattr
    while tmpattr != newattr
      found = false
      table.keys.each do |key|
        if (table[key] & tmpattr) != (table[key] & newattr)
          found = true
          result << RNDK::L_MARKER
          if (table[key] & tmpattr).nonzero?
            result << '!'
            tmpattr &= ~(table[key])
          else
            result << '/'
            tmpattr |= table[key]
          end
          result << key
          break
        end
      end
      # XXX: Only checks if terminal has colours not if colours are started
      if Ncurses.has_colors
        if (tmpattr & Ncurses::A_COLOR) != (newattr & Ncurses::A_COLOR)
          oldpair = Ncurses.PAIR_NUMBER(tmpattr)
          newpair = Ncurses.PAIR_NUMBER(newattr)
          if !found
            found = true
            result << RNDK::L_MARKER
          end
          if newpair.zero?
            result << '!'
            result << oldpair.to_s
          else
            result << '/'
            result << newpair.to_s
          end
          tmpattr &= ~(Ncurses::A_COLOR)
          newattr &= ~(Ncurses::A_COLOR)
        end
      end

      if found
        result << RNDK::R_MARKER
      else
        break
      end
    end
  end

  return from + result.size - base_len
end

.digit?(character) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
# File 'lib/rndk.rb', line 107

def RNDK.digit? character
  return false if character.nil?

  not character.match(/^[[:digit:]]$/).nil?
end

.dirName(pathname) ⇒ Object

Returns the directory for the given pathname, i.e. the part before the last slash For now this function is just a wrapper for File.dirname kept for ease of porting and will be completely replaced in the future



88
89
90
# File 'lib/rndk/core/utils.rb', line 88

def RNDK.dirName (pathname)
  File.dirname(pathname)
end

.encodeAttribute(string, from, mask) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/rndk/core/markup.rb', line 447

def RNDK.encodeAttribute (string, from, mask)
  mask << 0
  case string[from + 1]
  when 'B'
    mask[0] = RNDK::Color[:bold]
  when 'D'
    mask[0] = RNDK::Color[:dim]
  when 'K'
    mask[0] = RNDK::Color[:blink]
  when 'R'
    mask[0] = RNDK::Color[:reverse]
  when 'S'
    mask[0] = RNDK::Color[:standout]
  when 'U'
    mask[0] = RNDK::Color[:underline]
  end

  if mask[0] != 0
    from += 1
  elsif RNDK.digit?(string[from+1]) and RNDK.digit?(string[from + 2])
    if Ncurses.has_colors
      # XXX: Only checks if terminal has colours not if colours are started
      pair = string[from + 1..from + 2].to_i
      mask[0] = Ncurses.COLOR_PAIR(pair)
    else
      mask[0] = Ncurses.A_BOLD
    end

    from += 2
  elsif RNDK.digit?(string[from + 1])
    if Ncurses.has_colors
      # XXX: Only checks if terminal has colours not if colours are started
      pair = string[from + 1].to_i
      mask[0] = Ncurses.COLOR_PAIR(pair)
    else
      mask[0] = Ncurses.A_BOLD
    end

    from += 1
  end

  return from
end

.get_directory_contents(directory) ⇒ Object

Returns all files from directory as an Array of Strings.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rndk/core/utils.rb', line 12

def RNDK.get_directory_contents directory
  list = []

  Dir.foreach(directory) do |filename|
    next if filename == '.' or filename == '..'
    list << filename
  end

  list.sort
end

.intlen(value) ⇒ Object

Number of digits value has when represented as a String.



6
7
8
# File 'lib/rndk/core/utils.rb', line 6

def RNDK.intlen value
  value.to_str.size
end

.is_alpha?(character) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/rndk.rb', line 113

def RNDK.is_alpha? character
  return false if character.nil?

  not character.match(/^[[:alpha:]]$/).nil?
end

.is_char?(character) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# File 'lib/rndk.rb', line 119

def RNDK.is_char? character
  return false if character.nil?

  (character >= 0) and (character < Ncurses::KEY_MIN)
end

.justifyString(box_width, mesg_length, justify) ⇒ Object

This takes a string, a field width, and a justification type and returns the adjustment to make, to fill the justification requirement



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/rndk/core/markup.rb', line 426

def RNDK.justifyString (box_width, mesg_length, justify)

  # make sure the message isn't longer than the width
  # if it is, return 0
  if mesg_length >= box_width
    return 0
  end

  # try to justify the message
  case justify
  when LEFT
    0
  when RIGHT
    box_width - mesg_length
  when CENTER
    (box_width - mesg_length) / 2
  else
    justify
  end
end

.KEY_F(n) ⇒ Object

Returns internal Ncurses' function keys values (F1, F2 ... F12 ...)



127
128
129
# File 'lib/rndk.rb', line 127

def RNDK.KEY_F(n)
  264 + n
end

.read_file(filename) ⇒ Object

Note:

The lines don't end with '\n'.

Reads a file and concatenate it's lines into array.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rndk/core/utils.rb', line 122

def RNDK.read_file filename
  begin
    fd = File.new(filename, "r")
  rescue
    return nil
  end

  lines = fd.readlines.map do |line|
    if line.size > 0 && line[-1] == "\n"
      line[0...-1]
    else
      line
    end
  end
  fd.close

  lines
end

.search_list(list, list_size, pattern) ⇒ Object

This looks for a subset of a word in the given list



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
# File 'lib/rndk/core/utils.rb', line 24

def RNDK.search_list(list, list_size, pattern)
  index = -1

  if pattern.size > 0
    (0...list_size).each do |x|
      len = [list[x].size, pattern.size].min
      ret = (list[x][0...len] <=> pattern)

      # If 'ret' is less than 0 then the current word is alphabetically
      # less than the provided word.  At this point we will set the index
      # to the current position.  If 'ret' is greater than 0, then the
      # current word is alphabetically greater than the given word. We
      # should return with index, which might contain the last best match.
      # If they are equal then we've found it.
      if ret < 0
        index = ret
      else
        if ret == 0
          index = x
        end
        break
      end
    end
  end
  return index
end

.set_widget_dimension(parent_dim, proposed_dim, adjustment) ⇒ Object

If the dimension is a negative value, the dimension will be the full height/width of the parent window - the value of the dimension. Otherwise, the dimension will be the given value.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rndk/core/utils.rb', line 95

def RNDK.set_widget_dimension (parent_dim, proposed_dim, adjustment)
  # If the user passed in FULL, return the parents size
  if proposed_dim == FULL or proposed_dim == 0
    parent_dim
  elsif proposed_dim >= 0
    # if they gave a positive value, return it

    if proposed_dim >= parent_dim
      parent_dim
    else
      proposed_dim + adjustment
    end
  else
    # if they gave a negative value then return the dimension
    # of the parent plus the value given
    #
    if parent_dim + proposed_dim < 0
      parent_dim
    else
      parent_dim + proposed_dim
    end
  end
end

.window_delete(window) ⇒ Object

Safely deletes a raw Ncurses window.



153
154
155
156
157
158
159
# File 'lib/rndk.rb', line 153

def RNDK.window_delete window
  return if window.nil?

  RNDK.window_erase window
  Ncurses.delwin window
  window = nil
end

.window_erase(window) ⇒ Object

Safely erases a raw Ncurses window.



145
146
147
148
149
150
# File 'lib/rndk.rb', line 145

def RNDK.window_erase window
  return if window.nil?

  Ncurses.werase window
  Ncurses.wrefresh window
end

.window_move(window, xdiff, ydiff) ⇒ Object

Safely moves a raw Ncurses window.

Developer Note:

Moves a given window if we're able to set the window's beginning. We do not use mvwin(), because it does not (usually) move subwindows.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rndk.rb', line 167

def RNDK.window_move(window, xdiff, ydiff)
  return if window.nil?

  xpos = []
  ypos = []
  Ncurses.getbegyx(window, ypos, xpos)
  if Ncurses.mvwin(window, ypos[0], xpos[0]) != Ncurses::ERR
    xpos[0] += xdiff
    ypos[0] += ydiff
    Ncurses.werase window
    Ncurses.mvwin(window, ypos[0], xpos[0])
  else
    RNDK.beep
  end
end

.window_refresh(win) ⇒ Object

Refreshes a raw Ncurses window.

Developer Notes

FIXME(original): this should be rewritten to use the panel library, so it would not be necessary to touch the window to ensure that it covers other windows.



190
191
192
193
# File 'lib/rndk.rb', line 190

def RNDK.window_refresh win
  Ncurses.touchwin win
  Ncurses.wrefresh win
end