-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSSH.Console.fs
1871 lines (1730 loc) · 103 KB
/
SSH.Console.fs
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
namespace SSH.Console
(*
NOTE: We used a fork of Renci to fix a bug. I expect the bug has been fixed in Renci by now. If not let me know and I will make my Renci fork public.
The bug either causes the connection not to work or the console to hang. You'll know if it's there.
*)
open System
open System.Globalization
open System.Windows
open System.Windows.Controls
open System.Windows.Data
open System.Windows.Input
open System.Windows.Media
open System.Windows.Media.Animation
open System.Windows.Shapes
open Renci.SshNet
open Renci.SshNet.Common
open System.ComponentModel
open System.Diagnostics
open System.Reactive.Linq
open System.Text
open System.Threading.Tasks
open System.Windows.Data
open System.Windows.Input
open System.Runtime.InteropServices
open System.Windows.Input
open System.Text
open System.Collections.Generic
open System.ComponentModel
open System.Collections.Generic
open System.Text
[<AutoOpen>]
module Utils =
open System
open System.Threading.Tasks
let inline defaultTo def x = match x with | null -> def | _ -> x
let awaitTask (t : Task) : Async<unit> =
let getException (t : Task) : Exception =
if t.IsFaulted then
let ex =
if t.Exception.InnerExceptions.Count = 1 then
t.Exception.InnerException
else
t.Exception :> Exception
ex
else
null
async {
let! ex = t.ContinueWith getException |> Async.AwaitTask
if ex <> null then raise ex
}
let ignoreErrorsInFn f x =
try
f x
with
| ex -> ()
module Option =
let defaultTo def x = match x with | None -> def | Some value -> value
module List =
let choosei f = List.mapi f >> List.choose id
let getPartitionLocations selector =
let partitionAnItem s t =
let value = selector t
match s with
| [] -> [ value, 0, 1 ]
| (value0, start, len) :: xs ->
if value = value0 then (value0, start, len + 1) :: xs
else (value, start + len, 1) :: (value0, start, len) :: xs
Seq.fold partitionAnItem []
type System.Windows.Threading.DispatcherObject with
member this.InvokeOnDispatcher f =
this.Dispatcher.BeginInvoke(Action f) |> ignore
type Coord = {
X : int
Y : int
} with
static member Origin = { Coord.X = 0; Y = 0 }
type Selection = {
Start : Coord
End : Coord
} with
static member Empty(coord) = { Selection.Start = coord; End = coord }
static member Null = Selection.Empty(Coord.Origin)
member this.IsEmpty = this.Start = this.End
member this.First = min this.Start this.End
member this.Last = max this.Start this.End
member this.ExtendTo position =
if position = this.End then this
else { Selection.Start = this.Start; End = position }
module Colours =
let ColourMap =
dict [
// Primary 3-bit (8 colors)
(00, 0x000000); (01, 0x800000); (02, 0x008000); (03, 0x808000); (04, 0x000080); (05, 0x800080); (06, 0x008080); (07, 0xc0c0c0);
// Equivalent "bright" versions of original 8 colors
(08, 0x808080); (09, 0xff0000); (10, 0x00ff00); (11, 0xffff00); (12, 0x0000ff); (13, 0xff00ff); (14, 0x00ffff); (15, 0xffffff);
// Strictly ascending
(16, 0x000000); (17, 0x00005f); (18, 0x000087); (19, 0x0000af); (20, 0x0000d7); (21, 0x0000ff); (22, 0x005f00); (23, 0x005f5f);
(24, 0x005f87); (25, 0x005faf); (26, 0x005fd7); (27, 0x005fff); (28, 0x008700); (29, 0x00875f); (30, 0x008787); (31, 0x0087af);
(32, 0x0087d7); (33, 0x0087ff); (34, 0x00af00); (35, 0x00af5f); (36, 0x00af87); (37, 0x00afaf); (38, 0x00afd7); (39, 0x00afff);
(40, 0x00d700); (41, 0x00d75f); (42, 0x00d787); (43, 0x00d7af); (44, 0x00d7d7); (45, 0x00d7ff); (46, 0x00ff00); (47, 0x00ff5f);
(48, 0x00ff87); (49, 0x00ffaf); (50, 0x00ffd7); (51, 0x00ffff); (52, 0x5f0000); (53, 0x5f005f); (54, 0x5f0087); (55, 0x5f00af);
(56, 0x5f00d7); (57, 0x5f00ff); (58, 0x5f5f00); (59, 0x5f5f5f); (60, 0x5f5f87); (61, 0x5f5faf); (62, 0x5f5fd7); (63, 0x5f5fff);
(64, 0x5f8700); (65, 0x5f875f); (66, 0x5f8787); (67, 0x5f87af); (68, 0x5f87d7); (69, 0x5f87ff); (70, 0x5faf00); (71, 0x5faf5f);
(72, 0x5faf87); (73, 0x5fafaf); (74, 0x5fafd7); (75, 0x5fafff); (76, 0x5fd700); (77, 0x5fd75f); (78, 0x5fd787); (79, 0x5fd7af);
(80, 0x5fd7d7); (81, 0x5fd7ff); (82, 0x5fff00); (83, 0x5fff5f); (84, 0x5fff87); (85, 0x5fffaf); (86, 0x5fffd7); (87, 0x5fffff);
(88, 0x870000); (89, 0x87005f); (90, 0x870087); (91, 0x8700af); (92, 0x8700d7); (93, 0x8700ff); (94, 0x875f00); (95, 0x875f5f);
(96, 0x875f87); (97, 0x875faf); (98, 0x875fd7); (99, 0x875fff); (100, 0x878700); (101, 0x87875f); (102, 0x878787); (103, 0x8787af);
(104, 0x8787d7); (105, 0x8787ff); (106, 0x87af00); (107, 0x87af5f); (108, 0x87af87); (109, 0x87afaf); (110, 0x87afd7); (111, 0x87afff);
(112, 0x87d700); (113, 0x87d75f); (114, 0x87d787); (115, 0x87d7af); (116, 0x87d7d7); (117, 0x87d7ff); (118, 0x87ff00); (119, 0x87ff5f);
(120, 0x87ff87); (121, 0x87ffaf); (122, 0x87ffd7); (123, 0x87ffff); (124, 0xaf0000); (125, 0xaf005f); (126, 0xaf0087); (127, 0xaf00af);
(128, 0xaf00d7); (129, 0xaf00ff); (130, 0xaf5f00); (131, 0xaf5f5f); (132, 0xaf5f87); (133, 0xaf5faf); (134, 0xaf5fd7); (135, 0xaf5fff);
(136, 0xaf8700); (137, 0xaf875f); (138, 0xaf8787); (139, 0xaf87af); (140, 0xaf87d7); (141, 0xaf87ff); (142, 0xafaf00); (143, 0xafaf5f);
(144, 0xafaf87); (145, 0xafafaf); (146, 0xafafd7); (147, 0xafafff); (148, 0xafd700); (149, 0xafd75f); (150, 0xafd787); (151, 0xafd7af);
(152, 0xafd7d7); (153, 0xafd7ff); (154, 0xafff00); (155, 0xafff5f); (156, 0xafff87); (157, 0xafffaf); (158, 0xafffd7); (159, 0xafffff);
(160, 0xd70000); (161, 0xd7005f); (162, 0xd70087); (163, 0xd700af); (164, 0xd700d7); (165, 0xd700ff); (166, 0xd75f00); (167, 0xd75f5f);
(168, 0xd75f87); (169, 0xd75faf); (170, 0xd75fd7); (171, 0xd75fff); (172, 0xd78700); (173, 0xd7875f); (174, 0xd78787); (175, 0xd787af);
(176, 0xd787d7); (177, 0xd787ff); (178, 0xd7af00); (179, 0xd7af5f); (180, 0xd7af87); (181, 0xd7afaf); (182, 0xd7afd7); (183, 0xd7afff);
(184, 0xd7d700); (185, 0xd7d75f); (186, 0xd7d787); (187, 0xd7d7af); (188, 0xd7d7d7); (189, 0xd7d7ff); (190, 0xd7ff00); (191, 0xd7ff5f);
(192, 0xd7ff87); (193, 0xd7ffaf); (194, 0xd7ffd7); (195, 0xd7ffff); (196, 0xff0000); (197, 0xff005f); (198, 0xff0087); (199, 0xff00af);
(200, 0xff00d7); (201, 0xff00ff); (202, 0xff5f00); (203, 0xff5f5f); (204, 0xff5f87); (205, 0xff5faf); (206, 0xff5fd7); (207, 0xff5fff);
(208, 0xff8700); (209, 0xff875f); (210, 0xff8787); (211, 0xff87af); (212, 0xff87d7); (213, 0xff87ff); (214, 0xffaf00); (215, 0xffaf5f);
(216, 0xffaf87); (217, 0xffafaf); (218, 0xffafd7); (219, 0xffafff); (220, 0xffd700); (221, 0xffd75f); (222, 0xffd787); (223, 0xffd7af);
(224, 0xffd7d7); (225, 0xffd7ff); (226, 0xffff00); (227, 0xffff5f); (228, 0xffff87); (229, 0xffffaf); (230, 0xffffd7); (231, 0xffffff);
// Gray-scale range
(232, 0x080808); (233, 0x121212); (234, 0x1c1c1c); (235, 0x262626); (236, 0x303030); (237, 0x3a3a3a); (238, 0x444444); (239, 0x4e4e4e);
(240, 0x585858); (241, 0x626262); (242, 0x6c6c6c); (243, 0x767676); (244, 0x808080); (245, 0x8a8a8a); (246, 0x949494); (247, 0x9e9e9e);
(248, 0xa8a8a8); (249, 0xb2b2b2); (250, 0xbcbcbc); (251, 0xc6c6c6); (252, 0xd0d0d0); (253, 0xdadada); (254, 0xe4e4e4); (255, 0xeeeeee) ]
type Colour =
| Black
| Red
| Green
| Yellow
| Blue
| Magenta
| Cyan
| White
| BrightBlack
| BrightRed
| BrightGreen
| BrightYellow
| BrightBlue
| BrightMagenta
| BrightCyan
| BrightWhite
| RGB of (byte * byte * byte)
| Palette of byte with
static member RgbFromInt (n : int) = (byte (n >>> 16) &&& 0xFFuy, byte (n >>> 8) &&& 0xFFuy, byte n &&& 0xFFuy)
member this.AsRgb =
match this with
| Black -> Colour.RgbFromInt(Colours.ColourMap.[0])
| Red -> Colour.RgbFromInt(Colours.ColourMap.[1])
| Green -> Colour.RgbFromInt(Colours.ColourMap.[2])
| Yellow -> Colour.RgbFromInt(Colours.ColourMap.[3])
| Blue -> Colour.RgbFromInt(Colours.ColourMap.[4])
| Magenta -> Colour.RgbFromInt(Colours.ColourMap.[5])
| Cyan -> Colour.RgbFromInt(Colours.ColourMap.[6])
| White -> Colour.RgbFromInt(Colours.ColourMap.[7])
| BrightBlack -> Colour.RgbFromInt(Colours.ColourMap.[8])
| BrightRed -> Colour.RgbFromInt(Colours.ColourMap.[9])
| BrightGreen -> Colour.RgbFromInt(Colours.ColourMap.[10])
| BrightYellow -> Colour.RgbFromInt(Colours.ColourMap.[11])
| BrightBlue -> Colour.RgbFromInt(Colours.ColourMap.[12])
| BrightMagenta -> Colour.RgbFromInt(Colours.ColourMap.[13])
| BrightCyan -> Colour.RgbFromInt(Colours.ColourMap.[14])
| BrightWhite -> Colour.RgbFromInt(Colours.ColourMap.[15])
| RGB (r, g, b) -> (r, g, b)
| Palette n -> Colour.RgbFromInt(Colours.ColourMap.[int n])
[<Flags>]
type InputModifiers =
| None = 0
| Shift = 1
| Control = 2
| Alt = 4
type InputCommand =
| Character of char
| Return
| Tab
| Backspace
| Insert
| Delete
| Left
| Right
| Up
| Down
| PageUp
| PageDown
| Home
| End
| Escape
| Function of (byte * InputModifiers)
| PastedText of string
type FontWeight =
| Normal
| Bold
| Light
[<Flags>]
type FontStyle =
| Normal = 0x0
| Oblique = 0x1
| Italic = 0x2
| Underline = 0x4
| Blink = 0x8
| Inverse = 0x10
| Hidden = 0x20
[<Flags>]
type CursorStyle =
| Blinking = 0
| Steady = 1
| Block = 0
| Underline = 2
| Bar = 4
type ScrollRegion = (int * int) option
type OutputCommand =
| Character of char * ScrollRegion option
| Tab
| SetTabStop
| ClearTabStop
| ClearAllTabStops
| MoveTo of Coord
| MoveToColumn of int
| MoveToRow of int
| MoveLeft of int * ScrollRegion option
| MoveRight of int * ScrollRegion option
| MoveUp of int * ScrollRegion option
| MoveDown of int * ScrollRegion option
| MoveToLineStart
| MoveToLineEnd
| ScrollUp of int * ScrollRegion
| ScrollDown of int * ScrollRegion
| ClearScreen
| ClearToScreenStart
| ClearToScreenEnd
| ClearLine
| ClearToLineStart
| ClearToLineEnd
| InsertLines of int
| DeleteLines of int
| SetCurrentAttributesDefault
| SetCurrentForegroundColour of Colour
| ClearCurrentForegroundColour
| SetCurrentBackgroundColour of Colour
| ClearCurrentBackgroundColour
| SetCurrentFontWeight of FontWeight
| SetCurrentFontStyle of FontStyle
| ClearCurrentFontStyle of FontStyle
| SaveCursor of int
| RestoreCursor of int
| SetCursorStyle of CursorStyle
| UseScreenBuffer of int
| ScrollUpThroughBuffer of int
| Bell
| SetAudioVolume of float
| SetWindowTitle of string
| SetIconName of string
module XTerm =
module ControlChars =
/// <summary>Ignored on input (not stored in input buffer; see full duplex protocol).</summary>
[<Literal>]
let NUL = 0x00uy
/// <summary>Transmit answerback message.</summary>
[<Literal>]
let ENQ = 0x05uy
/// <summary>Sound bell tone from keyboard.</summary>
[<Literal>]
let BEL = 0x07uy
/// <summary>Move the cursor to the left one character position, unless it is at the left margin, in which case no action occurs.</summary>
[<Literal>]
let BS = 0x08uy
/// <summary>Move the cursor to the next tab stop, or to the right margin if no further tab stops are present on the line.</summary>
[<Literal>]
let HT = 0x09uy
/// <summary>This code causes a line feed or a new line operation (see new line mode).</summary>
[<Literal>]
let LF = 0x0Auy
/// <summary>Interpreted as LF.</summary>
[<Literal>]
let VT = 0x0Buy
/// <summary>Interpreted as LF.</summary>
[<Literal>]
let FF = 0x0Cuy
/// <summary>Move cursor to the left margin on the current line.</summary>
[<Literal>]
let CR = 0x0Duy
/// <summary>Invoke G1 character set, as designated by SCS control sequence.</summary>
[<Literal>]
let SO = 0x0Euy
/// <summary>Select G0 character set, as selected by ESC ( sequence.</summary>
[<Literal>]
let SI = 0x0Fuy
/// <summary>Causes terminal to resume transmission.</summary>
[<Literal>]
let XON = 0x11uy
/// <summary>Causes terminal to stop transmitted all codes except XOFF and XON.</summary>
[<Literal>]
let XOFF = 0x13uy
/// <summary>If sent during a control sequence, the sequence is immediately terminated and not executed and the error character is displayed.</summary>
[<Literal>]
let CAN = 0x18uy
/// <summary>Interpreted as CAN.</summary>
[<Literal>]
let SUB = 0x1Auy
/// <summary>Invokes a control sequence.</summary>
[<Literal>]
let ESC = 0x1Buy
[<Literal>]
let FS = 0x1Cuy
[<Literal>]
let GS = 0x1Duy
[<Literal>]
let RS = 0x1Euy
[<Literal>]
let US = 0x1Fuy
/// <summary>Ignored on input (not stored in input buffer).</summary>
[<Literal>]
let DEL = 0x7Fuy
[<Literal>]
let IND = 0x84uy
[<Literal>]
let NEL = 0x85uy
[<Literal>]
let HTS = 0x88uy
[<Literal>]
let RI = 0x8Duy
[<Literal>]
let SS2 = 0x8Euy
[<Literal>]
let SS3 = 0x8Fuy
[<Literal>]
let DCS = 0x90uy
[<Literal>]
let SPA = 0x96uy
[<Literal>]
let EPA = 0x97uy
[<Literal>]
let SOS = 0x98uy
[<Literal>]
let DECID = 0x9Auy
[<Literal>]
let CSI = 0x9Buy
[<Literal>]
let ST = 0x9Cuy
[<Literal>]
let OSC = 0x9Duy
[<Literal>]
let PM = 0x9Euy
[<Literal>]
let APC = 0x9Fuy
let private getAsciiChar b = (Encoding.ASCII.GetChars [| b |]).[0]
let private getCharAsciiCodes (c : char) = Encoding.ASCII.GetBytes [| c |] |> List.ofArray
let private getStrAsciiCodes (s : string) = Encoding.ASCII.GetBytes s |> List.ofArray
type CharacterSets =
| DecSpecialCharacterLineDrawing
| UnitedKingdom
| UnitedStates
| Dutch
| Finnish
| French
| FrenchCanadian
| German
| Italian
| NorwegianDanish
| Spanish
| Swedish
| Swiss
with
static member FromCode =
function
| '0' -> DecSpecialCharacterLineDrawing
| 'A' -> UnitedKingdom
| 'B' -> UnitedStates
| '4' -> Dutch
| 'C'
| '5' -> Finnish
| 'R' -> French
| 'Q' -> FrenchCanadian
| 'K' -> German
| 'Y' -> Italian
| 'E'
| '6' -> NorwegianDanish
| 'Z' -> Spanish
| 'H'
| '7' -> Swedish
| '=' -> Swiss
| _ -> invalidArg "c" "Character did not represent a valid character set"
type CustomModes =
| KeypadAsCodes
| Use8BitControls
type State(scrollRegion : ScrollRegion,
modes : Set<int>, customModes : Set<CustomModes>,
characterSets : Map<int, CharacterSets>,
incompleteCtrlSeq : CtrlSeq option, incompleteOSCtrlSeq : OSCtrlSeq option) =
new() = State(None, Set.empty, Set.empty, Map.empty, None, None)
member this.ScrollRegion = scrollRegion
member this.ClearScrollRegion = State(None, modes, customModes, characterSets, None, None)
member this.UpdateScrollRegion (top, afterBottom) =
let top = top |> max 0
let afterBottom = afterBottom |> max (top + 1)
State(Some (top, afterBottom), modes, customModes, characterSets, None, None)
member this.GetDeviceIndependantRow y =
let topLine =
if modes.Contains 6 && scrollRegion.IsSome then
fst scrollRegion.Value + 1
else
1
y - topLine |> max 0
member this.GetMoveToCommand x y =
[ { Coord.X = x - 1 |> max 0; Y = this.GetDeviceIndependantRow y } |> OutputCommand.MoveTo ]
member this.IsInCtrlSeq = incompleteCtrlSeq.IsSome || incompleteOSCtrlSeq.IsSome
member this.ClearCtrlSeq = State(scrollRegion, modes, customModes, characterSets, None, None)
member this.UpdateCtrlSeq ctrlSeq = State(scrollRegion, modes, customModes, characterSets, Some ctrlSeq, incompleteOSCtrlSeq)
member this.ProcessNextCtrlSeqChar =
match incompleteCtrlSeq with
| None ->
match incompleteOSCtrlSeq with
| None -> invalidOp "Can't ProcessNextCtrlSeqChar when incompleteCtrlSeq and incompleteOSCtrlSeq are None"
| Some osCtrlSeq -> osCtrlSeq.ProcessNextChar this
| Some ctrlSeq -> ctrlSeq.ProcessNextChar this
member this.IsInOSCtrlSeq = incompleteOSCtrlSeq.IsSome
member this.EndOSCtrlSeq =
match incompleteOSCtrlSeq with
| None -> invalidOp "Can't EndOSCtrlSeq when incompleteOSCtrlSeq is None"
| Some osCtrlSeq ->
State(scrollRegion, modes, customModes, characterSets, None, None),
osCtrlSeq.Execute
member this.UpdateOSCtrlSeq osCtrlSeq = State(scrollRegion, modes, customModes, characterSets, None, Some osCtrlSeq)
member this.SetMode nonAnsi mode =
State(scrollRegion, modes.Add mode, customModes, characterSets, None, None),
if not nonAnsi then
match mode with
| 2 -> [] // Keyboard Action Mode (AM)
| 4 -> [] // Insert Mode (IRM)
| 12 -> [] // Send/receive (SRM)
| 20 -> [] // Automatic Newline (LNM)
| _ -> []
else
match mode with
| 1 -> [] // Application Cursor Keys (DECCKM).
| 2 -> [] // Designate USASCII for character sets G0-G3 (DECANM), and set VT100 mode.
| 3 -> [] // 132 Column Mode (DECCOLM).
| 4 -> [] // Smooth (Slow) Scroll (DECSCLM).
| 5 -> [] // Reverse Video (DECSCNM).
| 6 -> // Origin Mode (DECOM).
this.GetMoveToCommand 1 1
| 7 -> [] // Wraparound Mode (DECAWM).
| 8 -> [] // Auto-repeat Keys (DECARM).
| 9 -> [] // Send Mouse X & Y on button press. See the section Mouse Tracking.
| 10 -> [] // Show toolbar (rxvt).
| 12 -> [] // Start Blinking Cursor (att610).
| 18 -> [] // Print form feed (DECPFF).
| 19 -> [] // Set print extent to full screen (DECPEX).
| 25 -> [] // Show Cursor (DECTCEM).
| 30 -> [] // Show scrollbar (rxvt).
| 35 -> [] // Enable font-shifting functions (rxvt).
| 38 -> [] // Enter Tektronix Mode (DECTEK).
| 40 -> [] // Allow 80 → 132 Mode.
| 41 -> [] // more(1) fix (see curses resource).
| 42 -> [] // Enable Nation Replacement Character sets (DECNRCM).
| 44 -> [] // Turn On Margin Bell.
| 45 -> [] // Reverse-wraparound Mode.
| 46 -> [] // Start Logging. This is normally disabled by a compile-time option.
| 47 -> // Use Alternate Screen Buffer. (This may be disabled by the titeInhibit resource).
[ OutputCommand.UseScreenBuffer 1 ]
| 66 -> [] // Application keypad (DECNKM).
| 67 -> [] // Backarrow key sends backspace (DECBKM).
| 69 -> [] // Enable left and right margin mode (DECLRMM), VT420 and up.
| 95 -> [] // Do not clear screen when DECCOLM is set/reset (DECNCSM), VT510 and up.
| 1000 -> [] // Send Mouse X & Y on button press and release. See the section Mouse Tracking.
| 1001 -> [] // Use Hilite Mouse Tracking.
| 1002 -> [] // Use Cell Motion Mouse Tracking.
| 1003 -> [] // Use All Motion Mouse Tracking.
| 1004 -> [] // Send FocusIn/FocusOut events.
| 1005 -> [] // Enable UTF-8 Mouse Mode.
| 1006 -> [] // Enable SGR Mouse Mode.
| 1007 -> [] // Enable Alternate Scroll Mode.
| 1010 -> [] // Scroll to bottom on tty output (rxvt).
| 1015 -> [] // Enable urxvt Mouse Mode.
| 1011 -> [] // Scroll to bottom on key press (rxvt).
| 1034 -> [] // Interpret "meta" key, sets eighth bit. (enables the eightBitInput resource).
| 1035 -> [] // Enable special modifiers for Alt and NumLock keys. (This enables the numLock resource).
| 1036 -> [] // Send ESC when Meta modifies a key. (This enables the metaSendsEscape resource).
| 1037 -> [] // Send DEL from the editing-keypad Delete key.
| 1039 -> [] // Send ESC when Alt modifies a key. (This enables the altSendsEscape resource).
| 1040 -> [] // Keep selection even if not highlighted. (This enables the keepSelection resource).
| 1041 -> [] // Use the CLIPBOARD selection. (This enables the selectToClipboard resource).
| 1042 -> [] // Enable Urgency window manager hint when Control-G is received. (This enables the bellIsUrgent resource).
| 1043 -> [] // Enable raising of the window when Control-G is received. (enables the popOnBell resource).
| 1047 -> // Use Alternate Screen Buffer. (This may be disabled by the titeInhibit resource).
[ OutputCommand.UseScreenBuffer 1; OutputCommand.ClearScreen ]
| 1048 -> // Save cursor as in DECSC. (This may be disabled by the titeInhibit resource).
[ OutputCommand.SaveCursor 0 ]
| 1049 -> // Save cursor as in DECSC and use Alternate Screen Buffer, clearing it first. (This may be disabled by the titeInhibit resource). This combines the effects of the 1047 and 1048 modes. Use this with terminfo-based applications rather than the 47 mode.
[ OutputCommand.SaveCursor 0; OutputCommand.UseScreenBuffer 1; OutputCommand.ClearScreen ]
| 1050 -> [] // Set terminfo/termcap function-key mode.
| 1051 -> [] // Set Sun function-key mode.
| 1052 -> [] // Set HP function-key mode.
| 1053 -> [] // Set SCO function-key mode.
| 1060 -> [] // Set legacy keyboard emulation (X11R6).
| 1061 -> [] // Set VT220 keyboard emulation.
| 2004 -> [] // Set bracketed paste mode.
| _ -> []
member this.ResetMode nonAnsi mode =
State(scrollRegion, modes.Remove mode, customModes, characterSets, None, None),
if not nonAnsi then
match mode with
| 2 -> [] // Keyboard Action Mode (AM).
| 4 -> [] // Replace Mode (IRM).
| 12 -> [] // Send/receive (SRM).
| 20 -> [] // Normal Linefeed (LNM).
| _ -> []
else
match mode with
| 1 -> [] // Normal Cursor Keys (DECCKM).
| 2 -> [] // Designate VT52 mode (DECANM).
| 3 -> [] // 80 Column Mode (DECCOLM).
| 4 -> [] // Jump (Fast) Scroll (DECSCLM).
| 5 -> [] // Normal Video (DECSCNM).
| 6 -> // Normal Cursor Mode (DECOM).
this.GetMoveToCommand 1 1
| 7 -> [] // No Wraparound Mode (DECAWM).
| 8 -> [] // No Auto-repeat Keys (DECARM).
| 9 -> [] // Don’t send Mouse X & Y on button press.
| 10 -> [] // Hide toolbar (rxvt).
| 12 -> [] // Stop Blinking Cursor (att610).
| 18 -> [] // Don’t print form feed (DECPFF).
| 19 -> [] // Limit print to scrolling region (DECPEX).
| 25 -> [] // Hide Cursor (DECTCEM).
| 30 -> [] // Don’t show scrollbar (rxvt).
| 35 -> [] // Disable font-shifting functions (rxvt).
| 40 -> [] // Disallow 80 → 132 Mode.
| 41 -> [] // No more(1) fix (see curses resource).
| 42 -> [] // Disable Nation Replacement Character sets (DECNRCM).
| 44 -> [] // Turn Off Margin Bell.
| 45 -> [] // No Reverse-wraparound Mode.
| 46 -> [] // Stop Logging. (This is normally disabled by a compile-time option).
| 47 -> // Use Normal Screen Buffer.
[ OutputCommand.UseScreenBuffer 0 ]
| 66 -> [] // Numeric keypad (DECNKM).
| 67 -> [] // Backarrow key sends delete (DECBKM).
| 69 -> [] // Disable left and right margin mode (DECLRMM), VT420 and up.
| 95 -> [] // Clear screen when DECCOLM is set/reset (DECNCSM), VT510 and up.
| 1000 -> [] // Don’t send Mouse X & Y on button press and release. See the section Mouse Tracking.
| 1001 -> [] // Don’t use Hilite Mouse Tracking.
| 1002 -> [] // Don’t use Cell Motion Mouse Tracking.
| 1003 -> [] // Don’t use All Motion Mouse Tracking.
| 1004 -> [] // Don’t send FocusIn/FocusOut events.
| 1005 -> [] // Disable UTF-8 Mouse Mode.
| 1006 -> [] // Disable SGR Mouse Mode.
| 1007 -> [] // Disable Alternate Scroll Mode.
| 1010 -> [] // Don’t scroll to bottom on tty output (rxvt).
| 1015 -> [] // Disable urxvt Mouse Mode.
| 1011 -> [] // Don’t scroll to bottom on key press (rxvt).
| 1034 -> [] // Don’t interpret "meta" key. (This disables the eightBitInput resource).
| 1035 -> [] // Disable special modifiers for Alt and NumLock keys. (This disables the numLock resource).
| 1036 -> [] // Don’t send ESC when Meta modifies a key. (This disables the metaSendsEscape resource).
| 1037 -> [] // Send VT220 Remove from the editing-keypad Delete key.
| 1039 -> [] // Don’t send ESC when Alt modifies a key. (This disables the altSendsEscape resource).
| 1040 -> [] // Do not keep selection when not highlighted. (This disables the keepSelection resource).
| 1041 -> [] // Use the PRIMARY selection. (This disables the selectToClipboard resource).
| 1042 -> [] // Disable Urgency window manager hint when Control-G is received. (This disables the bellIsUrgent resource).
| 1043 -> [] // Disable raising of the window when Control-G is received. (This disables the popOnBell resource).
| 1047 -> // Use Normal Screen Buffer, clearing screen first if in the Alternate Screen. (This may be disabled by the titeInhibit resource).
[ OutputCommand.ClearScreen; OutputCommand.UseScreenBuffer 0 ]
| 1048 -> // Restore cursor as in DECRC. (This may be disabled by the titeInhibit resource).
[ OutputCommand.RestoreCursor 0 ]
| 1049 -> // Use Normal Screen Buffer and restore cursor as in DECRC. (This may be disabled by the titeInhibit resource). This combines the effects of the 1047 and 1048 modes. Use this with terminfo-based applications rather than the 4 7 mode.
[ OutputCommand.UseScreenBuffer 0; OutputCommand.RestoreCursor 0 ]
| 1050 -> [] // Reset terminfo/termcap function-key mode.
| 1051 -> [] // Reset Sun function-key mode.
| 1052 -> [] // Reset HP function-key mode.
| 1053 -> [] // Reset SCO function-key mode.
| 1060 -> [] // Reset legacy keyboard emulation (X11R6).
| 1061 -> [] // Reset keyboard emulation to Sun/PC style.
| 2004 -> [] // Reset bracketed paste mode.
| _ -> []
member this.SetCustomMode mode = State(scrollRegion, modes, customModes.Add mode, characterSets, None, None)
member this.ResetCustomMode mode = State(scrollRegion, modes, customModes.Remove mode, characterSets, None, None)
member this.ScrollRegionIfWrapping = if modes.Contains 7 then Some scrollRegion else None
member this.SetCharSet setNo cs = State(scrollRegion, modes, customModes, characterSets.Add(setNo, cs), None, None)
and CtrlSeq(prefix : string, reversedParameters : int list, nonAnsi : bool) =
new(prefix : string) = CtrlSeq(prefix, [], false)
new() = CtrlSeq("")
member this.ProcessNextChar (state : State) (c : char) : (State * OutputCommand list) =
let moveTo x y = state.GetMoveToCommand x y
if prefix = "" then
match c with
| 'M' -> state.ClearCtrlSeq, [ OutputCommand.MoveUp (1, Some state.ScrollRegion) ] // Reverse Index (RI)
| 'D' -> state.ClearCtrlSeq, [ OutputCommand.MoveDown (1, Some state.ScrollRegion) ] // Index (IND)
| 'E' -> state.ClearCtrlSeq, [ OutputCommand.MoveDown (1, Some state.ScrollRegion); OutputCommand.MoveToLineStart ] // Next Line (NEL)
| 'H' -> state.ClearCtrlSeq, [ OutputCommand.SetTabStop ] // Tab Set (HTS)
| '[' -> CtrlSeq(c.ToString(), [ 0 ], nonAnsi) |> state.UpdateCtrlSeq, [] // Control Sequence Introducer (CSI)
| 'N' -> state.ClearCtrlSeq, [] // Single Shift Select of G2 Character Set (affects next character only) (SS2)
| 'O' -> state.ClearCtrlSeq, [] // Single Shift Select of G3 Character Set (affects next character only) (SS3)
| 'P' -> state.ClearCtrlSeq, [] // Device Control String (DCS)
| 'V' -> state.ClearCtrlSeq, [] // Start of Guarded Area (SPA)
| 'W' -> state.ClearCtrlSeq, [] // End of Guarded Area (EPA)
| 'X' -> state.ClearCtrlSeq, [] // Start of String (SOS)
| '|' -> state.ClearCtrlSeq, [] // Return Terminal ID (DECID)
| ']' -> OSCtrlSeq() |> state.UpdateOSCtrlSeq, [] // Operating System Command (OSC)
| '\\' -> // String Terminator (ST)
if state.IsInOSCtrlSeq then
state.EndOSCtrlSeq
else
state.ClearCtrlSeq, []
| '^' -> state.ClearCtrlSeq, [] // Privacy Message (PM)
| '_' -> state.ClearCtrlSeq, [] // Application Program Command (APC)
| '6' -> state.ClearCtrlSeq, [] // Back Index (DECBI)
| '7' -> state.ClearCtrlSeq, [ OutputCommand.SaveCursor 0 ] // Save Cursor (DECSC)
| '8' -> state.ClearCtrlSeq, [ OutputCommand.RestoreCursor 0 ] // Restore Cursor (DECRC)
| '9' -> state.ClearCtrlSeq, [] // Forward Index (DECFI)
| '=' -> state.SetCustomMode KeypadAsCodes, [] // Keypad Application Mode (DECKPAM)
| '>' -> state.ResetCustomMode KeypadAsCodes, [] // Keypad Numeric Mode (DECKPNM)
| 'C' -> state.ClearCtrlSeq, [] // Full Reset (RIS)
| 'n' -> state.ClearCtrlSeq, [] // Invoke the G2 Character Set as GL (LS2).
| 'o' -> state.ClearCtrlSeq, [] // Invoke the G3 Character Set as GL (LS3).
// | '|' -> state.ClearCtrlSeq, [] // Invoke the G3 Character Set as GR (LS3R).
| '}' -> state.ClearCtrlSeq, [] // Invoke the G2 Character Set as GR (LS2R).
| '~' -> state.ClearCtrlSeq, [] // Invoke the G1 Character Set as GR (LS1R).
| _ -> CtrlSeq(c.ToString(), [], nonAnsi) |> state.UpdateCtrlSeq, []
elif not reversedParameters.IsEmpty && c >= '0' && c <= '9' then
CtrlSeq(prefix, reversedParameters.Head * 10 + (int c - int '0') :: reversedParameters.Tail, nonAnsi) |> state.UpdateCtrlSeq, []
elif not reversedParameters.IsEmpty && c = ';' then
CtrlSeq(prefix, 0 :: reversedParameters, nonAnsi) |> state.UpdateCtrlSeq, []
elif reversedParameters = [ 0 ] && c = '?' then
CtrlSeq(prefix, reversedParameters, true) |> state.UpdateCtrlSeq, []
else
let parameters : int list = List.rev reversedParameters
let chooseParams (f : int -> OutputCommand option) = state.ClearCtrlSeq, List.choose f parameters
let collectParams (f : int -> OutputCommand list) = state.ClearCtrlSeq, List.collect f parameters
let foldParams (f : State -> int -> State * OutputCommand list) =
let applyFAndAppendCmds (state, cmds) p =
let (newState, newCmds) = f state p
newState, cmds @ newCmds
List.fold applyFAndAppendCmds (state, []) parameters
let sumMinOneParams() = List.sumBy (max 1) parameters
match prefix with
| "[" ->
match c with
| 'b' // Repeat the preceding graphic character (REP)
| 'X' // Erase Characters (ECH)
| 'P' // Delete Characters (DCH)
| '@' -> collectParams (fun p -> OutputCommand.Character (' ', state.ScrollRegionIfWrapping) |> List.replicate p) // Insert Blank Character(s) (ICH)
| 'D' -> state.ClearCtrlSeq, [ OutputCommand.MoveLeft (sumMinOneParams(), None) ] // Cursor Backward (CUB)
| 'a' // Character Position Relative (HPR)
| 'C' -> state.ClearCtrlSeq, [ OutputCommand.MoveRight (sumMinOneParams(), None) ] // Cursor Forward (CUF)
| 'A' -> state.ClearCtrlSeq, [ OutputCommand.MoveUp (sumMinOneParams(), None) ] // Cursor Up (CUU)
| 'e' // Line Position Relative (VPR)
| 'B' -> state.ClearCtrlSeq, [ OutputCommand.MoveDown (sumMinOneParams(), None) ] // Cursor Down (CUD)
| 'E' -> state.ClearCtrlSeq, [ OutputCommand.MoveDown (sumMinOneParams(), Some state.ScrollRegion) ] // Cursor Next Line (CNL)
| 'F' -> state.ClearCtrlSeq, [ OutputCommand.MoveUp (sumMinOneParams(), Some state.ScrollRegion) ] // Cursor Preceding Line (CPL)
| 'I' -> state.ClearCtrlSeq, List.replicate (sumMinOneParams()) OutputCommand.Tab // Cursor Forward Tabulation (CHT)
| 'Z' -> state.ClearCtrlSeq, List.replicate (-sumMinOneParams()) OutputCommand.Tab // Cursor Forward Tabulation (CHT)
| 'J' ->
if nonAnsi then // Selective Erase in Display (DECSED)
collectParams (function
| 0 -> [ OutputCommand.ClearToScreenEnd ]
| 1 -> [ OutputCommand.ClearToScreenStart ]
| 2 -> [ OutputCommand.ClearScreen ]
| _ -> [])
else // Erase in Display (ED)
collectParams (function
| 0 -> [ OutputCommand.ClearToScreenEnd ]
| 1 -> [ OutputCommand.ClearToScreenStart ]
| 2 -> [ OutputCommand.ClearScreen ]
| _ -> [])
| 'K' ->
if nonAnsi then // Selective Erase in Line (DECSEL)
collectParams (function
| 0 -> [ OutputCommand.ClearToLineEnd ]
| 1 -> [ OutputCommand.ClearToLineStart ]
| 2 -> [ OutputCommand.ClearLine ]
| _ -> [])
else // Erase in Line (EL)
collectParams (function
| 0 -> [ OutputCommand.ClearToLineEnd ]
| 1 -> [ OutputCommand.ClearToLineStart ]
| 2 -> [ OutputCommand.ClearLine ]
| _ -> [])
| 'G' // Cursor Character Absolute (CHA)
| '`' -> // Character Position Absolute (HPA)
state.ClearCtrlSeq,
match parameters with
| [ x ] -> [ x - 1 |> max 0 |> OutputCommand.MoveToColumn ]
| _ -> [] // Too many parameters
| 'd' -> // Character Position Absolute (HPA)
state.ClearCtrlSeq,
match parameters with
| [ y ] -> [ state.GetDeviceIndependantRow y |> OutputCommand.MoveToRow ]
| _ -> [] // Too many parameters
| 'H' // Cursor Position (CUP)
| 'f' -> // Horizontal and Vertical Position (HVP)
state.ClearCtrlSeq,
match parameters with
| [ y; x ] -> moveTo x y
| [ y ] -> moveTo 1 y
| _ -> [] // Too many parameters
| 'L' -> state.ClearCtrlSeq, [ sumMinOneParams() |> OutputCommand.InsertLines ] // Insert Lines (IL)
| 'M' -> state.ClearCtrlSeq, [ sumMinOneParams() |> OutputCommand.DeleteLines ] // Delete Lines (DL)
| 'S' -> state.ClearCtrlSeq, [ OutputCommand.ScrollUp (sumMinOneParams(), state.ScrollRegion) ] // Scroll Up (SU)
| 'T' -> state.ClearCtrlSeq, [ OutputCommand.ScrollDown (sumMinOneParams(), state.ScrollRegion) ] // Scroll Down (SD)
| 'm' -> // Character Attributes (SGR)
match parameters with
| [ 38; 2; r; g; b ] ->
state.ClearCtrlSeq,
[ RGB (byte (min 255 r), byte (min 255 g), byte (min 255 b))
|> OutputCommand.SetCurrentForegroundColour ]
| [ 38; 5; idx ] ->
state.ClearCtrlSeq,
[ Palette (byte (min 255 idx)) |> OutputCommand.SetCurrentForegroundColour ]
| [ 48; 2; r; g; b ] ->
state.ClearCtrlSeq,
[ RGB (byte (min 255 r), byte (min 255 g), byte (min 255 b))
|> OutputCommand.SetCurrentBackgroundColour ]
| [ 48; 5; idx ] ->
state.ClearCtrlSeq,
[ Palette (byte (min 255 idx)) |> OutputCommand.SetCurrentBackgroundColour ]
| _ ->
chooseParams (function
| 0 -> OutputCommand.SetCurrentAttributesDefault |> Some
| 1 -> OutputCommand.SetCurrentFontWeight Bold |> Some
| 2 -> OutputCommand.SetCurrentFontWeight Light |> Some
| 4 -> OutputCommand.SetCurrentFontStyle FontStyle.Underline |> Some
| 5 -> OutputCommand.SetCurrentFontStyle FontStyle.Blink |> Some
| 7 -> OutputCommand.SetCurrentFontStyle FontStyle.Inverse |> Some
| 8 -> OutputCommand.SetCurrentFontStyle FontStyle.Hidden |> Some
| 22 -> OutputCommand.SetCurrentFontWeight Normal |> Some
| 24 -> OutputCommand.ClearCurrentFontStyle FontStyle.Underline |> Some
| 25 -> OutputCommand.ClearCurrentFontStyle FontStyle.Blink |> Some
| 27 -> OutputCommand.ClearCurrentFontStyle FontStyle.Inverse |> Some
| 28 -> OutputCommand.ClearCurrentFontStyle FontStyle.Hidden |> Some
| 30 -> OutputCommand.SetCurrentForegroundColour Black |> Some
| 31 -> OutputCommand.SetCurrentForegroundColour Red |> Some
| 32 -> OutputCommand.SetCurrentForegroundColour Green |> Some
| 33 -> OutputCommand.SetCurrentForegroundColour Yellow |> Some
| 34 -> OutputCommand.SetCurrentForegroundColour Blue |> Some
| 35 -> OutputCommand.SetCurrentForegroundColour Magenta |> Some
| 36 -> OutputCommand.SetCurrentForegroundColour Cyan |> Some
| 37 -> OutputCommand.SetCurrentForegroundColour White |> Some
| 39 -> OutputCommand.ClearCurrentForegroundColour |> Some
| 40 -> OutputCommand.SetCurrentBackgroundColour Black |> Some
| 41 -> OutputCommand.SetCurrentBackgroundColour Red |> Some
| 42 -> OutputCommand.SetCurrentBackgroundColour Green |> Some
| 43 -> OutputCommand.SetCurrentBackgroundColour Yellow |> Some
| 44 -> OutputCommand.SetCurrentBackgroundColour Blue |> Some
| 45 -> OutputCommand.SetCurrentBackgroundColour Magenta |> Some
| 46 -> OutputCommand.SetCurrentBackgroundColour Cyan |> Some
| 47 -> OutputCommand.SetCurrentBackgroundColour White |> Some
| 49 -> OutputCommand.ClearCurrentBackgroundColour |> Some
| 90 -> OutputCommand.SetCurrentForegroundColour BrightBlack |> Some
| 91 -> OutputCommand.SetCurrentForegroundColour BrightRed |> Some
| 92 -> OutputCommand.SetCurrentForegroundColour BrightGreen |> Some
| 93 -> OutputCommand.SetCurrentForegroundColour BrightYellow |> Some
| 94 -> OutputCommand.SetCurrentForegroundColour BrightBlue |> Some
| 95 -> OutputCommand.SetCurrentForegroundColour BrightMagenta |> Some
| 96 -> OutputCommand.SetCurrentForegroundColour BrightCyan |> Some
| 97 -> OutputCommand.SetCurrentForegroundColour BrightWhite |> Some
| 100 -> OutputCommand.SetCurrentBackgroundColour BrightBlack |> Some
| 101 -> OutputCommand.SetCurrentBackgroundColour BrightRed |> Some
| 102 -> OutputCommand.SetCurrentBackgroundColour BrightGreen |> Some
| 103 -> OutputCommand.SetCurrentBackgroundColour BrightYellow |> Some
| 104 -> OutputCommand.SetCurrentBackgroundColour BrightBlue |> Some
| 105 -> OutputCommand.SetCurrentBackgroundColour BrightMagenta |> Some
| 106 -> OutputCommand.SetCurrentBackgroundColour BrightCyan |> Some
| 107 -> OutputCommand.SetCurrentBackgroundColour BrightWhite |> Some
| _ -> None)
| 'g' -> // TBC - Tabulation Clear
chooseParams (function
| 0 -> Some OutputCommand.ClearTabStop
| 3 -> Some OutputCommand.ClearAllTabStops
| _ -> None)
| 'l' -> // Reset Mode (RM)
foldParams (fun state mode -> state.ResetMode nonAnsi mode)
| 'h' -> // Set Mode (SM)
foldParams (fun state mode -> state.SetMode nonAnsi mode)
| 'r' ->
match parameters with
| [ top; bottom ] -> state.UpdateScrollRegion (top - 1, bottom), moveTo 1 1
| _ -> state.ClearScrollRegion, moveTo 1 1
| ' '
| '!'
| '>' -> CtrlSeq(prefix + c.ToString(), reversedParameters, nonAnsi) |> state.UpdateCtrlSeq, []
| _ -> state.ClearCtrlSeq, []
| "[>" ->
match c with
| 'm' -> state.ClearCtrlSeq, [] // Set or reset resource-values used by xterm to decide whether to construct escape sequences holding information about the modifiers pressed with a given key
| 'p' -> state.ClearCtrlSeq, [] // Set resource value pointerMode. This is used by xterm to decide whether to hide the pointer cursor as the user types.
| _ -> state.ClearCtrlSeq, []
| "[!" ->
match c with
| 'p' -> state.ClearCtrlSeq, [] // Soft terminal reset (DECSTR)
| _ -> state.ClearCtrlSeq, []
| "[ " ->
match c with
| 'q' -> // Set cursor style (DECSCUSR, VT520)
chooseParams(function
| 0 -> CursorStyle.Blinking ||| CursorStyle.Block |> OutputCommand.SetCursorStyle |> Some
| 1 -> CursorStyle.Blinking ||| CursorStyle.Block |> OutputCommand.SetCursorStyle |> Some
| 2 -> CursorStyle.Steady ||| CursorStyle.Block |> OutputCommand.SetCursorStyle |> Some
| 3 -> CursorStyle.Blinking ||| CursorStyle.Underline |> OutputCommand.SetCursorStyle |> Some
| 4 -> CursorStyle.Steady ||| CursorStyle.Underline |> OutputCommand.SetCursorStyle |> Some
| 5 -> CursorStyle.Blinking ||| CursorStyle.Bar |> OutputCommand.SetCursorStyle |> Some
| 6 -> CursorStyle.Steady ||| CursorStyle.Bar |> OutputCommand.SetCursorStyle |> Some
| _ -> None)
| _ -> state.ClearCtrlSeq, []
| " " ->
match c with
| 'F' -> state.ResetCustomMode Use8BitControls, [] // 7-bit controls (S7C1T)
| 'G' -> state.SetCustomMode Use8BitControls, [] // 8-bit controls (S8C1T)
| 'L' -> state.ClearCtrlSeq, [] // Set ANSI conformance level 1 (dpANS X3.134.1)
| 'M' -> state.ClearCtrlSeq, [] // Set ANSI conformance level 2 (dpANS X3.134.1)
| 'N' -> state.ClearCtrlSeq, [] // Set ANSI conformance level 3 (dpANS X3.134.1)
| _ -> state.ClearCtrlSeq, []
| "#" ->
match c with
| '3' -> state.ClearCtrlSeq, [] // DEC double-height line, top half (DECDHL)
| '4' -> state.ClearCtrlSeq, [] // DEC double-height line, bottom half (DECDHL)
| '5' -> state.ClearCtrlSeq, [] // DEC single-width line (DECSWL)
| '6' -> state.ClearCtrlSeq, [] // DEC double-width line (DECDWL)
| '8' -> state.ClearCtrlSeq, [] // DEC Screen Alignment Test (DECALN)
| _ -> state.ClearCtrlSeq, []
| "%" ->
match c with
| '@' -> state.ClearCtrlSeq, [] // Select default ISO 8859-1 character set (ISO 2022)
| 'G' -> state.ClearCtrlSeq, [] // Select UTF-8 character set (ISO 2022)
| _ -> state.ClearCtrlSeq, []
| "(" ->
state.SetCharSet 0 (CharacterSets.FromCode c), [] // Designate G0 Character Set (ISO 2022, VT100)
| ")"
| "-" ->
state.SetCharSet 1 (CharacterSets.FromCode c), [] // Designate G0 Character Set (ISO 2022, VT100)
| "*"
| "." ->
state.SetCharSet 2 (CharacterSets.FromCode c), [] // Designate G0 Character Set (ISO 2022, VT100)
| "+"
| "/" ->
state.SetCharSet 3 (CharacterSets.FromCode c), [] // Designate G0 Character Set (ISO 2022, VT100)
| _ ->
state.ClearCtrlSeq, []
and OSCtrlSeq(parameter : int, text : string) =
new() = OSCtrlSeq(0, null)
member this.ProcessNextChar (state : State) (c : char) : (State * OutputCommand list) =
if text = null && c >= '0' && c <= '9' then
OSCtrlSeq(parameter * 10 + (int c - int '0'), null) |> state.UpdateOSCtrlSeq, []
elif text = null && c = ';' then
OSCtrlSeq(parameter, String.Empty) |> state.UpdateOSCtrlSeq, []
else
OSCtrlSeq(parameter, text + c.ToString()) |> state.UpdateOSCtrlSeq, []
member this.Execute =
match parameter with
| 0 -> [ OutputCommand.SetIconName text; OutputCommand.SetWindowTitle text ]
| 1 -> [ OutputCommand.SetIconName text ]
| 2 -> [ OutputCommand.SetWindowTitle text ]
| _ -> []
let Process (outputFromServer : IObservable<byte[]>, inputFromConsole : IObservable<InputCommand>) : (IObservable<byte[]> * IObservable<OutputCommand seq>) =
let processInput : InputCommand -> byte list =
function
| InputCommand.Character char -> getCharAsciiCodes char
| InputCommand.Return -> [ ControlChars.CR ]
| InputCommand.Tab -> [ ControlChars.HT ]
| InputCommand.Backspace -> [ ControlChars.BS ]
| InputCommand.Insert -> ControlChars.ESC :: getStrAsciiCodes "[2~"
| InputCommand.Delete -> ControlChars.ESC :: getStrAsciiCodes "[3~"
| InputCommand.Left -> ControlChars.ESC :: getStrAsciiCodes "[D"
| InputCommand.Right -> ControlChars.ESC :: getStrAsciiCodes "[C"
| InputCommand.Up -> ControlChars.ESC :: getStrAsciiCodes "[A"
| InputCommand.Down -> ControlChars.ESC :: getStrAsciiCodes "[B"
| InputCommand.PageUp -> ControlChars.ESC :: getStrAsciiCodes "[5~"
| InputCommand.PageDown -> ControlChars.ESC :: getStrAsciiCodes "[6~"
| InputCommand.Home -> ControlChars.ESC :: getStrAsciiCodes "[1~"
| InputCommand.End -> ControlChars.ESC :: getStrAsciiCodes "[4~"
| InputCommand.Escape -> [ ControlChars.ESC ]
| InputCommand.Function (value, mods) ->
let getModedString (n : int) =
if mods = InputModifiers.None then
ControlChars.ESC :: getStrAsciiCodes (String.Format("[{0}~", n))
else
let modCode =
(if mods.HasFlag InputModifiers.Shift then 1 else 0)
+ (if mods.HasFlag InputModifiers.Alt then 2 else 0)
+ (if mods.HasFlag InputModifiers.Control then 4 else 0)
+ 1
ControlChars.ESC :: getStrAsciiCodes (String.Format("[{0};{1}~", n, modCode))
match value with
| 1uy -> ControlChars.SS3 :: getStrAsciiCodes "P"
| 2uy -> ControlChars.SS3 :: getStrAsciiCodes "Q"
| 3uy -> ControlChars.SS3 :: getStrAsciiCodes "R"
| 4uy -> ControlChars.SS3 :: getStrAsciiCodes "S"
| 5uy -> getModedString 15
| 6uy -> getModedString 17
| 7uy -> getModedString 18
| 8uy -> getModedString 19
| 9uy -> getModedString 20
| 10uy -> getModedString 21
| 11uy -> getModedString 23
| 12uy -> getModedString 24
| _ -> []
| InputCommand.PastedText text -> text.Replace("\r\n", "\r").Replace("\n", "\r") |> getStrAsciiCodes
let processOutputByte (state : State, _) (c : byte) : (State * OutputCommand list) =
match c with
| ControlChars.DEL -> state, []
| ControlChars.BS -> state, [ OutputCommand.MoveLeft (1, state.ScrollRegionIfWrapping) ] // Backspace (BS)
| ControlChars.HT -> state, [ OutputCommand.Tab ] // Horizontal Tab (HT)
| ControlChars.VT // Vertical Tab (VT)
| ControlChars.FF // Form Feed or New Page (NP)
| ControlChars.LF -> state, [ OutputCommand.MoveDown (1, Some state.ScrollRegion) ] // Line Feed or New Line (NL)
| ControlChars.CR -> state, [ OutputCommand.MoveToLineStart ] // Carriage Return (CR)
| ControlChars.ESC -> CtrlSeq() |> state.UpdateCtrlSeq, []
| ControlChars.CAN
| ControlChars.SUB -> state.ClearCtrlSeq, []
| ControlChars.ENQ -> state, [] // Return Terminal Status (ENQ)
| ControlChars.SI -> state, [] // Shift In (SI) Switch to Standard Character Set (invoke the G0 character set) (the default).
| ControlChars.SO -> state, [] // Shift Out (SO) Switch to Alternate Character Set (invoke the G1 character set)
| ControlChars.IND -> state, [ OutputCommand.MoveDown (1, Some state.ScrollRegion) ] // Index (IND)
| ControlChars.NEL -> state, [ OutputCommand.MoveDown (1, Some state.ScrollRegion); OutputCommand.MoveToLineStart ] // Next Line (NEL)
| ControlChars.HTS -> state, [ OutputCommand.SetTabStop ] // Tab Set (HTS)
| ControlChars.RI -> state, [ OutputCommand.MoveUp (1, Some state.ScrollRegion) ] // Reverse Index (RI)
| ControlChars.CSI -> CtrlSeq("[") |> state.UpdateCtrlSeq, []
| ControlChars.OSC -> // Operating System Command (OSC)
OSCtrlSeq() |> state.UpdateOSCtrlSeq, []
| ControlChars.ST ->
if state.IsInOSCtrlSeq then
state.EndOSCtrlSeq
else
state, []
| ControlChars.BEL -> // Bell (BEL)
if state.IsInOSCtrlSeq then
state.EndOSCtrlSeq
else
state, [ OutputCommand.Bell ]
| _ when c >= 0x20uy && state.IsInCtrlSeq ->
state.ProcessNextCtrlSeqChar (getAsciiChar c)
| _ when c >= 0x20uy && c < 0x7Fuy ->
state, [ OutputCommand.Character (getAsciiChar c, state.ScrollRegionIfWrapping) ]
| _ -> state, []
let processOutputData (state : State, _) (data : byte[]) : (State * OutputCommand seq) =
let processed = Array.scan processOutputByte (state, []) data
fst processed.[processed.Length - 1],
processed |> Seq.map snd |> Seq.concat
let preamble =
Observable.ToObservable [
//[ TelnetCommands.IAC; TelnetCommands.WILL; TelnetOptions.WindowSize ] // NAWS
]
Observable.map processInput inputFromConsole |> Observable.merge preamble
|> Observable.filter (List.isEmpty >> not) |> Observable.map Array.ofList,
outputFromServer |> Observable.scan processOutputData (State(), Seq.empty) |> Observable.map snd
type LineBuffer<'Char>(originalWidth : int, maxLength : int, blankChar : 'Char) =
let mutable width = originalWidth
let mutable lines : 'Char[] List = List<'Char[]>()