-
Notifications
You must be signed in to change notification settings - Fork 31
/
tpp.rb
executable file
·1765 lines (1512 loc) · 38.5 KB
/
tpp.rb
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
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
490
491
492
493
494
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
require 'io/console'
version_number = "1.3.1"
# Loads the ncurses-ruby module and imports "Ncurses" into the
# current namespace. It stops the program if loading the
# ncurses-ruby module fails.
def load_ncurses
begin
require "ncurses"
include Ncurses
rescue LoadError
$stderr.print <<EOF
There is no Ncurses-Ruby package installed which is needed by TPP.
You can download it on: http://ncurses-ruby.berlios.de/
EOF
Kernel.exit(1)
end
end
# Maps color names to constants and indexes.
class ColorMap
# Maps color name _color_ to a constant
def ColorMap.get_color(color)
colors = { "white" => COLOR_WHITE,
"yellow" => COLOR_YELLOW,
"red" => COLOR_RED,
"green" => COLOR_GREEN,
"blue" => COLOR_BLUE,
"cyan" => COLOR_CYAN,
"magenta" => COLOR_MAGENTA,
"black" => COLOR_BLACK,
"default" => -1 }
colors[color]
end
# Maps color name to a color pair index
def ColorMap.get_color_pair(color)
colors = { "white" => 1,
"yellow" => 2,
"red" => 3,
"green" => 4,
"blue" => 5,
"cyan" => 6,
"magenta" => 7,
"black" => 8,
"default" =>-1}
colors[color]
end
end
# Opens a TPP source file, and splits it into the different pages.
class FileParser
def initialize(filename)
@filename = filename
@pages = []
end
# Parses the specified file and returns an array of Page objects
def get_pages
begin
f = File.open(@filename)
rescue
$stderr.puts "Error: couldn't open file: #{$!}"
Kernel.exit(1)
end
number_pages = 0
cur_page = Page.new("Title")
f.each_line do |line|
line.chomp!
case line
when /^--##/ # ignore comments
when /^--newpage/
@pages << cur_page
number_pages += 1
name = line.sub(/^--newpage/,"")
if name == "" then
name = "slide " + (number_pages+1).to_s
else
name.strip!
end
cur_page = Page.new(name)
else
cur_page.add_line(line)
end # case
end # each
@pages << cur_page
end
end # class FileParser
# Represents a page (aka `slide') in TPP. A page consists of a title and one or
# more lines.
class Page
def initialize(title)
@lines = []
@title = title
@cur_line = 0
@eop = false
end
# Appends a line to the page, but only if _line_ is not null
def add_line(line)
@lines << line if line
if line =~ /^\$\$/ or line =~ /^\$\%/
prefix = ''
prefix = '%' if line =~ /^\$\%/
cmd = line[2..-1]
begin
op = IO.popen(cmd,"r")
op.readlines.each do |out_line|
@lines << prefix + out_line
end
op.close
rescue => e
@lines << e.to_s
end
end
end
# Returns the next line. In case the last line is hit, then the end-of-page marker is set.
def next_line
line = @lines[@cur_line]
@cur_line += 1
if @cur_line >= @lines.size then
@eop = true
end
return line
end
# Returns whether end-of-page has been reached.
def eop?
@eop
end
# Resets the end-of-page marker and sets the current line marker to the first line
def reset_eop
@cur_line = 0
@eop = false
end
# Returns all lines in the page.
def lines
@lines
end
# Returns the page's title
def title
@title
end
end
# Implements a generic visualizer from which all other visualizers need to be
# derived. If Ruby supported abstract methods, all the do_* methods would be
# abstract.
class TppVisualizer
def initialize
# nothing
end
# Splits a line into several lines, where each of the result lines is at most
# _width_ characters long, caring about word boundaries, and returns an array
# of strings.
def split_lines(text,width)
lines = []
if text then
begin
i = width
if text.length <= i then # text length is OK -> add it to array and stop splitting
lines << text
text = ""
else
# search for word boundary (space actually)
while i > 0 and text[i] != ' '[0] do
i -= 1
end
# if we can't find any space character, simply cut it off at the maximum width
if i == 0 then
i = width
end
# extract line
x = text[0..i-1]
# remove extracted line
text = text[i+1..-1]
# added line to array
lines << x
end
end while text.length > 0
end
return lines
end
def do_footer(footer_text)
$stderr.puts "Error: TppVisualizer#do_footer has been called directly."
Kernel.exit(1)
end
def do_header(header_text)
$stderr.puts "Error: TppVisualizer#do_header has been called directly."
Kernel.exit(1)
end
def do_refresh
$stderr.puts "Error: TppVisualizer#do_refresh has been called directly."
Kernel.exit(1)
end
def new_page
$stderr.puts "Error: TppVisualizer#new_page has been called directly."
Kernel.exit(1)
end
def do_heading(text)
$stderr.puts "Error: TppVisualizer#do_heading has been called directly."
Kernel.exit(1)
end
def do_withborder
$stderr.puts "Error: TppVisualizer#do_withborder has been called directly."
Kernel.exit(1)
end
def do_horline
$stderr.puts "Error: TppVisualizer#do_horline has been called directly."
Kernel.exit(1)
end
def do_color(text)
$stderr.puts "Error: TppVisualizer#do_color has been called directly."
Kernel.exit(1)
end
def do_center(text)
$stderr.puts "Error: TppVisualizer#do_center has been called directly."
Kernel.exit(1)
end
def do_right(text)
$stderr.puts "Error: TppVisualizer#do_right has been called directly."
Kernel.exit(1)
end
def do_exec(cmdline)
$stderr.puts "Error: TppVisualizer#do_exec has been called directly."
Kernel.exit(1)
end
def do_wait
$stderr.puts "Error: TppVisualizer#do_wait has been called directly."
Kernel.exit(1)
end
def do_beginoutput
$stderr.puts "Error: TppVisualizer#do_beginoutput has been called directly."
Kernel.exit(1)
end
def do_beginshelloutput
$stderr.puts "Error: TppVisualizer#do_beginshelloutput has been called directly."
Kernel.exit(1)
end
def do_endoutput
$stderr.puts "Error: TppVisualizer#do_endoutput has been called directly."
Kernel.exit(1)
end
def do_endshelloutput
$stderr.puts "Error: TppVisualizer#do_endshelloutput has been called directly."
Kernel.exit(1)
end
def do_sleep(time2sleep)
$stderr.puts "Error: TppVisualizer#do_sleep has been called directly."
Kernel.exit(1)
end
def do_boldon
$stderr.puts "Error: TppVisualizer#do_boldon has been called directly."
Kernel.exit(1)
end
def do_boldoff
$stderr.puts "Error: TppVisualizer#do_boldoff has been called directly."
Kernel.exit(1)
end
def do_revon
$stderr.puts "Error: TppVisualizer#do_revon has been called directly."
Kernel.exit(1)
end
def do_revoff
$stderr.puts "Error: TppVisualizer#do_revoff has been called directly."
Kernel.exit(1)
end
def do_ulon
$stderr.puts "Error: TppVisualizer#do_ulon has been called directly."
Kernel.exit(1)
end
def do_uloff
$stderr.puts "Error: TppVisualizer#do_uloff has been called directly."
Kernel.exit(1)
end
def do_beginslideleft
$stderr.puts "Error: TppVisualizer#do_beginslideleft has been called directly."
Kernel.exit(1)
end
def do_endslide
$stderr.puts "Error: TppVisualizer#do_endslide has been called directly."
Kernel.exit(1)
end
def do_beginslideright
$stderr.puts "Error: TppVisualizer#do_beginslideright has been called directly."
Kernel.exit(1)
end
def do_beginslidetop
$stderr.puts "Error: TppVisualizer#do_beginslidetop has been called directly."
Kernel.exit(1)
end
def do_beginslidebottom
$stderr.puts "Error: TppVisualizer#do_beginslidebottom has been called directly."
Kernel.exit(1)
end
def do_sethugefont
$stderr.puts "Error: TppVisualizer#do_sethugefont has been called directly."
Kernel.exit(1)
end
def do_huge(text)
$stderr.puts "Error: TppVisualizer#do_huge has been called directly."
Kernel.exit(1)
end
def print_line(line)
$stderr.puts "Error: TppVisualizer#print_line has been called directly."
Kernel.exit(1)
end
def do_title(title)
$stderr.puts "Error: TppVisualizer#do_title has been called directly."
Kernel.exit(1)
end
def do_author(author)
$stderr.puts "Error: TppVisualizer#do_author has been called directly."
Kernel.exit(1)
end
def do_date(date)
$stderr.puts "Error: TppVisualizer#do_date has been called directly."
Kernel.exit(1)
end
def do_bgcolor(color)
$stderr.puts "Error: TppVisualizer#do_bgcolor has been called directly."
Kernel.exit(1)
end
def do_fgcolor(color)
$stderr.puts "Error: TppVisualizer#do_fgcolor has been called directly."
Kernel.exit(1)
end
def do_color(color)
$stderr.puts "Error: TppVisualizer#do_color has been called directly."
Kernel.exit(1)
end
# Receives a _line_, parses it if necessary, and dispatches it
# to the correct method which then does the correct processing.
# It returns whether the controller shall wait for input.
def visualize(line)
case line
when /^--heading /
text = line.sub(/^--heading /,"")
do_heading(text)
when /^--withborder/
do_withborder
when /^--horline/
do_horline
when /^--color /
text = line.sub(/^--color /,"")
text.strip!
do_color(text)
when /^--center /
text = line.sub(/^--center /,"")
do_center(text)
when /^--right /
text = line.sub(/^--right /,"")
do_right(text)
when /^--exec /
cmdline = line.sub(/^--exec /,"")
do_exec(cmdline)
when /^---/
do_wait
return true
when /^--beginoutput/
do_beginoutput
when /^--beginshelloutput/
do_beginshelloutput
when /^--endoutput/
do_endoutput
when /^--endshelloutput/
do_endshelloutput
when /^--sleep /
time2sleep = line.sub(/^--sleep /,"")
do_sleep(time2sleep)
when /^--boldon/
do_boldon
when /^--boldoff/
do_boldoff
when /^--revon/
do_revon
when /^--revoff/
do_revoff
when /^--ulon/
do_ulon
when /^--uloff/
do_uloff
when /^--beginslideleft/
do_beginslideleft
when /^--endslideleft/, /^--endslideright/, /^--endslidetop/, /^--endslidebottom/
do_endslide
when /^--beginslideright/
do_beginslideright
when /^--beginslidetop/
do_beginslidetop
when /^--beginslidebottom/
do_beginslidebottom
when /^--sethugefont /
params = line.sub(/^--sethugefont /,"")
do_sethugefont(params.strip)
when /^--huge /
figlet_text = line.sub(/^--huge /,"")
do_huge(figlet_text)
when /^--footer /
@footer_txt = line.sub(/^--footer /,"")
do_footer(@footer_txt)
when /^--header /
@header_txt = line.sub(/^--header /,"")
do_header(@header_txt)
when /^--title /
title = line.sub(/^--title /,"")
do_title(title)
when /^--author /
author = line.sub(/^--author /,"")
do_author(author)
when /^--date /
date = line.sub(/^--date /,"")
if date == "today" then
date = Time.now.strftime("%b %d %Y")
elsif date =~ /^today / then
date = Time.now.strftime(date.sub(/^today /,""))
end
do_date(date)
when /^--bgcolor /
color = line.sub(/^--bgcolor /,"").strip
do_bgcolor(color)
when /^--fgcolor /
color = line.sub(/^--fgcolor /,"").strip
do_fgcolor(color)
when /^--color /
color = line.sub(/^--color /,"").strip
do_color(color)
when /^--include-file /
@lastFileName = line.sub(/^--include-file /,"").strip
do_beginoutput
print_line(@lastFileName)
do_beginoutput
f = File.open(@lastFileName)
f.each_line do |fileLine|
fileLine.chomp!
if fileLine
print_line(fileLine)
end
end
do_endoutput
else
print_line(line)
end
return false
end
def close
# nothing
end
end
# Implements an interactive visualizer which builds on top of ncurses.
class NcursesVisualizer < TppVisualizer
def initialize
@figletfont = "standard"
Ncurses.initscr
Ncurses.curs_set(0)
Ncurses.cbreak # unbuffered input
Ncurses.noecho # turn off input echoing
Ncurses.stdscr.intrflush(false)
Ncurses.stdscr.keypad(true)
@screen = Ncurses.stdscr
@lastFileName = nil
setsizes
Ncurses.start_color()
Ncurses.use_default_colors()
do_bgcolor("black")
#do_fgcolor("white")
@fgcolor = ColorMap.get_color_pair("white")
@voffset = 5
@indent = 3
@cur_line = @voffset
@output = @shelloutput = false
end
def get_key
ch = Ncurses.getch
case ch
when 100, #d
68, #D
106, #j
74, #J
108, #l
76, #L
Ncurses::KEY_DOWN,
Ncurses::KEY_RIGHT
return :keyright
when 97, #a
65, #A
98, #b
66, #B
104, #h
72, #h
107, #k
75, #k
Ncurses::KEY_UP,
Ncurses::KEY_LEFT
return :keyleft
when 122, #z
90 #Z
return :keyresize
when 114, #r
82 #R
return :reload
when 113, #q
81 #Q
return :quit
when 115, #s
83 #S
return :firstpage
when 101, #e
69 #E
return :edit
when 103, #g
71 #g
return :jumptoslide
when 63 #?
return :help
else
return :keyright
end
end
def clear
@screen.clear
@screen.refresh
end
def setsizes
@termwidth = Ncurses.getmaxx(@screen)
@termheight = Ncurses.getmaxy(@screen)
end
def do_refresh
@screen.refresh
end
def do_withborder
@withborder = true
draw_border
end
def draw_border
@screen.move(0,0)
@screen.addstr(".")
(@termwidth-2).times { @screen.addstr("-") }; @screen.addstr(".")
@screen.move(@termheight-2,0)
@screen.addstr("`")
(@termwidth-2).times { @screen.addstr("-") }; @screen.addstr("'")
1.upto(@termheight-3) do |y|
@screen.move(y,0)
@screen.addstr("|")
end
1.upto(@termheight-3) do |y|
@screen.move(y,@termwidth-1)
@screen.addstr("|")
end
end
def new_page
@cur_line = @voffset
@output = @shelloutput = false
setsizes
@screen.clear
end
def do_heading(line)
@screen.attron(Ncurses::A_BOLD)
print_heading(line)
@screen.attroff(Ncurses::A_BOLD)
end
def do_horline
@screen.attron(Ncurses::A_BOLD)
@termwidth.times do |x|
@screen.move(@cur_line,x)
@screen.addstr("-")
end
@screen.attroff(Ncurses::A_BOLD)
end
def print_heading(text)
width = @termwidth - 2*@indent
lines = split_lines(text,width)
lines.each do |l|
@screen.move(@cur_line,@indent)
x = (@termwidth - l.length)/2
@screen.move(@cur_line,x)
@screen.addstr(l)
@cur_line += 1
end
end
def do_center(text)
width = @termwidth - 2*@indent
if @output or @shelloutput then
width -= 2
end
lines = split_lines(text,width)
lines.each do |l|
@screen.move(@cur_line,@indent)
if @output or @shelloutput then
@screen.addstr("| ")
end
x = (@termwidth - l.length)/2
@screen.move(@cur_line,x)
@screen.addstr(l)
if @output or @shelloutput then
@screen.move(@cur_line,@termwidth - @indent - 2)
@screen.addstr(" |")
end
@cur_line += 1
end
end
def do_right(text)
width = @termwidth - 2*@indent
if @output or @shelloutput then
width -= 2
end
lines = split_lines(text,width)
lines.each do |l|
@screen.move(@cur_line,@indent)
if @output or @shelloutput then
@screen.addstr("| ")
end
x = (@termwidth - l.length - 5)
@screen.move(@cur_line,x)
@screen.addstr(l)
if @output or @shelloutput then
@screen.addstr(" |")
end
@cur_line += 1
end
end
def show_help_page
help_text = [ "tpp help",
"",
"space bar ............................... display next entry within page",
"space bar, cursor-down, cursor-right .... display next page",
"b, cursor-up, cursor-left ............... display previous page",
"q, Q .................................... quit tpp",
"j, J .................................... jump directly to page",
"l, L .................................... reload current file",
"s, S .................................... jump to the first page",
"e, E .................................... jump to the last page",
"c, C .................................... start command line",
"?, h .................................... this help screen" ]
@screen.clear
y = @voffset
help_text.each do |line|
@screen.move(y,@indent)
@screen.addstr(line)
y += 1
end
@screen.move(@termheight - 2, @indent)
@screen.addstr("Press any key to return to slide")
@screen.refresh
end
def do_exec(cmdline)
rc = Kernel.system(cmdline)
if not rc then
# @todo: add error message
end
end
def do_wait
# nothing
end
def do_beginoutput
@screen.move(@cur_line,@indent)
@screen.addstr(".")
(@termwidth - @indent*2 - 2).times { @screen.addstr("-") }
@screen.addstr(".")
@output = true
@cur_line += 1
end
def do_beginshelloutput
@screen.move(@cur_line,@indent)
@screen.addstr(".")
(@termwidth - @indent*2 - 2).times { @screen.addstr("-") }
@screen.addstr(".")
@shelloutput = true
@cur_line += 1
end
def do_endoutput
if @output then
@screen.move(@cur_line,@indent)
@screen.addstr("`")
(@termwidth - @indent*2 - 2).times { @screen.addstr("-") }
@screen.addstr("'")
@output = false
@cur_line += 1
end
end
def do_title(title)
do_boldon
do_center(title)
do_boldoff
do_center("")
end
def do_footer(footer_txt)
@screen.move(@termheight - 3, (@termwidth - footer_txt.length)/2)
@screen.addstr(footer_txt)
end
def do_header(header_txt)
@screen.move(@termheight - @termheight+1, (@termwidth - header_txt.length)/2)
@screen.addstr(header_txt)
end
def do_author(author)
do_center(author)
do_center("")
end
def do_date(date)
do_center(date)
do_center("")
end
def do_endshelloutput
if @shelloutput then
@screen.move(@cur_line,@indent)
@screen.addstr("`")
(@termwidth - @indent*2 - 2).times { @screen.addstr("-") }
@screen.addstr("'")
@shelloutput = false
@cur_line += 1
end
end
def do_sleep(time2sleep)
Kernel.sleep(time2sleep.to_i)
end
def do_boldon
@screen.attron(Ncurses::A_BOLD)
end
def do_boldoff
@screen.attroff(Ncurses::A_BOLD)
end
def do_revon
@screen.attron(Ncurses::A_REVERSE)
end
def do_revoff
@screen.attroff(Ncurses::A_REVERSE)
end
def do_ulon
@screen.attron(Ncurses::A_UNDERLINE)
end
def do_uloff
@screen.attroff(Ncurses::A_UNDERLINE)
end
def do_beginslideleft
@slideoutput = true
@slidedir = "left"
end
def do_endslide
@slideoutput = false
end
def do_beginslideright
@slideoutput = true
@slidedir = "right"
end
def do_beginslidetop
@slideoutput = true
@slidedir = "top"
end
def do_beginslidebottom
@slideoutput = true
@slidedir = "bottom"
end
def do_sethugefont(params)
@figletfont = params
end
def do_huge(figlet_text)
output_width = @termwidth - @indent
output_width -= 2 if @output or @shelloutput
op = IO.popen("figlet -f #{@figletfont} -w #{output_width} -k \"#{figlet_text}\"","r")
op.readlines.each do |line|
print_line(line)
end
op.close
end
def do_bgcolor(color)
bgcolor = ColorMap.get_color(color) or COLOR_BLACK
Ncurses.init_pair(1, COLOR_WHITE, bgcolor)
Ncurses.init_pair(2, COLOR_YELLOW, bgcolor)
Ncurses.init_pair(3, COLOR_RED, bgcolor)
Ncurses.init_pair(4, COLOR_GREEN, bgcolor)
Ncurses.init_pair(5, COLOR_BLUE, bgcolor)
Ncurses.init_pair(6, COLOR_CYAN, bgcolor)
Ncurses.init_pair(7, COLOR_MAGENTA, bgcolor)
Ncurses.init_pair(8, COLOR_BLACK, bgcolor)
if @fgcolor then
Ncurses.bkgd(Ncurses.COLOR_PAIR(@fgcolor))
else
Ncurses.bkgd(Ncurses.COLOR_PAIR(1))
end
end
def do_fgcolor(color)
@fgcolor = ColorMap.get_color_pair(color)
Ncurses.attron(Ncurses.COLOR_PAIR(@fgcolor))
end
def do_color(color)
num = ColorMap.get_color_pair(color)
Ncurses.attron(Ncurses.COLOR_PAIR(num))
end
def type_line(l)
l.each_byte do |x|
@screen.addstr(x.chr)
@screen.refresh()
r = rand(20)
time_to_sleep = (5 + r).to_f / 250;
# puts "#{time_to_sleep} #{r}"
Kernel.sleep(time_to_sleep)
end
end
def slide_text(l)
return if l == ""
case @slidedir
when "left"
xcount = l.length-1
while xcount >= 0
@screen.move(@cur_line,@indent)
@screen.addstr(l[xcount..l.length-1])
@screen.refresh()
time_to_sleep = 1.to_f / 20
Kernel.sleep(time_to_sleep)
xcount -= 1
end
when "right"
(@termwidth - @indent).times do |pos|
@screen.move(@cur_line,@termwidth - pos - 1)
@screen.clrtoeol()
@screen.addstr(l[0..pos])
@screen.refresh()
time_to_sleep = 1.to_f / 20
Kernel.sleep(time_to_sleep)
end # do
when "top"
# ycount = @cur_line
new_scr = @screen.dupwin
1.upto(@cur_line) do |i|
Ncurses.overwrite(new_scr,@screen) # overwrite @screen with new_scr
@screen.move(i,@indent)
@screen.addstr(l)
@screen.refresh()
Kernel.sleep(1.to_f / 10)
end
when "bottom"
new_scr = @screen.dupwin
(@termheight-1).downto(@cur_line) do |i|
Ncurses.overwrite(new_scr,@screen)
@screen.move(i,@indent)
@screen.addstr(l)
@screen.refresh()
Kernel.sleep(1.to_f / 10)
end
end
end
def print_line(line)
width = @termwidth - 2*@indent
if @output or @shelloutput then
width -= 2
end
lines = split_lines(line,width)
lines.each do |l|
@screen.move(@cur_line,@indent)
if (@output or @shelloutput) and ! @slideoutput then
@screen.addstr("| ")
end
if @shelloutput and (l =~ /^\$/ or l=~ /^%/ or l =~ /^#/) then # allow sh and csh style prompts
type_line(l)
elsif @slideoutput then
slide_text(l)
else
@screen.addstr(l)
end
if (@output or @shelloutput) and ! @slideoutput then
@screen.move(@cur_line,@termwidth - @indent - 2)
@screen.addstr(" |")
end
@cur_line += 1
end
end
def close
Ncurses.nocbreak
Ncurses.endwin
end
def read_newpage(pages,current_page)
page = []
@screen.clear()
col = 0
line = 2
pages.each_index do |i|
@screen.move(line,col*15 + 2)
if current_page == i then
@screen.printw("%2d %s <=",i+1,pages[i].title[0..80])
else
@screen.printw("%2d %s",i+1,pages[i].title[0..80])
end
line += 1
if line >= @termheight - 3 then
line = 2
col += 1
end
end
prompt = "jump to slide: "