mattscilipoti-rdialog 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rdialog.rb ADDED
@@ -0,0 +1,585 @@
1
+
2
+ # vim:ts=4:sw=4:
3
+ # = rdialog - A dialog gem for Ruby
4
+ #
5
+ # Homepage:: http://built-it.net/ruby/rdialog/
6
+ # Author:: Aleks Clark (http://built-it.net)
7
+ # Copyright:: (cc) 2004 Aleks Clark
8
+ # License:: BSD
9
+ #
10
+ # class RDialog::Dialog.new( array, str, array)
11
+
12
+ require 'tempfile'
13
+ require 'date'
14
+ class RDialog
15
+ #
16
+ # All accessors are boolean unless otherwise noted.
17
+ #
18
+
19
+ #
20
+ # This gives you some control over the box dimensions when
21
+ # using auto sizing (specifying 0 for height and width).
22
+ # It represents width / height. The default is 9,
23
+ # which means 9 characters wide to every 1 line high.
24
+ #
25
+ attr_accessor :aspect
26
+
27
+ #
28
+ # Specifies a backtitle string to be displayed on the backdrop,
29
+ # at the top of the screen.
30
+ #
31
+ attr_accessor :backtitle
32
+
33
+ #
34
+ # Sound the audible alarm each time the screen is refreshed.
35
+ #
36
+ attr_accessor :beep
37
+
38
+ #
39
+ # Specify the position of the upper left corner of a dialog box
40
+ # on the screen, as an array containing two integers.
41
+ #
42
+ attr_accessor :begin
43
+
44
+ #
45
+ # Interpret embedded newlines in the dialog text as a newline
46
+ # on the screen. Otherwise, dialog will only wrap lines where
47
+ # needed to fit inside the text box. Even though you can control
48
+ # line breaks with this, dialog will still wrap any lines that are
49
+ # too long for the width of the box. Without cr-wrap, the layout
50
+ # of your text may be formatted to look nice in the source code of
51
+ # your script without affecting the way it will look in the dialog.
52
+ #
53
+ attr_accessor :crwrap
54
+
55
+ #
56
+ # Interpret the tags data for checklist, radiolist and menuboxes
57
+ # adding a column which is displayed in the bottom line of the
58
+ # screen, for the currently selected item.
59
+ #
60
+ attr_accessor :itemhelp
61
+
62
+ #
63
+ # Suppress the "Cancel" button in checklist, inputbox and menubox
64
+ # modes. A script can still test if the user pressed the ESC key to
65
+ # cancel to quit.
66
+ #
67
+ attr_accessor :nocancel
68
+
69
+ #
70
+ # Draw a shadow to the right and bottom of each dialog box.
71
+ #
72
+ attr_accessor :shadow
73
+
74
+ #
75
+ # Sleep (delay) for the given integer of seconds after processing
76
+ # a dialog box.
77
+ #
78
+ attr_accessor :sleep
79
+
80
+ #
81
+ # Convert each tab character to one or more spaces.
82
+ # Otherwise, tabs are rendered according to the curses library's
83
+ # interpretation.
84
+ #
85
+ attr_accessor :tabcorrect
86
+
87
+ #
88
+ # Specify the number(int) of spaces that a tab character occupies
89
+ # if the tabcorrect option is set true. The default is 8.
90
+ #
91
+ attr_accessor :tablen
92
+
93
+ #
94
+ # Title string to be displayed at the top of the dialog box.
95
+ #
96
+ attr_accessor :title
97
+
98
+ #
99
+ # Alternate path to dialog. If this is not set, environment path
100
+ # is used.
101
+ attr_accessor :path_to_dialog
102
+
103
+ # Returns a new RDialog Object
104
+
105
+ def initialize()
106
+ end
107
+ # A calendar box displays month, day and year in separately
108
+ # adjustable windows. If the values for day, month or year are
109
+ # missing or negative, the current date's corresponding values
110
+ # are used. You can increment or decrement any of those using
111
+ # the left-, up-, right- and down-arrows. Use vi-style h, j, k
112
+ # and l for moving around the array of days in a month. Use tab
113
+ # or backtab to move between windows. If the year is given as
114
+ # zero, the current date is used as an initial value.
115
+ #
116
+ # Returns a Date object with the selected date
117
+
118
+ def calendar(text="Select a Date", height=0, width=0, day=Date.today.mday(), month=Date.today.mon(), year=Date.today.year())
119
+
120
+ tmp = Tempfile.new('tmp')
121
+
122
+ command = option_string() + "--calendar \"" + text.to_s +
123
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " " +
124
+ day.to_i.to_s + " " + month.to_i.to_s + " " + year.to_i.to_s +
125
+ " 2> " + tmp.path
126
+ success = system(command)
127
+ if success
128
+ date = Date::civil(*tmp.readline.split('/').collect {|i| i.to_i}.reverse)
129
+ tmp.close!
130
+ return date
131
+ else
132
+ tmp.close!
133
+ return success
134
+ end
135
+
136
+ end
137
+
138
+ # A checklist box is similar to a menu box; there are multiple
139
+ # entries presented in the form of a menu. Instead of choosing
140
+ # one entry among the entries, each entry can be turned on or off
141
+ # by the user. The initial on/off state of each entry is speci-
142
+ # fied by status.
143
+
144
+ def checklist(text, items, height=0, width=0, listheight=0)
145
+
146
+ tmp = Tempfile.new('tmp')
147
+
148
+ itemlist = String.new
149
+
150
+ for item in items
151
+ if item[2]
152
+ item[2] = "on"
153
+ else
154
+ item[2] = "off"
155
+ end
156
+ itemlist += "\"" + item[0].to_s + "\" \"" + item[1].to_s +
157
+ "\" " + item[2] + " "
158
+
159
+ if @itemhelp
160
+ itemlist += "\"" + item[3].to_s + "\" "
161
+ end
162
+ end
163
+
164
+ command = option_string() + "--checklist \"" + text.to_s +
165
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s +
166
+ " " + listheight.to_i.to_s + " " + itemlist + "2> " +
167
+ tmp.path
168
+ puts command
169
+ success = system(command)
170
+ puts success
171
+ if success
172
+ selected_string = tmp.readline
173
+ tmp.close!
174
+ selected_string.slice!(0)
175
+ selected_string.chomp!("\"")
176
+ selected_array = selected_string.split('" "')
177
+ for item in selected_array
178
+ item.delete!("\\")
179
+ end
180
+
181
+ return selected_array
182
+ else
183
+ tmp.close!
184
+ return success
185
+ end
186
+
187
+ end
188
+
189
+ # The file-selection dialog displays a text-entry window in which
190
+ # you can type a filename (or directory), and above that two win-
191
+ # dows with directory names and filenames.
192
+
193
+ # Here filepath can be a filepath in which case the file and
194
+ # directory windows will display the contents of the path and the
195
+ # text-entry window will contain the preselected filename.
196
+ #
197
+ # Use tab or arrow keys to move between the windows. Within the
198
+ # directory or filename windows, use the up/down arrow keys to
199
+ # scroll the current selection. Use the space-bar to copy the
200
+ # current selection into the text-entry window.
201
+ #
202
+ # Typing any printable characters switches focus to the text-
203
+ # entry window, entering that character as well as scrolling the
204
+ # directory and filename windows to the closest match.
205
+ #
206
+ # Use a carriage return or the "OK" button to accept the current
207
+ # value in the text-entry window and exit.
208
+
209
+ def fselect(path, height=0, width=0)
210
+ tmp = Tempfile.new('tmp')
211
+
212
+ command = option_string() + "--fselect \"" + path.to_s +
213
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
214
+
215
+ command += "2> " + tmp.path
216
+
217
+ success = system(command)
218
+
219
+ if success
220
+ begin
221
+ selected_string = tmp.readline
222
+ rescue EOFError
223
+ selected_string = ""
224
+ end
225
+ tmp.close!
226
+ return selected_string
227
+ else
228
+ tmp.close!
229
+ return success
230
+ end
231
+ end
232
+ # Does not work. Someone is welcome to try and make it work.
233
+ def gauge(text, height=0, width=0)
234
+ return false
235
+ end
236
+
237
+ # An info box is basically a message box. However, in this case,
238
+ # dialog will exit immediately after displaying the message to
239
+ # the user. The screen is not cleared when dialog exits, so that
240
+ # the message will remain on the screen until the calling shell
241
+ # script clears it later. This is useful when you want to inform
242
+ # the user that some operations are carrying on that may require
243
+ # some time to finish.
244
+ #
245
+ # Returns false if esc was pushed
246
+
247
+ def infobox(text, height=0, width=0)
248
+ command = option_string() + "--infobox \"" + text.to_s +
249
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
250
+ success = system(command)
251
+ return success
252
+ end
253
+
254
+ # A radiolist box is similar to a menu box. The only difference
255
+ # is that you can indicate which entry is currently selected, by
256
+ # setting its status to true.
257
+
258
+ def radiolist(text, items, height=0, width=0, listheight=0)
259
+
260
+ tmp = Tempfile.new('tmp')
261
+
262
+ itemlist = String.new
263
+
264
+ for item in items
265
+ if item[2]
266
+ item[2] = "on"
267
+ else
268
+ item[2] = "off"
269
+ end
270
+ itemlist += "\"" + item[0].to_s + "\" \"" + item[1].to_s +
271
+ "\" " + item[2] + " "
272
+
273
+ if @itemhelp
274
+ itemlist += "\"" + item[3].to_s + "\" "
275
+ end
276
+ end
277
+
278
+ command = option_string() + "--radiolist \"" + text.to_s +
279
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s +
280
+ " " + listheight.to_i.to_s + " " + itemlist + "2> " +
281
+ tmp.path
282
+ success = system(command)
283
+
284
+ if success
285
+ selected_string = tmp.readline
286
+ tmp.close!
287
+ return selected_string
288
+ else
289
+ tmp.close!
290
+ return success
291
+ end
292
+
293
+ end
294
+
295
+ # As its name suggests, a menu box is a dialog box that can be
296
+ # used to present a list of choices in the form of a menu for the
297
+ # user to choose. Choices are displayed in the order given.
298
+ # Each menu entry consists of a tag string and an item string.
299
+ # The tag gives the entry a name to distinguish it from the other
300
+ # entries in the menu. The item is a short description of the
301
+ # option that the entry represents. The user can move between
302
+ # the menu entries by pressing the cursor keys, the first letter
303
+ # of the tag as a hot-key, or the number keys 1-9. There are
304
+ # menu-height entries displayed in the menu at one time, but the
305
+ # menu will be scrolled if there are more entries than that.
306
+ #
307
+ # Returns a string containing the tag of the chosen menu entry.
308
+
309
+ def menu(text="Text Goes Here", items=nil, height=0, width=0, listheight=0)
310
+ tmp = Tempfile.new('tmp')
311
+
312
+ itemlist = String.new
313
+
314
+ for item in items
315
+ itemlist += "\"" + item[0].to_s + "\" \"" + item[1].to_s + "\" "
316
+
317
+ if @itemhelp
318
+ itemlist += "\"" + item[2].to_s + "\" "
319
+ end
320
+ end
321
+
322
+ command = option_string() + "--menu \"" + text.to_s +
323
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s +
324
+ " " + listheight.to_i.to_s + " " + itemlist + "2> " +
325
+ tmp.path
326
+ success = system(command)
327
+
328
+ if success
329
+ selected_string = tmp.readline
330
+ tmp.close!
331
+ return selected_string
332
+ else
333
+ tmp.close!
334
+ return success
335
+ end
336
+
337
+ end
338
+
339
+ # A message box is very similar to a yes/no box. The only dif-
340
+ # ference between a message box and a yes/no box is that a mes-
341
+ # sage box has only a single OK button. You can use this dialog
342
+ # box to display any message you like. After reading the mes-
343
+ # sage, the user can press the ENTER key so that dialog will exit
344
+ # and the calling shell script can continue its operation.
345
+
346
+ def msgbox(text="Text Goes Here", height=0, width=0)
347
+ command = option_string() + "--msgbox \"" + text.to_s +
348
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
349
+
350
+ success = system(command)
351
+ return success
352
+ end
353
+
354
+ # A password box is similar to an input box, except that the text
355
+ # the user enters is not displayed. This is useful when prompt-
356
+ # ing for passwords or other sensitive information. Be aware
357
+ # that if anything is passed in "init", it will be visible in the
358
+ # system's process table to casual snoopers. Also, it is very
359
+ # confusing to the user to provide them with a default password
360
+ # they cannot see. For these reasons, using "init" is highly
361
+ # discouraged.
362
+
363
+ def passwordbox(text="Please enter some text", height=0, width=0, init="")
364
+ tmp = Tempfile.new('tmp')
365
+ command = option_string() + "--passwordbox \"" + text.to_s +
366
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
367
+
368
+ unless init.empty?
369
+ command += init.to_s + " "
370
+ end
371
+
372
+ command += "2> " + tmp.path
373
+
374
+ success = system(command)
375
+
376
+ if success
377
+ begin
378
+ selected_string = tmp.readline
379
+ rescue EOFError
380
+ selected_string = ""
381
+ end
382
+ tmp.close!
383
+ return selected_string
384
+ else
385
+ tmp.close!
386
+ return success
387
+ end
388
+ end
389
+
390
+ # The textbox method handles three similar dialog functions, textbox,
391
+ # tailbox, and tailboxbg. They are activated by setting type to
392
+ # "text", "tail", and "bg" respectively
393
+ #
394
+ # Textbox mode:
395
+ # A text box lets you display the contents of a text file in a
396
+ # dialog box. It is like a simple text file viewer. The user
397
+ # can move through the file by using the cursor, PGUP/PGDN and
398
+ # HOME/END keys available on most keyboards. If the lines are
399
+ # too long to be displayed in the box, the LEFT/RIGHT keys can be
400
+ # used to scroll the text region horizontally. You may also use
401
+ # vi-style keys h, j, k, l in place of the cursor keys, and B or
402
+ # N in place of the pageup/pagedown keys. Scroll up/down using
403
+ # vi-style 'k' and 'j', or arrow-keys. Scroll left/right using
404
+ # vi-style 'h' and 'l', or arrow-keys. A '0' resets the
405
+ # left/right scrolling. For more convenience, vi-style forward
406
+ # and backward searching functions are also provided.
407
+ #
408
+ # Tailbox mode:
409
+ # Display text from a file in a dialog box, as in a "tail -f"
410
+ # command. Scroll left/right using vi-style 'h' and 'l', or
411
+ # arrow-keys. A '0' resets the scrolling.
412
+ #
413
+ # Tailboxbg mode:
414
+ # Display text from a file in a dialog box as a background task,
415
+ # as in a "tail -f &" command. Scroll left/right using vi-style
416
+ # 'h' and 'l', or arrow-keys. A '0' resets the scrolling.
417
+
418
+ def textbox(file, type="text", height=0, width=0)
419
+ case type
420
+ when "text"
421
+ opt = "--textbox"
422
+ when "tail"
423
+ opt = "--tailbox"
424
+ when "bg"
425
+ opt = "--textboxbg"
426
+ end
427
+
428
+ command = option_string() + opt +" \"" + file.to_s +
429
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
430
+
431
+ success = system(command)
432
+
433
+ return success
434
+ end
435
+
436
+ # A dialog is displayed which allows you to select hour, minute
437
+ # and second. If the values for hour, minute or second are miss-
438
+ # ing or negative, the current date's corresponding values are
439
+ # used. You can increment or decrement any of those using the
440
+ # left-, up-, right- and down-arrows. Use tab or backtab to move
441
+ # between windows.
442
+ #
443
+ # On exit, a Time object is returned.
444
+
445
+ def timebox(file, type="text", height=0, width=0, time=Time.now)
446
+ tmp = Tempfile.new('tmp')
447
+
448
+ command = option_string() + "--timebox \"" + text.to_s +
449
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " " +
450
+ time.hour.to_s + " " + time.min.to_s + " " +
451
+ time.sec.to_s + " 2> " + tmp.path
452
+ success = system(command)
453
+ if success
454
+ time = Time.parse(tmp.readline)
455
+ tmp.close!
456
+ return time
457
+ else
458
+ tmp.close!
459
+ return success
460
+ end
461
+
462
+ end
463
+
464
+ # An input box is useful when you want to ask questions that
465
+ # require the user to input a string as the answer. If init is
466
+ # supplied it is used to initialize the input string. When
467
+ # entering the string, the backspace, delete and cursor keys can
468
+ # be used to correct typing errors. If the input string is
469
+ # longer than can fit in the dialog box, the input field will be
470
+ # scrolled.
471
+ #
472
+ # On exit, the input string will be returned.
473
+
474
+
475
+ def inputbox(text="Please enter some text", height=0, width=0, init="")
476
+ tmp = Tempfile.new('tmp')
477
+
478
+ command = option_string() + "--inputbox \"" + text.to_s +
479
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s + " "
480
+
481
+ unless init.empty?
482
+ command += init.to_s + " "
483
+ end
484
+
485
+ command += "2> " + tmp.path
486
+
487
+ success = system(command)
488
+
489
+ if success
490
+ begin
491
+ selected_string = tmp.readline
492
+ rescue EOFError
493
+ selected_string = ""
494
+ end
495
+ tmp.close!
496
+ return selected_string
497
+ else
498
+ tmp.close!
499
+ return success
500
+ end
501
+ end
502
+
503
+ # A yes/no dialog box of size height rows by width columns will
504
+ # be displayed. The string specified by text is displayed inside
505
+ # the dialog box. If this string is too long to fit in one line,
506
+ # it will be automatically divided into multiple lines at appro-
507
+ # priate places. The text string can also contain the sub-string
508
+ # "\n" or newline characters '\n' to control line breaking
509
+ # explicitly. This dialog box is useful for asking questions
510
+ # that require the user to answer either yes or no. The dialog
511
+ # box has a Yes button and a No button, in which the user can
512
+ # switch between by pressing the TAB key.
513
+
514
+ def yesno(text="Please enter some text", height=0, width=0)
515
+ command = option_string() + "--inputbox \"" + text.to_s +
516
+ "\" " + height.to_i.to_s + " " + width.to_i.to_s
517
+
518
+ success = system(command)
519
+ return success
520
+ end
521
+
522
+ private
523
+
524
+ def option_string()
525
+ unless @path_to_dialog
526
+ ostring = "dialog "
527
+ else
528
+ ostring = @path_to_dialog + " "
529
+ end
530
+ if @aspect
531
+ ostring += "--aspect " + aspect + " "
532
+ end
533
+
534
+ if @beep
535
+ ostring += "--beep "
536
+ end
537
+
538
+ if @boxbegin
539
+ ostring += "--begin " + @boxbegin[0] + @boxbegin[1] + " "
540
+ end
541
+
542
+ if @backtitle
543
+ ostring += "--backtitle \"" + @backtitle + "\" "
544
+ end
545
+
546
+ if @itemhelp
547
+ ostring += "--item-help "
548
+ end
549
+
550
+ unless @shadow == nil
551
+ if @shadow == true
552
+ ostring += "--shadow "
553
+ else
554
+ ostring += "--no-shadow "
555
+ end
556
+ end
557
+
558
+ if @sleep
559
+ ostring += "--sleep " + @sleep.to_i + " "
560
+ end
561
+
562
+ if @tabcorrect
563
+ ostring += "--tab-correct "
564
+ end
565
+
566
+ if @tablen
567
+ ostring += "--tab-len " + @tablen.to_i + " "
568
+ end
569
+
570
+ if @title
571
+ ostring += "--title " + @title.to_s + " "
572
+ end
573
+
574
+ if @nocancel
575
+ ostring += "--nocancel "
576
+ end
577
+
578
+ return ostring
579
+
580
+ end
581
+ end
582
+
583
+
584
+
585
+ #Dir[File.join(File.dirname(__FILE__), 'rdialog/**/*.rb')].sort.each { |lib| require lib }
data/scripts/txt2html ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'redcloth'
5
+ require 'syntax/convertors/html'
6
+ require 'erb'
7
+ require File.dirname(__FILE__) + '/../lib/rdialog/version.rb'
8
+
9
+ version = Rdialog::VERSION::STRING
10
+ download = 'http://rubyforge.org/projects/rdialog'
11
+
12
+ class Fixnum
13
+ def ordinal
14
+ # teens
15
+ return 'th' if (10..19).include?(self % 100)
16
+ # others
17
+ case self % 10
18
+ when 1: return 'st'
19
+ when 2: return 'nd'
20
+ when 3: return 'rd'
21
+ else return 'th'
22
+ end
23
+ end
24
+ end
25
+
26
+ class Time
27
+ def pretty
28
+ return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
29
+ end
30
+ end
31
+
32
+ def convert_syntax(syntax, source)
33
+ return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
34
+ end
35
+
36
+ if ARGV.length >= 1
37
+ src, template = ARGV
38
+ template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
39
+
40
+ else
41
+ puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
42
+ exit!
43
+ end
44
+
45
+ template = ERB.new(File.open(template).read)
46
+
47
+ title = nil
48
+ body = nil
49
+ File.open(src) do |fsrc|
50
+ title_text = fsrc.readline
51
+ body_text = fsrc.read
52
+ syntax_items = []
53
+ body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
54
+ ident = syntax_items.length
55
+ element, syntax, source = $1, $2, $3
56
+ syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
57
+ "syntax-temp-#{ident}"
58
+ }
59
+ title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
60
+ body = RedCloth.new(body_text).to_html
61
+ body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
62
+ end
63
+ stat = File.stat(src)
64
+ created = stat.ctime
65
+ modified = stat.mtime
66
+
67
+ $stdout << template.result(binding)