-
Notifications
You must be signed in to change notification settings - Fork 7
/
profanity.rb
2278 lines (2162 loc) · 95.6 KB
/
profanity.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
=begin
ProfanityFE
Copyright (C) 2013 Matthew Lowe
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=end
require 'json'
require 'benchmark'
require 'socket'
require 'rexml/document'
require 'curses'
require 'fileutils'
require 'ostruct'
include Curses
require_relative "./ext/string.rb"
require_relative "./util/opts.rb"
require_relative "./ui/countdown.rb"
require_relative "./ui/indicator.rb"
require_relative "./ui/progress.rb"
require_relative "./ui/text.rb"
require_relative "./ui/exp.rb"
require_relative "./ui/perc.rb"
require_relative "./plugin/autocomplete.rb"
require_relative "./settings/settings.rb"
require_relative "./hilite/hilite.rb"
module Profanity
LOG_FILE = Settings.file("debug.log")
def self.log_file
return File.open(LOG_FILE, 'a') { |file| yield file } if block_given?
end
@title = nil
@status = nil
@char = Opts.char.capitalize
@state = {}
def self.fetch(key, default = nil)
@state.fetch(key, default)
end
def self.put(**args)
@state.merge!(args)
end
def self.set_terminal_title(title)
return if @title.eql?(title) # noop
@title = title
system("printf \"\033]0;#{title}\007\"")
Process.setproctitle(title)
end
def self.app_title(*parts)
return if @status == parts.join("")
@status = parts.join("")
return set_terminal_title(@char) if @status.empty?
set_terminal_title([@char, "[#{parts.reject(&:empty?).join(":")}]"].join(" ").gsub(">", ""))
end
def self.update_process_title()
return if Opts["no-status"]
app_title(Profanity.fetch(:prompt, ""), Profanity.fetch(:room, ""))
end
def self.log(str)
log_file { |f| f.puts str }
end
def self.help_menu()
puts <<~HELP
Profanity FrontEnd
#{' '}
--port=<port> the port to connect to Lich on
--default-color-id=<id> optional override, a terminal palette color number
--default-background-color-id=<id> optional override, a terminal palette color number
--char=<character> character name used in Lich
--no-status do not redraw the process title with status updates
--links enable links to be shown by default, otherwise can enable via .links command
--speech-ts display timestamps on speech, familiar and thought window
--remote-url display LaunchURLs on screen, used for remote environments
--template=<filename.xml> filename of template to use in templates subdirectory
HELP
exit
end
end
Curses.init_screen
Curses.start_color
Curses.cbreak
Curses.noecho
server = nil
command_buffer = String.new
command_buffer_pos = 0
command_buffer_offset = 0
command_history = Array.new
command_history_pos = 0
min_cmd_length_for_history = 4
$server_time_offset = 0
skip_server_time_offset = false
key_binding = Hash.new
key_action = Hash.new
need_prompt = false
prompt_text = ">"
stream_handler = Hash.new
indicator_handler = Hash.new
progress_handler = Hash.new
countdown_handler = Hash.new
command_window = nil
command_window_layout = nil
blue_links = (Opts["links"] ? true : false)
# We need a mutex for the settings because highlights can be accessed during a
# reload. For now, it is just used to protect access to HIGHLIGHT, but if we
# ever support reloading other settings in the future it will have to protect
# those.
SETTINGS_LOCK = Mutex.new
# TODO: fix this dirty, dirty, scumbag hack
HIGHLIGHT = Hilite.pointer()
PRESET = Hash.new
LAYOUT = Hash.new
WINDOWS = Hash.new
SCROLL_WINDOW = Array.new
PORT = (Opts.port || 8000).to_i
HOST = (Opts.host || "127.0.0.1")
DEFAULT_COLOR_ID = (Opts["default-color-id"] || 7).to_i
DEFAULT_BACKGROUND_COLOR_ID = (Opts["default-background-color-id"] || 0).to_i
if Opts.char
if Opts.template
if File.exist?(File.join(File.expand_path(File.dirname(__FILE__)), 'templates', Opts.template.downcase))
SETTINGS_FILENAME = File.join(File.expand_path(File.dirname(__FILE__)), 'templates', Opts.template.downcase)
else
raise StandardError, <<~ERROR
You specified --template=#{Opts.template} but it doesn't exist.
Please try again!
ERROR
end
else
if File.exist?(Settings.file(Opts.char.downcase + ".xml"))
SETTINGS_FILENAME = Settings.file(Opts.char.downcase + ".xml")
elsif File.exist?(File.join(File.expand_path(File.dirname(__FILE__)), 'templates', Opts.char.downcase + ".xml"))
SETTINGS_FILENAME = File.join(File.expand_path(File.dirname(__FILE__)), 'templates', Opts.char.downcase + ".xml")
else
SETTINGS_FILENAME = File.join(File.expand_path(File.dirname(__FILE__)), 'templates', 'default.xml')
end
end
end
def add_prompt(window, prompt_text, cmd = "")
window.add_string("#{prompt_text}#{cmd}", [{ :start => 0, :end => (prompt_text.length + cmd.length), :fg => '555555' }])
end
unless defined?(SETTINGS_FILENAME)
raise StandardError, <<~ERROR
you must pass --char=<character> or --template=<filename.xml>
#{Opts.parse()}
ERROR
end
Profanity.set_terminal_title(Opts.char.capitalize)
unless defined?(CUSTOM_COLORS)
CUSTOM_COLORS = Curses.can_change_color?
end
DEFAULT_COLOR_CODE = Curses.color_content(DEFAULT_COLOR_ID).collect { |num| ((num / 1000.0) * 255).round.to_s(16) }.join('').rjust(6, '0')
DEFAULT_BACKGROUND_COLOR_CODE = Curses.color_content(DEFAULT_BACKGROUND_COLOR_ID).collect { |num| ((num / 1000.0) * 255).round.to_s(16) }.join('').rjust(6, '0')
xml_escape_list = {
'<' => '<',
'>' => '>',
'"' => '"',
''' => "'",
'&' => '&',
# '
' => "\n",
}
key_name = {
'ctrl+a' => 1,
'ctrl+b' => 2,
# 'ctrl+c' => 3,
'ctrl+d' => 4,
'ctrl+e' => 5,
'ctrl+f' => 6,
'ctrl+g' => 7,
'ctrl+h' => 8,
'win_backspace' => 8,
'ctrl+i' => 9,
'tab' => 9,
'ctrl+j' => 10,
'enter' => 10,
'ctrl+k' => 11,
'ctrl+l' => 12,
'return' => 13,
'ctrl+m' => 13,
'ctrl+n' => 14,
'ctrl+o' => 15,
'ctrl+p' => 16,
# 'ctrl+q' => 17,
'ctrl+r' => 18,
# 'ctrl+s' => 19,
'ctrl+t' => 20,
'ctrl+u' => 21,
'ctrl+v' => 22,
'ctrl+w' => 23,
'ctrl+x' => 24,
'ctrl+y' => 25,
'ctrl+z' => 26,
'alt' => 27,
'escape' => 27,
'ctrl+?' => 127,
'down' => 258,
'up' => 259,
'left' => 260,
'right' => 261,
'home' => 262,
'backspace' => 263,
'f1' => 265,
'f2' => 266,
'f3' => 267,
'f4' => 268,
'f5' => 269,
'f6' => 270,
'f7' => 271,
'f8' => 272,
'f9' => 273,
'f10' => 274,
'f11' => 275,
'f12' => 276,
'delete' => 330,
'insert' => 331,
'page_down' => 338,
'page_up' => 339,
'end' => 360,
'resize' => 410,
'num_7' => 449,
'num_8' => 450,
'num_9' => 451,
'num_4' => 452,
'num_5' => 453,
'num_6' => 454,
'num_1' => 455,
'num_2' => 456,
'num_3' => 457,
'num_enter' => 459,
'ctrl+delete' => 513,
'alt+down' => 517,
'ctrl+down' => 519,
'alt+left' => 537,
'ctrl+left' => 539,
'alt+page_down' => 542,
'alt+page_up' => 547,
'alt+right' => 552,
'ctrl+right' => 554,
'alt+up' => 558,
'ctrl+up' => 560,
}
if CUSTOM_COLORS
COLOR_ID_LOOKUP = Hash.new
# e.g. code 000000 = id 0
COLOR_ID_LOOKUP[DEFAULT_COLOR_CODE] = DEFAULT_COLOR_ID
COLOR_ID_LOOKUP[DEFAULT_BACKGROUND_COLOR_CODE] = DEFAULT_BACKGROUND_COLOR_ID
COLOR_ID_HISTORY = Array.new
for num in 0...Curses.colors
unless (num == DEFAULT_COLOR_ID) or (num == DEFAULT_BACKGROUND_COLOR_ID)
COLOR_ID_HISTORY.push(num)
end
end
def get_color_id(code)
if (color_id = COLOR_ID_LOOKUP[code])
color_id
else
color_id = COLOR_ID_HISTORY.shift
COLOR_ID_LOOKUP.delete_if { |_k, v| v == color_id }
sleep 0.01 # somehow this keeps Curses.init_color from failing sometimes
Curses.init_color(color_id, ((code[0..1].to_s.hex / 255.0) * 1000).round, ((code[2..3].to_s.hex / 255.0) * 1000).round, ((code[4..5].to_s.hex / 255.0) * 1000).round)
COLOR_ID_LOOKUP[code] = color_id
COLOR_ID_HISTORY.push(color_id)
color_id
end
end
else
COLOR_CODE = ['000000', '800000', '008000', '808000', '000080', '800080', '008080', 'c0c0c0', '808080', 'ff0000', '00ff00', 'ffff00', '0000ff', 'ff00ff', '00ffff', 'ffffff', '000000', '00005f', '000087', '0000af', '0000d7', '0000ff', '005f00', '005f5f', '005f87', '005faf', '005fd7', '005fff', '008700', '00875f', '008787', '0087af', '0087d7', '0087ff', '00af00', '00af5f', '00af87', '00afaf', '00afd7', '00afff', '00d700', '00d75f', '00d787', '00d7af', '00d7d7', '00d7ff', '00ff00', '00ff5f', '00ff87', '00ffaf', '00ffd7', '00ffff', '5f0000', '5f005f', '5f0087', '5f00af', '5f00d7', '5f00ff', '5f5f00', '5f5f5f', '5f5f87', '5f5faf', '5f5fd7', '5f5fff', '5f8700', '5f875f', '5f8787', '5f87af', '5f87d7', '5f87ff', '5faf00', '5faf5f', '5faf87', '5fafaf', '5fafd7', '5fafff', '5fd700', '5fd75f', '5fd787', '5fd7af', '5fd7d7', '5fd7ff', '5fff00', '5fff5f', '5fff87', '5fffaf', '5fffd7', '5fffff', '870000', '87005f', '870087', '8700af', '8700d7', '8700ff', '875f00', '875f5f', '875f87', '875faf', '875fd7', '875fff', '878700', '87875f', '878787', '8787af', '8787d7', '8787ff', '87af00', '87af5f', '87af87', '87afaf', '87afd7', '87afff', '87d700', '87d75f', '87d787', '87d7af', '87d7d7', '87d7ff', '87ff00', '87ff5f', '87ff87', '87ffaf', '87ffd7', '87ffff', 'af0000', 'af005f', 'af0087', 'af00af', 'af00d7', 'af00ff', 'af5f00', 'af5f5f', 'af5f87', 'af5faf', 'af5fd7', 'af5fff', 'af8700', 'af875f', 'af8787', 'af87af', 'af87d7', 'af87ff', 'afaf00', 'afaf5f', 'afaf87', 'afafaf', 'afafd7', 'afafff', 'afd700', 'afd75f', 'afd787', 'afd7af', 'afd7d7', 'afd7ff', 'afff00', 'afff5f', 'afff87', 'afffaf', 'afffd7', 'afffff', 'd70000', 'd7005f', 'd70087', 'd700af', 'd700d7', 'd700ff', 'd75f00', 'd75f5f', 'd75f87', 'd75faf', 'd75fd7', 'd75fff', 'd78700', 'd7875f', 'd78787', 'd787af', 'd787d7', 'd787ff', 'd7af00', 'd7af5f', 'd7af87', 'd7afaf', 'd7afd7', 'd7afff', 'd7d700', 'd7d75f', 'd7d787', 'd7d7af', 'd7d7d7', 'd7d7ff', 'd7ff00', 'd7ff5f', 'd7ff87', 'd7ffaf', 'd7ffd7', 'd7ffff', 'ff0000', 'ff005f', 'ff0087', 'ff00af', 'ff00d7', 'ff00ff', 'ff5f00', 'ff5f5f', 'ff5f87', 'ff5faf', 'ff5fd7', 'ff5fff', 'ff8700', 'ff875f', 'ff8787', 'ff87af', 'ff87d7', 'ff87ff', 'ffaf00', 'ffaf5f', 'ffaf87', 'ffafaf', 'ffafd7', 'ffafff', 'ffd700', 'ffd75f', 'ffd787', 'ffd7af', 'ffd7d7', 'ffd7ff', 'ffff00', 'ffff5f', 'ffff87', 'ffffaf', 'ffffd7', 'ffffff', '080808', '121212', '1c1c1c', '262626', '303030', '3a3a3a', '444444', '4e4e4e', '585858', '626262', '6c6c6c', '767676', '808080', '8a8a8a', '949494', '9e9e9e', 'a8a8a8', 'b2b2b2', 'bcbcbc', 'c6c6c6', 'd0d0d0', 'dadada', 'e4e4e4', 'eeeeee'][0...Curses.colors]
COLOR_ID_LOOKUP = Hash.new
def get_color_id(code)
if (color_id = COLOR_ID_LOOKUP[code])
color_id
else
least_error = nil
least_error_id = nil
COLOR_CODE.each_index { |color_id|
error = ((COLOR_CODE[color_id][0..1].hex - code[0..1].hex)**2) + ((COLOR_CODE[color_id][2..3].hex - code[2..3].hex)**2) + ((COLOR_CODE[color_id][4..6].hex - code[4..6].hex)**2)
if least_error.nil? or (error < least_error)
least_error = error
least_error_id = color_id
end
}
COLOR_ID_LOOKUP[code] = least_error_id
least_error_id
end
end
end
# COLOR_PAIR_LIST = Array.new
# for num in 1...Curses::color_pairs
# COLOR_PAIR_LIST.push h={ :color_id => nil, :background_id => nil, :id => num }
# end
# 157+12+1 = 180
# 38+1+6 = 45
# 32767
COLOR_PAIR_ID_LOOKUP = Hash.new
COLOR_PAIR_HISTORY = Array.new
# fixme: high color pair id's change text?
# A_NORMAL = 0
# A_STANDOUT = 65536
# A_UNDERLINE = 131072
# 15000 = black background, dark blue-green text
# 10000 = dark yellow background, black text
# 5000 = black
# 2000 = black
# 1000 = highlights show up black
# 100 = normal
# 500 = black and some underline
for num in 1...Curses::color_pairs # fixme: things go to hell at about pair 256
# for num in 1...([Curses::color_pairs, 256].min)
COLOR_PAIR_HISTORY.push(num)
end
def get_color_pair_id(fg_code, bg_code)
if fg_code.nil?
fg_id = DEFAULT_COLOR_ID
else
fg_id = get_color_id(fg_code)
end
if bg_code.nil?
bg_id = DEFAULT_BACKGROUND_COLOR_ID
else
bg_id = get_color_id(bg_code)
end
if (COLOR_PAIR_ID_LOOKUP[fg_id]) and (color_pair_id = COLOR_PAIR_ID_LOOKUP[fg_id][bg_id])
color_pair_id
else
color_pair_id = COLOR_PAIR_HISTORY.shift
COLOR_PAIR_ID_LOOKUP.each { |_w, x| x.delete_if { |_y, z| z == color_pair_id } }
sleep 0.01
Curses.init_pair(color_pair_id, fg_id, bg_id)
COLOR_PAIR_ID_LOOKUP[fg_id] ||= Hash.new
COLOR_PAIR_ID_LOOKUP[fg_id][bg_id] = color_pair_id
COLOR_PAIR_HISTORY.push(color_pair_id)
color_pair_id
end
end
# Previously we weren't setting bkgd so it's no wonder it didn't seem to work
# Had to put this down here under the get_color_pair_id definition
Curses.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
Curses.refresh
# Implement support for basic readline-style kill and yank (cut and paste)
# commands. Successive calls to delete_word, backspace_word, kill_forward, and
# kill_line will accumulate text into the kill_buffer as long as no other
# commands have changed the command buffer. These commands call kill_before to
# reset the kill_buffer if the command buffer has changed, add the newly
# deleted text to the kill_buffer, and finally call kill_after to remember the
# state of the command buffer for next time.
kill_buffer = ''
kill_original = ''
kill_last = ''
kill_last_pos = 0
kill_before = proc {
if kill_last != command_buffer || kill_last_pos != command_buffer_pos
kill_buffer = ''
kill_original = command_buffer
end
}
kill_after = proc {
kill_last = command_buffer.dup
kill_last_pos = command_buffer_pos
}
fix_layout_number = proc { |str|
str = str.gsub('lines', Curses.lines.to_s).gsub('cols', Curses.cols.to_s)
begin
proc { eval(str) }.call.to_i
rescue
$stderr.puts $!
$stderr.puts $!.backtrace[0..1]
0
end
}
load_layout = proc { |layout_id|
if (xml = LAYOUT[layout_id])
old_windows = IndicatorWindow.list | TextWindow.list | CountdownWindow.list | ProgressWindow.list
previous_indicator_handler = indicator_handler
indicator_handler = Hash.new
previous_stream_handler = stream_handler
stream_handler = Hash.new
previous_progress_handler = progress_handler
progress_handler = Hash.new
previous_countdown_handler = countdown_handler
progress_handler = Hash.new
xml.elements.each { |e|
if e.name == 'window'
height, width, top, left = fix_layout_number.call(
e.attributes['height']
),
fix_layout_number.call(e.attributes['width']),
fix_layout_number.call(e.attributes['top']),
fix_layout_number.call(e.attributes['left'])
if (height > 0) and (width > 0) and (top >= 0) and (left >= 0) and (top < Curses.lines) and (left < Curses.cols)
if e.attributes['class'] == 'indicator'
if e.attributes['value'] and (window = previous_indicator_handler[e.attributes['value']])
previous_indicator_handler[e.attributes['value']] = nil
old_windows.delete(window)
else
window = IndicatorWindow.new(height, width, top, left)
window.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
end
window.layout = [e.attributes['height'], e.attributes['width'], e.attributes['top'], e.attributes['left']]
window.scrollok(false)
window.label = e.attributes['label'] if e.attributes['label']
window.fg = e.attributes['fg'].split(',')
.collect { |val| if val == 'nil'; nil; else; val; end } if e.attributes['fg']
window.bg = e.attributes['bg'].split(',')
.collect { |val| if val == 'nil'; nil; else; val; end } if e.attributes['bg']
if e.attributes['value']
indicator_handler[e.attributes['value']] = window
end
window.redraw
elsif e.attributes['class'] == 'text'
if width > 1
if e.attributes['value'] and (window = previous_stream_handler[previous_stream_handler.keys.find { |key| e.attributes['value'].split(',').include?(key) }])
previous_stream_handler[e.attributes['value']] = nil
old_windows.delete(window)
else
window = TextWindow.new(height, width - 1, top, left)
window.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
window.scrollbar = Curses::Window.new(window.maxy, 1, window.begy, window.begx + window.maxx)
window.scrollbar.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
end
window.layout = [e.attributes['height'], e.attributes['width'], e.attributes['top'], e.attributes['left']]
window.scrollok(true)
window.max_buffer_size = e.attributes['buffer-size'] || 1000
window.time_stamp = e.attributes['timestamp']
e.attributes['value'].split(',').each { |str|
stream_handler[str] = window
}
end
elsif e.attributes['class'] == 'exp'
stream_handler['exp'] = ExpWindow.new(height, width - 1, top, left)
stream_handler['exp'].bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
elsif e.attributes['class'] == 'percWindow'
stream_handler['percWindow'] = PercWindow.new(height, width - 1, top, left)
stream_handler['percWindow'].bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
elsif e.attributes['class'] == 'countdown'
if e.attributes['value'] and (window = previous_countdown_handler[e.attributes['value']])
previous_countdown_handler[e.attributes['value']] = nil
old_windows.delete(window)
else
window = CountdownWindow.new(height, width, top, left)
window.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
end
window.layout = [e.attributes['height'], e.attributes['width'], e.attributes['top'], e.attributes['left']]
window.scrollok(false)
window.label = e.attributes['label'] if e.attributes['label']
window.fg = e.attributes['fg'].split(',').collect { |val| if val == 'nil'; nil; else; val; end } if e.attributes['fg']
window.bg = e.attributes['bg'].split(',').collect { |val| if val == 'nil'; nil; else; val; end } if e.attributes['bg']
if e.attributes['value']
countdown_handler[e.attributes['value']] = window
end
window.update
elsif e.attributes['class'] == 'progress'
if e.attributes['value'] and (window = previous_progress_handler[e.attributes['value']])
previous_progress_handler[e.attributes['value']] = nil
old_windows.delete(window)
else
window = ProgressWindow.new(height, width, top, left)
window.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
end
window.layout = [e.attributes['height'], e.attributes['width'], e.attributes['top'], e.attributes['left']]
window.scrollok(false)
window.label = e.attributes['label'] if e.attributes['label']
window.fg = e.attributes['fg'].split(',').collect { |val| if val == 'nil'; nil; else; val; end } if e.attributes['fg']
window.bg = e.attributes['bg'].split(',').collect { |val| if val == 'nil'; nil; else; val; end } if e.attributes['bg']
if e.attributes['value']
progress_handler[e.attributes['value']] = window
end
window.redraw
elsif e.attributes['class'] == 'command'
unless command_window
command_window = Curses::Window.new(height, width, top, left)
command_window.bkgd(Curses.color_pair(get_color_pair_id(nil, nil)))
end
command_window_layout = [e.attributes['height'], e.attributes['width'], e.attributes['top'], e.attributes['left']]
command_window.scrollok(false)
command_window.keypad(true)
end
end
end
}
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.update_scrollbar
end
for window in old_windows
IndicatorWindow.list.delete(window)
TextWindow.list.delete(window)
CountdownWindow.list.delete(window)
ProgressWindow.list.delete(window)
if window.class == TextWindow
window.scrollbar.close
end
window.close
end
Curses.doupdate
end
}
do_macro = nil
setup_key = proc { |xml, binding|
if (key = xml.attributes['id'])
if key =~ /^[0-9]+$/
key = key.to_i
elsif (key.class) == String and (key.length == 1)
nil
else
key = key_name[key]
end
if key
if (macro = xml.attributes['macro'])
binding[key] = proc { do_macro.call(macro) }
elsif xml.attributes['action'] and (action = key_action[xml.attributes['action']])
binding[key] = action
else
binding[key] ||= Hash.new
xml.elements.each { |e|
setup_key.call(e, binding[key])
}
end
end
end
}
load_settings_file = proc { |reload|
SETTINGS_LOCK.synchronize {
begin
xml = Hilite.load(file: SETTINGS_FILENAME, flush: reload)
unless reload
xml.elements.each { |e|
# These are things that we ignore if we're doing a reload of the settings file
if e.name == 'preset'
PRESET[e.attributes['id']] = [e.attributes['fg'], e.attributes['bg']]
elsif (e.name == 'layout') and (layout_id = e.attributes['id'])
LAYOUT[layout_id] = e
elsif e.name == 'key'
setup_key.call(e, key_binding)
end
}
end
rescue
Profanity.log $!
Profanity.log $!.backtrace[0..1]
end
}
}
command_window_put_ch = proc { |ch|
if (command_buffer_pos - command_buffer_offset + 1) >= command_window.maxx
command_window.setpos(0, 0)
command_window.delch
command_buffer_offset += 1
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
end
command_buffer.insert(command_buffer_pos, ch)
command_buffer_pos += 1
command_window.insch(ch)
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
}
do_macro = proc { |macro|
# fixme: gsub %whatever
backslash = false
at_pos = nil
backfill = nil
macro.split('').each_with_index { |ch, i|
if backslash
if ch == '\\'
command_window_put_ch.call('\\')
elsif ch == 'x'
command_buffer.clear
command_buffer_pos = 0
command_buffer_offset = 0
command_window.deleteln
command_window.setpos(0, 0)
elsif ch == 'r'
at_pos = nil
key_action['send_command'].call
elsif ch == '@'
command_window_put_ch.call('@')
elsif ch == '?'
backfill = i - 3
else
nil
end
backslash = false
else
if ch == '\\'
backslash = true
elsif ch == '@'
at_pos = command_buffer_pos
else
command_window_put_ch.call(ch)
end
end
}
if at_pos
while at_pos < command_buffer_pos
key_action['cursor_left'].call
end
while at_pos > command_buffer_pos
key_action['cursor_right'].call
end
end
command_window.noutrefresh
if backfill then
command_window.setpos(0, backfill)
command_buffer_pos = backfill
backfill = nil
end
Curses.doupdate
}
key_action['resize'] = proc {
# fixme: re-word-wrap
Curses.clear
Curses.refresh
first_text_window = true
for window in TextWindow.list.to_a
window.resize(fix_layout_number.call(window.layout[0]), fix_layout_number.call(window.layout[1]) - 1)
window.move(fix_layout_number.call(window.layout[2]), fix_layout_number.call(window.layout[3]))
window.scrollbar.resize(window.maxy, 1)
window.scrollbar.move(window.begy, window.begx + window.maxx)
window.scroll(-window.maxy)
window.scroll(window.maxy)
window.clear_scrollbar
if first_text_window
window.update_scrollbar
first_text_window = false
end
window.noutrefresh
end
for window in [IndicatorWindow.list.to_a, ProgressWindow.list.to_a, CountdownWindow.list.to_a].flatten
window.resize(fix_layout_number.call(window.layout[0]), fix_layout_number.call(window.layout[1]))
window.move(fix_layout_number.call(window.layout[2]), fix_layout_number.call(window.layout[3]))
window.noutrefresh
end
if command_window
command_window.resize(fix_layout_number.call(command_window_layout[0]), fix_layout_number.call(command_window_layout[1]))
command_window.move(fix_layout_number.call(command_window_layout[2]), fix_layout_number.call(command_window_layout[3]))
command_window.noutrefresh
end
Curses.doupdate
}
key_action['cursor_left'] = proc {
if (command_buffer_offset > 0) and (command_buffer_pos - command_buffer_offset == 0)
command_buffer_pos -= 1
command_buffer_offset -= 1
command_window.insch(command_buffer[command_buffer_pos])
else
command_buffer_pos = [command_buffer_pos - 1, 0].max
end
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
command_window.noutrefresh
Curses.doupdate
}
key_action['cursor_right'] = proc {
if ((command_buffer.length - command_buffer_offset) >= (command_window.maxx - 1)) and (command_buffer_pos - command_buffer_offset + 1) >= command_window.maxx
if command_buffer_pos < command_buffer.length
command_window.setpos(0, 0)
command_window.delch
command_buffer_offset += 1
command_buffer_pos += 1
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
unless command_buffer_pos >= command_buffer.length
command_window.insch(command_buffer[command_buffer_pos])
end
end
else
command_buffer_pos = [command_buffer_pos + 1, command_buffer.length].min
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
end
command_window.noutrefresh
Curses.doupdate
}
key_action['cursor_word_left'] = proc {
if command_buffer_pos > 0
if (m = command_buffer[0...(command_buffer_pos - 1)].match(/.*(\w[^\w\s]|\W\w|\s\S)/))
new_pos = m.begin(1) + 1
else
new_pos = 0
end
if (command_buffer_offset > new_pos)
command_window.setpos(0, 0)
command_buffer[new_pos, (command_buffer_offset - new_pos)].split('').reverse.each { |ch| command_window.insch(ch) }
command_buffer_pos = new_pos
command_buffer_offset = new_pos
else
command_buffer_pos = new_pos
end
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
command_window.noutrefresh
Curses.doupdate
end
}
key_action['cursor_word_right'] = proc {
if command_buffer_pos < command_buffer.length
if (m = command_buffer[command_buffer_pos..-1].match(/\w[^\w\s]|\W\w|\s\S/))
new_pos = command_buffer_pos + m.begin(0) + 1
else
new_pos = command_buffer.length
end
overflow = new_pos - command_window.maxx - command_buffer_offset + 1
if overflow > 0
command_window.setpos(0, 0)
overflow.times {
command_window.delch
command_buffer_offset += 1
}
command_window.setpos(0, command_window.maxx - overflow)
command_window.addstr command_buffer[(command_window.maxx - overflow + command_buffer_offset), overflow]
end
command_buffer_pos = new_pos
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
command_window.noutrefresh
Curses.doupdate
end
}
key_action['cursor_home'] = proc {
command_buffer_pos = 0
command_window.setpos(0, 0)
for num in 1..command_buffer_offset
begin
command_window.insch(command_buffer[command_buffer_offset - num])
rescue
Profanity.log_file { |f|
f.puts "command_buffer: #{command_buffer.inspect}";
f.puts "command_buffer_offset: #{command_buffer_offset.inspect}";
f.puts "num: #{num.inspect}";
f.puts $!;
f.puts $!.backtrace[0...4]
}
exit
end
end
command_buffer_offset = 0
command_window.noutrefresh
Curses.doupdate
}
key_action['cursor_end'] = proc {
if command_buffer.length < (command_window.maxx - 1)
command_buffer_pos = command_buffer.length
command_window.setpos(0, command_buffer_pos)
else
scroll_left_num = command_buffer.length - command_window.maxx + 1 - command_buffer_offset
command_window.setpos(0, 0)
scroll_left_num.times {
command_window.delch
command_buffer_offset += 1
}
command_buffer_pos = command_buffer_offset + command_window.maxx - 1 - scroll_left_num
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
scroll_left_num.times {
command_window.addch(command_buffer[command_buffer_pos])
command_buffer_pos += 1
}
end
command_window.noutrefresh
Curses.doupdate
}
key_action['cursor_backspace'] = proc {
if command_buffer_pos > 0
command_buffer_pos -= 1
if command_buffer_pos == 0
command_buffer = command_buffer[(command_buffer_pos + 1)..-1]
else
command_buffer = command_buffer[0..(command_buffer_pos - 1)] + command_buffer[(command_buffer_pos + 1)..-1]
end
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
command_window.delch
if (command_buffer.length - command_buffer_offset + 1) > command_window.maxx
command_window.setpos(0, command_window.maxx - 1)
command_window.addch command_buffer[command_window.maxx - command_buffer_offset - 1]
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
end
command_window.noutrefresh
Curses.doupdate
end
}
key_action['cursor_delete'] = proc {
if (command_buffer.length > 0) and (command_buffer_pos < command_buffer.length)
if command_buffer_pos == 0
command_buffer = command_buffer[(command_buffer_pos + 1)..-1]
elsif command_buffer_pos < command_buffer.length
command_buffer = command_buffer[0..(command_buffer_pos - 1)] + command_buffer[(command_buffer_pos + 1)..-1]
end
command_window.delch
if (command_buffer.length - command_buffer_offset + 1) > command_window.maxx
command_window.setpos(0, command_window.maxx - 1)
command_window.addch command_buffer[command_window.maxx - command_buffer_offset - 1]
command_window.setpos(0, command_buffer_pos - command_buffer_offset)
end
command_window.noutrefresh
Curses.doupdate
end
}
key_action['cursor_backspace_word'] = proc {
num_deleted = 0
deleted_alnum = false
deleted_nonspace = false
while command_buffer_pos > 0 do
next_char = command_buffer[command_buffer_pos - 1]
if num_deleted == 0 || (!deleted_alnum && next_char.punct?) || (!deleted_nonspace && next_char.space?) || next_char.alnum?
deleted_alnum = deleted_alnum || next_char.alnum?
deleted_nonspace = !next_char.space?
num_deleted += 1
kill_before.call
kill_buffer = next_char + kill_buffer
key_action['cursor_backspace'].call
kill_after.call
else
break
end
end
}
key_action['cursor_delete_word'] = proc {
num_deleted = 0
deleted_alnum = false
deleted_nonspace = false
while command_buffer_pos < command_buffer.length do
next_char = command_buffer[command_buffer_pos]
if num_deleted == 0 || (!deleted_alnum && next_char.punct?) || (!deleted_nonspace && next_char.space?) || next_char.alnum?
deleted_alnum = deleted_alnum || next_char.alnum?
deleted_nonspace = !next_char.space?
num_deleted += 1
kill_before.call
kill_buffer = kill_buffer + next_char
key_action['cursor_delete'].call
kill_after.call
else
break
end
end
}
key_action['cursor_kill_forward'] = proc {
if command_buffer_pos < command_buffer.length
kill_before.call
if command_buffer_pos == 0
kill_buffer = kill_buffer + command_buffer
command_buffer = ''
else
kill_buffer = kill_buffer + command_buffer[command_buffer_pos..-1]
command_buffer = command_buffer[0..(command_buffer_pos - 1)]
end
kill_after.call
command_window.clrtoeol
command_window.noutrefresh
Curses.doupdate
end
}
key_action['cursor_kill_line'] = proc {
if command_buffer.length != 0
kill_before.call
kill_buffer = kill_original
command_buffer = ''
command_buffer_pos = 0
command_buffer_offset = 0
kill_after.call
command_window.setpos(0, 0)
command_window.clrtoeol
command_window.noutrefresh
Curses.doupdate
end
}
key_action['cursor_yank'] = proc {
kill_buffer.each_char { |c| command_window_put_ch.call(c) }
}
key_action['switch_current_window'] = proc {
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.clear_scrollbar
end
TextWindow.list.push(TextWindow.list.shift)
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.update_scrollbar
end
command_window.noutrefresh
Curses.doupdate
}
key_action['scroll_current_window_up_one'] = proc {
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.scroll(-1)
end
command_window.noutrefresh
Curses.doupdate
}
key_action['scroll_current_window_down_one'] = proc {
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.scroll(1)
end
command_window.noutrefresh
Curses.doupdate
}
key_action['scroll_current_window_up_page'] = proc {
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.scroll(0 - current_scroll_window.maxy + 1)
end
command_window.noutrefresh
Curses.doupdate
}
key_action['scroll_current_window_down_page'] = proc {
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.scroll(current_scroll_window.maxy - 1)
end
command_window.noutrefresh
Curses.doupdate
}
key_action['scroll_current_window_bottom'] = proc {
if (current_scroll_window = TextWindow.list[0])
current_scroll_window.scroll(current_scroll_window.max_buffer_size)
end
command_window.noutrefresh
Curses.doupdate
}
write_to_client = proc { |str, color|
stream_handler["main"].add_string str, [{ :fg => color, :start => 0, :end => str.size }]
command_window.noutrefresh
Curses.doupdate
}
key_action['autocomplete'] = proc { |idx|
Autocomplete.wrap do
current = command_buffer.dup
history = command_history.map(&:strip).reject(&:empty?).compact.uniq
# collection of possibilities