rndk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/demos/appointment.rb +40 -25
  3. data/demos/clock.rb +22 -11
  4. data/demos/fileview.rb +141 -0
  5. data/examples/01-hello-world.rb +1 -2
  6. data/examples/02-colors.rb +58 -0
  7. data/examples/03-markup.rb +70 -0
  8. data/examples/04-quick-widgets.rb +72 -0
  9. data/examples/05-position-widget.rb +3 -6
  10. data/examples/calendar.rb +104 -0
  11. data/examples/scroll.rb +106 -0
  12. data/lib/rndk/alphalist.rb +14 -14
  13. data/lib/rndk/button.rb +21 -21
  14. data/lib/rndk/buttonbox.rb +18 -18
  15. data/lib/rndk/calendar.rb +333 -240
  16. data/lib/rndk/core/color.rb +136 -0
  17. data/lib/rndk/core/display.rb +23 -12
  18. data/lib/rndk/core/draw.rb +32 -26
  19. data/lib/rndk/core/markup.rb +561 -0
  20. data/lib/rndk/core/quick_widgets.rb +232 -12
  21. data/lib/rndk/core/screen.rb +16 -17
  22. data/lib/rndk/core/utils.rb +143 -0
  23. data/lib/rndk/core/widget.rb +133 -92
  24. data/lib/rndk/dialog.rb +17 -17
  25. data/lib/rndk/entry.rb +21 -21
  26. data/lib/rndk/fselect.rb +16 -16
  27. data/lib/rndk/graph.rb +10 -10
  28. data/lib/rndk/histogram.rb +10 -10
  29. data/lib/rndk/itemlist.rb +20 -20
  30. data/lib/rndk/label.rb +66 -45
  31. data/lib/rndk/marquee.rb +10 -10
  32. data/lib/rndk/matrix.rb +27 -27
  33. data/lib/rndk/mentry.rb +22 -22
  34. data/lib/rndk/menu.rb +14 -14
  35. data/lib/rndk/radio.rb +19 -19
  36. data/lib/rndk/scale.rb +21 -21
  37. data/lib/rndk/scroll.rb +19 -19
  38. data/lib/rndk/scroller.rb +2 -0
  39. data/lib/rndk/selection.rb +20 -20
  40. data/lib/rndk/slider.rb +21 -21
  41. data/lib/rndk/swindow.rb +20 -20
  42. data/lib/rndk/template.rb +21 -21
  43. data/lib/rndk/version.rb +1 -1
  44. data/lib/rndk/viewer.rb +18 -18
  45. data/lib/rndk.rb +99 -777
  46. data/rndk.gemspec +1 -1
  47. metadata +12 -3
@@ -0,0 +1,561 @@
1
+ # # RNDK String Markup
2
+ #
3
+ # RNDK has special formatting commands which can be included
4
+ # in any String which add highlights, justification, or even
5
+ # colors to a basic String.
6
+ #
7
+ # These attributes, once set, remain in effect until changed
8
+ # explicitly, or until the end of the string.
9
+ #
10
+ # ## Colors
11
+ #
12
+ # RNDK has the capability to display colors in almost any text
13
+ # inside a Widget.
14
+ #
15
+ # To turn on colors, the function #initCDKColor has to be called. When
16
+ # this function is called 64 color pairs are created. Normally the
17
+ # color pairs are accessed via the COLOR_PAIR macro. You can still do
18
+ # this, but creating a string with multiple colors gets terribly
19
+ # difficult. That is why the color commands were created.
20
+ #
21
+ # The color settings are stored directly in the string. When the
22
+ # widget is created or activated, the string is converted to take
23
+ # advan- tage of any color commands in the string. To turn on a color
24
+ # pair insert </XX> into the string; where XX is a numeric value from
25
+ # 0 to 64. Color pair 0 is the standard default color pair for the
26
+ # screen. To turn off a color pair use the format command <!XX> where
27
+ # XX is a numeric value from 0 to 64.
28
+ #
29
+ # For example:
30
+ #
31
+ # "</31>This line should have a yellow foreground and a cyan background.<!31>"
32
+ # "</05>This line should have a white foreground and a blue background.<!05>"
33
+ # "</26>This line should have a yellow foreground and a red background.<!26>"
34
+ # "<C>This line should be set to whatever the screen default is."
35
+ #
36
+ # ## Attributes
37
+ #
38
+ # RNDK also provides attribute commands which allow different
39
+ # character attributes to be displayed in a Widget.
40
+ #
41
+ # To use a character attribute the format command is `</X>` where X is
42
+ # one of several command characters. To turn a attribute off use the
43
+ # command `<!X>`.
44
+ #
45
+ # Here's the command characters supported:
46
+ #
47
+ # B:: Bold
48
+ # U:: Underline
49
+ # K:: Blink
50
+ # R:: Reverse
51
+ # S:: Standout
52
+ # D:: Dim
53
+ # N:: Normal
54
+ #
55
+ # For example:
56
+ #
57
+ # "</B/31>Bold text yellow foreground / blue background.<!31>"
58
+ # "</U/05>Underlined text white foreground / blue background.<!05>"
59
+ # "</K/26>Blinking text yellow foreground / red background.<!26>"
60
+ # "<C>This line uses the screen default colors."
61
+ #
62
+ # ## Justification
63
+ #
64
+ # Justification commands can **left justify**, **right justify**, or
65
+ # **center** a string of text.
66
+ #
67
+ # A format command must be at the **beginning** of the string.
68
+ #
69
+ # To use a justification format in a string the command `<X>` is used.
70
+ #
71
+ # Format commands:
72
+ #
73
+ # <L>:: Left Justified. Default if not stated.
74
+ # <C>:: Centered text.
75
+ # <R>:: Right justified.
76
+ # <I=X>:: Indent the line X characters.
77
+ # <B=X>:: Bullet. X is the bullet string to use.
78
+ # <F=X>:: Links in a file where X is the filename. This works only with the viewer widget.
79
+ #
80
+ # For example:
81
+ #
82
+ # "<R></B/31>This line should have a yellow foreground and a blue background.<!31>"
83
+ # "</U/05>This line should have a white foreground and a blue background.<!05>"
84
+ # "<B=+>This is a bullet."
85
+ # "<I=10>This is indented 10 characters."
86
+ # "<C>This line should be set to whatever the screen default is."
87
+ #
88
+ # The bullet format command can take either a single character or a
89
+ # string. The bullet in the above example would look like
90
+ #
91
+ # + This is a bullet.
92
+ #
93
+ # but if we were to use the following command instead
94
+ #
95
+ # <B=***>This is a bullet.
96
+ #
97
+ # it would look like
98
+ #
99
+ # *** This is a bullet.
100
+ #
101
+ # ## Special Drawing Characters
102
+ #
103
+ # RNDK has a set of special drawing characters which can be inserted
104
+ # into any ASCII file. In order to use a special character the format
105
+ # command `<#XXX>` is used.
106
+ #
107
+ # Special character commands:
108
+ #
109
+ # <#UL>:: Upper Left Corner
110
+ # <#UR>:: Upper Right Corner
111
+ # <#LL>:: Lower Left Corner
112
+ # <#LR>:: Lower Right Corner
113
+ #
114
+ # <#LT>:: Left Tee
115
+ # <#RT>:: Right Tee
116
+ # <#TT>:: Top Tee
117
+ # <#BT>:: Bottom Tee
118
+ #
119
+ # <#HL>:: Horizontal Line
120
+ # <#VL>:: Vertical Line
121
+ #
122
+ # <#PL>:: Plus Sign
123
+ # <#PM>:: Plus or Minus Sign
124
+ # <#DG>:: Degree Sign
125
+ # <#CB>:: Checker Board
126
+ # <#DI>:: Diamond
127
+ # <#BU>:: Bullet
128
+ # <#S1>:: Scan line 1
129
+ # <#S9>:: Scan line 9
130
+ #
131
+ # <#LA>:: Left Arrow
132
+ # <#RA>:: Right Arrow
133
+ # <#TA>:: Top Arrow
134
+ # <#BA>:: Bottom Arrow
135
+ #
136
+ # The character formats can be repeated using an optional numeric
137
+ # repeat value. To repeat a character add the repeat count within
138
+ # parentheses to the end of the character format.
139
+ #
140
+ # The following example draws 10 horizontal-line characters:
141
+ #
142
+ # <#HL(10)>
143
+ #
144
+ # And the most complex example until now. Guess what it does:
145
+ #
146
+ # "<C><#UL><#HL(26)><#UR>"
147
+ # "<C><#VL></R>This text should be boxed.<!R><#VL>"
148
+ # "<C><#LL><#HL(26)><#LR>"
149
+ # "<C>While this is not."
150
+
151
+ module RNDK
152
+
153
+ # Takes a String full of format markers and translates it
154
+ # into a chtype array.
155
+ #
156
+ # This is better suited to curses because curses uses
157
+ # chtype almost exclusively
158
+ def RNDK.char2Chtype(string, to, align)
159
+ to << 0
160
+ align << LEFT
161
+ result = []
162
+
163
+ if string.size > 0
164
+ used = 0
165
+
166
+ # The original code makes two passes since it has to pre-allocate space but
167
+ # we should be able to make do with one since we can dynamically size it
168
+ adjust = 0
169
+ attrib = Ncurses::A_NORMAL
170
+ last_char = 0
171
+ start = 0
172
+ used = 0
173
+ x = 3
174
+
175
+ # Look for an alignment marker.
176
+ if string[0] == L_MARKER
177
+ if string[1] == 'C' && string[2] == R_MARKER
178
+ align[0] = CENTER
179
+ start = 3
180
+ elsif string[1] == 'R' && string[2] == R_MARKER
181
+ align[0] = RIGHT
182
+ start = 3
183
+ elsif string[1] == 'L' && string[2] == R_MARKER
184
+ start = 3
185
+ elsif string[1] == 'B' && string[2] == '='
186
+ # Set the item index value in the string.
187
+ result = [' '.ord, ' '.ord, ' '.ord]
188
+
189
+ # Pull out the bullet marker.
190
+ while x < string.size and string[x] != R_MARKER
191
+ result << (string[x].ord | Ncurses::A_BOLD)
192
+ x += 1
193
+ end
194
+ adjust = 1
195
+
196
+ # Set the alignment variables
197
+ start = x
198
+ used = x
199
+ elsif string[1] == 'I' && string[2] == '='
200
+ from = 3
201
+ x = 0
202
+
203
+ while from < string.size && string[from] != Ncurses.R_MARKER
204
+ if RNDK.digit?(string[from])
205
+ adjust = adjust * 10 + string[from].to_i
206
+ x += 1
207
+ end
208
+ from += 1
209
+ end
210
+
211
+ start = x + 4
212
+ end
213
+ end
214
+
215
+ while adjust > 0
216
+ adjust -= 1
217
+ result << ' '
218
+ used += 1
219
+ end
220
+
221
+ # Set the format marker boolean to false
222
+ inside_marker = false
223
+
224
+ # Start parsing the character string.
225
+ from = start
226
+ while from < string.size
227
+ # Are we inside a format marker?
228
+ if !inside_marker
229
+ if string[from] == L_MARKER &&
230
+ ['/', '!', '#'].include?(string[from + 1])
231
+ inside_marker = true
232
+ elsif string[from] == "\\" && string[from + 1] == L_MARKER
233
+ from += 1
234
+ result << (string[from].ord | attrib)
235
+ used += 1
236
+ from += 1
237
+ elsif string[from] == "\t"
238
+ begin
239
+ result << ' '
240
+ used += 1
241
+ end while (used & 7).nonzero?
242
+ else
243
+ result << (string[from].ord | attrib)
244
+ used += 1
245
+ end
246
+ else
247
+ case string[from]
248
+ when R_MARKER
249
+ inside_marker = false
250
+ when '#'
251
+ last_char = 0
252
+ case string[from + 2]
253
+ when 'L'
254
+ case string[from + 1]
255
+ when 'L'
256
+ last_char = Ncurses::ACS_LLCORNER
257
+ when 'U'
258
+ last_char = Ncurses::ACS_ULCORNER
259
+ when 'H'
260
+ last_char = Ncurses::ACS_HLINE
261
+ when 'V'
262
+ last_char = Ncurses::ACS_VLINE
263
+ when 'P'
264
+ last_char = Ncurses::ACS_PLUS
265
+ end
266
+ when 'R'
267
+ case string[from + 1]
268
+ when 'L'
269
+ last_char = Ncurses::ACS_LRCORNER
270
+ when 'U'
271
+ last_char = Ncurses::ACS_URCORNER
272
+ end
273
+ when 'T'
274
+ case string[from + 1]
275
+ when 'T'
276
+ last_char = Ncurses::ACS_TTEE
277
+ when 'R'
278
+ last_char = Ncurses::ACS_RTEE
279
+ when 'L'
280
+ last_char = Ncurses::ACS_LTEE
281
+ when 'B'
282
+ last_char = Ncurses::ACS_BTEE
283
+ end
284
+ when 'A'
285
+ case string[from + 1]
286
+ when 'L'
287
+ last_char = Ncurses::ACS_LARROW
288
+ when 'R'
289
+ last_char = Ncurses::ACS_RARROW
290
+ when 'U'
291
+ last_char = Ncurses::ACS_UARROW
292
+ when 'D'
293
+ last_char = Ncurses::ACS_DARROW
294
+ end
295
+ else
296
+ case [string[from + 1], string[from + 2]]
297
+ when ['D', 'I']
298
+ last_char = Ncurses::ACS_DIAMOND
299
+ when ['C', 'B']
300
+ last_char = Ncurses::ACS_CKBOARD
301
+ when ['D', 'G']
302
+ last_char = Ncurses::ACS_DEGREE
303
+ when ['P', 'M']
304
+ last_char = Ncurses::ACS_PLMINUS
305
+ when ['B', 'U']
306
+ last_char = Ncurses::ACS_BULLET
307
+ when ['S', '1']
308
+ last_char = Ncurses::ACS_S1
309
+ when ['S', '9']
310
+ last_char = Ncurses::ACS_S9
311
+ end
312
+ end
313
+
314
+ if last_char.nonzero?
315
+ adjust = 1
316
+ from += 2
317
+
318
+ if string[from + 1] == '('
319
+ # check for a possible numeric modifier
320
+ from += 2
321
+ adjust = 0
322
+
323
+ while from < string.size && string[from] != ')'
324
+ if RNDK.digit?(string[from])
325
+ adjust = (adjust * 10) + string[from].to_i
326
+ end
327
+ from += 1
328
+ end
329
+ end
330
+ end
331
+ (0...adjust).each do |x|
332
+ result << (last_char | attrib)
333
+ used += 1
334
+ end
335
+ when '/'
336
+ mask = []
337
+ from = RNDK.encodeAttribute(string, from, mask)
338
+ attrib |= mask[0]
339
+ when '!'
340
+ mask = []
341
+ from = RNDK.encodeAttribute(string, from, mask)
342
+ attrib &= ~(mask[0])
343
+ end
344
+ end
345
+ from += 1
346
+ end
347
+
348
+ if result.size == 0
349
+ result << attrib
350
+ end
351
+ to[0] = used
352
+ else
353
+ result = []
354
+ end
355
+ return result
356
+ end
357
+
358
+ # Compare a regular string to a chtype string
359
+ def RNDK.cmpStrChstr(str, chstr)
360
+ i = 0
361
+ r = 0
362
+
363
+ if str.nil? && chstr.nil?
364
+ return 0
365
+ elsif str.nil?
366
+ return 1
367
+ elsif chstr.nil?
368
+ return -1
369
+ end
370
+
371
+ while i < str.size && i < chstr.size
372
+ if str[r].ord < chstr[r]
373
+ return -1
374
+ elsif str[r].ord > chstr[r]
375
+ return 1
376
+ end
377
+ i += 1
378
+ end
379
+
380
+ if str.size < chstr.size
381
+ return -1
382
+ elsif str.size > chstr.size
383
+ return 1
384
+ else
385
+ return 0
386
+ end
387
+ end
388
+
389
+ def RNDK.CharOf chtype
390
+ (chtype.ord & 255).chr
391
+ end
392
+
393
+ # This returns a string from a chtype array
394
+ # Formatting codes are omitted.
395
+ def RNDK.chtype2Char(string)
396
+ newstring = ''
397
+
398
+ unless string.nil?
399
+ string.each do |char|
400
+ newstring << RNDK.CharOf(char)
401
+ end
402
+ end
403
+
404
+ return newstring
405
+ end
406
+
407
+ # This returns a string from a chtype array
408
+ # Formatting codes are embedded
409
+ def RNDK.chtype2String(string)
410
+ newstring = ''
411
+ unless string.nil?
412
+ need = 0
413
+ (0...string.size).each do |x|
414
+ need = RNDK.decodeAttribute(newstring, need,
415
+ x > 0 ? string[x - 1] : 0, string[x])
416
+ newstring << string[x]
417
+ end
418
+ end
419
+
420
+ return newstring
421
+ end
422
+
423
+ # This takes a string, a field width, and a justification type
424
+ # and returns the adjustment to make, to fill the justification
425
+ # requirement
426
+ def RNDK.justifyString (box_width, mesg_length, justify)
427
+
428
+ # make sure the message isn't longer than the width
429
+ # if it is, return 0
430
+ if mesg_length >= box_width
431
+ return 0
432
+ end
433
+
434
+ # try to justify the message
435
+ case justify
436
+ when LEFT
437
+ 0
438
+ when RIGHT
439
+ box_width - mesg_length
440
+ when CENTER
441
+ (box_width - mesg_length) / 2
442
+ else
443
+ justify
444
+ end
445
+ end
446
+
447
+ def RNDK.encodeAttribute (string, from, mask)
448
+ mask << 0
449
+ case string[from + 1]
450
+ when 'B'
451
+ mask[0] = Ncurses::A_BOLD
452
+ when 'D'
453
+ mask[0] = Ncurses::A_DIM
454
+ when 'K'
455
+ mask[0] = Ncurses::A_BLINK
456
+ when 'R'
457
+ mask[0] = Ncurses::A_REVERSE
458
+ when 'S'
459
+ mask[0] = Ncurses::A_STANDOUT
460
+ when 'U'
461
+ mask[0] = Ncurses::A_UNDERLINE
462
+ end
463
+
464
+ if mask[0] != 0
465
+ from += 1
466
+ elsif RNDK.digit?(string[from+1]) and RNDK.digit?(string[from + 2])
467
+ if Ncurses.has_colors
468
+ # XXX: Only checks if terminal has colours not if colours are started
469
+ pair = string[from + 1..from + 2].to_i
470
+ mask[0] = Ncurses.COLOR_PAIR(pair)
471
+ else
472
+ mask[0] = Ncurses.A_BOLD
473
+ end
474
+
475
+ from += 2
476
+ elsif RNDK.digit?(string[from + 1])
477
+ if Ncurses.has_colors
478
+ # XXX: Only checks if terminal has colours not if colours are started
479
+ pair = string[from + 1].to_i
480
+ mask[0] = Ncurses.COLOR_PAIR(pair)
481
+ else
482
+ mask[0] = Ncurses.A_BOLD
483
+ end
484
+
485
+ from += 1
486
+ end
487
+
488
+ return from
489
+ end
490
+
491
+ # The reverse of encodeAttribute
492
+ # Well, almost. If attributes such as bold and underline are combined in the
493
+ # same string, we do not necessarily reconstruct them in the same order.
494
+ # Also, alignment markers and tabs are lost.
495
+ def RNDK.decodeAttribute (string, from, oldattr, newattr)
496
+ table = {
497
+ 'B' => Ncurses::A_BOLD,
498
+ 'D' => Ncurses::A_DIM,
499
+ 'K' => Ncurses::A_BLINK,
500
+ 'R' => Ncurses::A_REVERSE,
501
+ 'S' => Ncurses::A_STANDOUT,
502
+ 'U' => Ncurses::A_UNDERLINE
503
+ }
504
+
505
+ result = if string.nil? then '' else string end
506
+ base_len = result.size
507
+ tmpattr = oldattr & Ncurses::A_ATTRIBUTES
508
+
509
+ newattr &= Ncurses::A_ATTRIBUTES
510
+ if tmpattr != newattr
511
+ while tmpattr != newattr
512
+ found = false
513
+ table.keys.each do |key|
514
+ if (table[key] & tmpattr) != (table[key] & newattr)
515
+ found = true
516
+ result << RNDK::L_MARKER
517
+ if (table[key] & tmpattr).nonzero?
518
+ result << '!'
519
+ tmpattr &= ~(table[key])
520
+ else
521
+ result << '/'
522
+ tmpattr |= table[key]
523
+ end
524
+ result << key
525
+ break
526
+ end
527
+ end
528
+ # XXX: Only checks if terminal has colours not if colours are started
529
+ if Ncurses.has_colors
530
+ if (tmpattr & Ncurses::A_COLOR) != (newattr & Ncurses::A_COLOR)
531
+ oldpair = Ncurses.PAIR_NUMBER(tmpattr)
532
+ newpair = Ncurses.PAIR_NUMBER(newattr)
533
+ if !found
534
+ found = true
535
+ result << RNDK::L_MARKER
536
+ end
537
+ if newpair.zero?
538
+ result << '!'
539
+ result << oldpair.to_s
540
+ else
541
+ result << '/'
542
+ result << newpair.to_s
543
+ end
544
+ tmpattr &= ~(Ncurses::A_COLOR)
545
+ newattr &= ~(Ncurses::A_COLOR)
546
+ end
547
+ end
548
+
549
+ if found
550
+ result << RNDK::R_MARKER
551
+ else
552
+ break
553
+ end
554
+ end
555
+ end
556
+
557
+ return from + result.size - base_len
558
+ end
559
+
560
+ end
561
+