pymud 0.21.1__py3-none-any.whl → 0.21.2a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pymud/__init__.py +16 -16
- pymud/__main__.py +3 -3
- pymud/decorators.py +234 -234
- pymud/dialogs.py +166 -166
- pymud/extras.py +930 -918
- pymud/i18n.py +62 -62
- pymud/lang/i18n_chs.py +226 -226
- pymud/lang/i18n_eng.py +850 -850
- pymud/logger.py +167 -167
- pymud/main.py +220 -220
- pymud/modules.py +285 -285
- pymud/objects.py +1032 -1032
- pymud/pkuxkx.py +280 -280
- pymud/protocol.py +1010 -1010
- pymud/pymud.py +1280 -1286
- pymud/session.py +3584 -3578
- pymud/settings.py +196 -196
- {pymud-0.21.1.dist-info → pymud-0.21.2a1.dist-info}/METADATA +477 -476
- pymud-0.21.2a1.dist-info/RECORD +23 -0
- {pymud-0.21.1.dist-info → pymud-0.21.2a1.dist-info}/WHEEL +1 -1
- {pymud-0.21.1.dist-info → pymud-0.21.2a1.dist-info}/licenses/LICENSE.txt +674 -674
- pymud-0.21.1.dist-info/RECORD +0 -23
- {pymud-0.21.1.dist-info → pymud-0.21.2a1.dist-info}/entry_points.txt +0 -0
- {pymud-0.21.1.dist-info → pymud-0.21.2a1.dist-info}/top_level.txt +0 -0
pymud/extras.py
CHANGED
@@ -1,919 +1,931 @@
|
|
1
|
-
# External Libraries
|
2
|
-
from unicodedata import east_asian_width
|
3
|
-
from wcwidth import wcwidth
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
from
|
8
|
-
from prompt_toolkit
|
9
|
-
from prompt_toolkit.
|
10
|
-
from prompt_toolkit.
|
11
|
-
from prompt_toolkit.formatted_text
|
12
|
-
from prompt_toolkit.
|
13
|
-
from prompt_toolkit.
|
14
|
-
from prompt_toolkit.
|
15
|
-
from prompt_toolkit.
|
16
|
-
from prompt_toolkit.
|
17
|
-
from prompt_toolkit.
|
18
|
-
from prompt_toolkit.
|
19
|
-
from prompt_toolkit.
|
20
|
-
from prompt_toolkit.
|
21
|
-
from prompt_toolkit.
|
22
|
-
|
23
|
-
from prompt_toolkit.
|
24
|
-
|
25
|
-
|
26
|
-
from prompt_toolkit.
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
from prompt_toolkit.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
from prompt_toolkit.
|
50
|
-
from prompt_toolkit.
|
51
|
-
|
52
|
-
from prompt_toolkit.
|
53
|
-
from prompt_toolkit.
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
self
|
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
|
-
line
|
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
|
-
if
|
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
|
-
if
|
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
|
-
if
|
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
|
-
|
1
|
+
# External Libraries
|
2
|
+
from unicodedata import east_asian_width
|
3
|
+
from wcwidth import wcwidth
|
4
|
+
from dataclasses import dataclass
|
5
|
+
import time, re, logging
|
6
|
+
|
7
|
+
from typing import Iterable, NamedTuple, Optional, Union, Tuple
|
8
|
+
from prompt_toolkit import ANSI
|
9
|
+
from prompt_toolkit.application import get_app
|
10
|
+
from prompt_toolkit.buffer import Buffer
|
11
|
+
from prompt_toolkit.formatted_text import to_formatted_text, fragment_list_to_text
|
12
|
+
from prompt_toolkit.formatted_text.base import OneStyleAndTextTuple
|
13
|
+
from prompt_toolkit.layout.controls import UIContent, UIControl
|
14
|
+
from prompt_toolkit.layout.processors import Processor, Transformation
|
15
|
+
from prompt_toolkit.application.current import get_app
|
16
|
+
from prompt_toolkit.buffer import Buffer
|
17
|
+
from prompt_toolkit.document import Document
|
18
|
+
from prompt_toolkit.data_structures import Point
|
19
|
+
from prompt_toolkit.layout.controls import UIContent, FormattedTextControl
|
20
|
+
from prompt_toolkit.lexers import Lexer
|
21
|
+
from prompt_toolkit.mouse_events import MouseButton, MouseEvent, MouseEventType
|
22
|
+
from prompt_toolkit.selection import SelectionType
|
23
|
+
from prompt_toolkit.buffer import Buffer, ValidationState
|
24
|
+
from prompt_toolkit.utils import Event
|
25
|
+
|
26
|
+
from prompt_toolkit.filters import (
|
27
|
+
FilterOrBool,
|
28
|
+
)
|
29
|
+
from prompt_toolkit.formatted_text import (
|
30
|
+
StyleAndTextTuples,
|
31
|
+
to_formatted_text,
|
32
|
+
)
|
33
|
+
from prompt_toolkit.formatted_text.utils import fragment_list_to_text
|
34
|
+
from prompt_toolkit.history import InMemoryHistory
|
35
|
+
from prompt_toolkit.key_binding.key_bindings import KeyBindingsBase
|
36
|
+
from prompt_toolkit.layout.containers import (
|
37
|
+
Window,
|
38
|
+
WindowAlign,
|
39
|
+
)
|
40
|
+
from prompt_toolkit.layout.controls import (
|
41
|
+
|
42
|
+
FormattedTextControl,
|
43
|
+
)
|
44
|
+
from prompt_toolkit.layout.processors import (
|
45
|
+
Processor,
|
46
|
+
TransformationInput,
|
47
|
+
Transformation
|
48
|
+
)
|
49
|
+
from prompt_toolkit.lexers import Lexer
|
50
|
+
from prompt_toolkit.mouse_events import MouseEvent, MouseEventType
|
51
|
+
from prompt_toolkit.utils import get_cwidth
|
52
|
+
from prompt_toolkit.widgets import Button, MenuContainer, MenuItem
|
53
|
+
from prompt_toolkit.widgets.base import Border
|
54
|
+
|
55
|
+
from prompt_toolkit.layout.screen import _CHAR_CACHE, Screen, WritePosition
|
56
|
+
from prompt_toolkit.layout.utils import explode_text_fragments
|
57
|
+
from prompt_toolkit.formatted_text.utils import (
|
58
|
+
fragment_list_to_text,
|
59
|
+
fragment_list_width,
|
60
|
+
)
|
61
|
+
|
62
|
+
from .settings import Settings
|
63
|
+
|
64
|
+
class VSplitWindow(Window):
|
65
|
+
"修改的分块窗口,向上翻页时,下半部保持最后数据不变"
|
66
|
+
|
67
|
+
def _copy_body(
|
68
|
+
self,
|
69
|
+
ui_content: UIContent,
|
70
|
+
new_screen: Screen,
|
71
|
+
write_position: WritePosition,
|
72
|
+
move_x: int,
|
73
|
+
width: int,
|
74
|
+
vertical_scroll: int = 0,
|
75
|
+
horizontal_scroll: int = 0,
|
76
|
+
wrap_lines: bool = False,
|
77
|
+
highlight_lines: bool = False,
|
78
|
+
vertical_scroll_2: int = 0,
|
79
|
+
always_hide_cursor: bool = False,
|
80
|
+
has_focus: bool = False,
|
81
|
+
align: WindowAlign = WindowAlign.LEFT,
|
82
|
+
get_line_prefix = None,
|
83
|
+
isNotMargin = True,
|
84
|
+
):
|
85
|
+
"""
|
86
|
+
Copy the UIContent into the output screen.
|
87
|
+
Return (visible_line_to_row_col, rowcol_to_yx) tuple.
|
88
|
+
|
89
|
+
:param get_line_prefix: None or a callable that takes a line number
|
90
|
+
(int) and a wrap_count (int) and returns formatted text.
|
91
|
+
"""
|
92
|
+
xpos = write_position.xpos + move_x
|
93
|
+
ypos = write_position.ypos
|
94
|
+
line_count = ui_content.line_count
|
95
|
+
new_buffer = new_screen.data_buffer
|
96
|
+
empty_char = _CHAR_CACHE["", ""]
|
97
|
+
|
98
|
+
# Map visible line number to (row, col) of input.
|
99
|
+
# 'col' will always be zero if line wrapping is off.
|
100
|
+
visible_line_to_row_col: dict[int, tuple[int, int]] = {}
|
101
|
+
|
102
|
+
# Maps (row, col) from the input to (y, x) screen coordinates.
|
103
|
+
rowcol_to_yx: dict[tuple[int, int], tuple[int, int]] = {}
|
104
|
+
|
105
|
+
def copy_line(
|
106
|
+
line: StyleAndTextTuples,
|
107
|
+
lineno: int,
|
108
|
+
x: int,
|
109
|
+
y: int,
|
110
|
+
is_input: bool = False,
|
111
|
+
):
|
112
|
+
"""
|
113
|
+
Copy over a single line to the output screen. This can wrap over
|
114
|
+
multiple lines in the output. It will call the prefix (prompt)
|
115
|
+
function before every line.
|
116
|
+
"""
|
117
|
+
if is_input:
|
118
|
+
current_rowcol_to_yx = rowcol_to_yx
|
119
|
+
else:
|
120
|
+
current_rowcol_to_yx = {} # Throwaway dictionary.
|
121
|
+
|
122
|
+
# Draw line prefix.
|
123
|
+
if is_input and get_line_prefix:
|
124
|
+
prompt = to_formatted_text(get_line_prefix(lineno, 0))
|
125
|
+
x, y = copy_line(prompt, lineno, x, y, is_input=False)
|
126
|
+
|
127
|
+
# Scroll horizontally.
|
128
|
+
skipped = 0 # Characters skipped because of horizontal scrolling.
|
129
|
+
if horizontal_scroll and is_input:
|
130
|
+
h_scroll = horizontal_scroll
|
131
|
+
line = explode_text_fragments(line)
|
132
|
+
while h_scroll > 0 and line:
|
133
|
+
h_scroll -= get_cwidth(line[0][1])
|
134
|
+
skipped += 1
|
135
|
+
del line[:1] # Remove first character.
|
136
|
+
|
137
|
+
x -= h_scroll # When scrolling over double width character,
|
138
|
+
# this can end up being negative.
|
139
|
+
|
140
|
+
# Align this line. (Note that this doesn't work well when we use
|
141
|
+
# get_line_prefix and that function returns variable width prefixes.)
|
142
|
+
if align == WindowAlign.CENTER:
|
143
|
+
line_width = fragment_list_width(line)
|
144
|
+
if line_width < width:
|
145
|
+
x += (width - line_width) // 2
|
146
|
+
elif align == WindowAlign.RIGHT:
|
147
|
+
line_width = fragment_list_width(line)
|
148
|
+
if line_width < width:
|
149
|
+
x += width - line_width
|
150
|
+
|
151
|
+
col = 0
|
152
|
+
wrap_count = 0
|
153
|
+
for style, text, *_ in line:
|
154
|
+
new_buffer_row = new_buffer[y + ypos]
|
155
|
+
|
156
|
+
# Remember raw VT escape sequences. (E.g. FinalTerm's
|
157
|
+
# escape sequences.)
|
158
|
+
if "[ZeroWidthEscape]" in style:
|
159
|
+
new_screen.zero_width_escapes[y + ypos][x + xpos] += text
|
160
|
+
continue
|
161
|
+
|
162
|
+
for c in text:
|
163
|
+
char = _CHAR_CACHE[c, style]
|
164
|
+
char_width = char.width
|
165
|
+
|
166
|
+
# Wrap when the line width is exceeded.
|
167
|
+
if wrap_lines and x + char_width > width:
|
168
|
+
visible_line_to_row_col[y + 1] = (
|
169
|
+
lineno,
|
170
|
+
visible_line_to_row_col[y][1] + x,
|
171
|
+
)
|
172
|
+
y += 1
|
173
|
+
wrap_count += 1
|
174
|
+
x = 0
|
175
|
+
|
176
|
+
# Insert line prefix (continuation prompt).
|
177
|
+
if is_input and get_line_prefix:
|
178
|
+
prompt = to_formatted_text(
|
179
|
+
get_line_prefix(lineno, wrap_count)
|
180
|
+
)
|
181
|
+
x, y = copy_line(prompt, lineno, x, y, is_input=False)
|
182
|
+
|
183
|
+
new_buffer_row = new_buffer[y + ypos]
|
184
|
+
|
185
|
+
if y >= write_position.height:
|
186
|
+
return x, y # Break out of all for loops.
|
187
|
+
|
188
|
+
# Set character in screen and shift 'x'.
|
189
|
+
if x >= 0 and y >= 0 and x < width:
|
190
|
+
new_buffer_row[x + xpos] = char
|
191
|
+
|
192
|
+
# When we print a multi width character, make sure
|
193
|
+
# to erase the neighbours positions in the screen.
|
194
|
+
# (The empty string if different from everything,
|
195
|
+
# so next redraw this cell will repaint anyway.)
|
196
|
+
if char_width > 1:
|
197
|
+
for i in range(1, char_width):
|
198
|
+
new_buffer_row[x + xpos + i] = empty_char
|
199
|
+
|
200
|
+
# If this is a zero width characters, then it's
|
201
|
+
# probably part of a decomposed unicode character.
|
202
|
+
# See: https://en.wikipedia.org/wiki/Unicode_equivalence
|
203
|
+
# Merge it in the previous cell.
|
204
|
+
elif char_width == 0:
|
205
|
+
# Handle all character widths. If the previous
|
206
|
+
# character is a multiwidth character, then
|
207
|
+
# merge it two positions back.
|
208
|
+
for pw in [2, 1]: # Previous character width.
|
209
|
+
if (
|
210
|
+
x - pw >= 0
|
211
|
+
and new_buffer_row[x + xpos - pw].width == pw
|
212
|
+
):
|
213
|
+
prev_char = new_buffer_row[x + xpos - pw]
|
214
|
+
char2 = _CHAR_CACHE[
|
215
|
+
prev_char.char + c, prev_char.style
|
216
|
+
]
|
217
|
+
new_buffer_row[x + xpos - pw] = char2
|
218
|
+
|
219
|
+
# Keep track of write position for each character.
|
220
|
+
current_rowcol_to_yx[lineno, col + skipped] = (
|
221
|
+
y + ypos,
|
222
|
+
x + xpos,
|
223
|
+
)
|
224
|
+
|
225
|
+
col += 1
|
226
|
+
x += char_width
|
227
|
+
return x, y
|
228
|
+
|
229
|
+
# Copy content.
|
230
|
+
def copy() -> int:
|
231
|
+
y = -vertical_scroll_2
|
232
|
+
lineno = vertical_scroll
|
233
|
+
|
234
|
+
total = write_position.height
|
235
|
+
upper = (total - 1) // 2
|
236
|
+
below = total - upper - 1
|
237
|
+
|
238
|
+
#if isNotMargin:
|
239
|
+
if isinstance(self.content, SessionBufferControl):
|
240
|
+
b = self.content.buffer
|
241
|
+
if not b:
|
242
|
+
return y
|
243
|
+
|
244
|
+
line_count = b.lineCount
|
245
|
+
start_lineno = b.start_lineno
|
246
|
+
if start_lineno < 0:
|
247
|
+
# no split window
|
248
|
+
if line_count < total:
|
249
|
+
# 内容行数小于屏幕行数
|
250
|
+
lineno = 0
|
251
|
+
|
252
|
+
while y < total and lineno < line_count:
|
253
|
+
# Take the next line and copy it in the real screen.
|
254
|
+
line = ui_content.get_line(lineno)
|
255
|
+
visible_line_to_row_col[y] = (lineno, horizontal_scroll)
|
256
|
+
x = 0
|
257
|
+
x, y = copy_line(line, lineno, x, y, is_input=True)
|
258
|
+
lineno += 1
|
259
|
+
y += 1
|
260
|
+
|
261
|
+
else:
|
262
|
+
# 若内容行数大于屏幕行数,则倒序复制,确保即使有自动折行时,最后一行也保持在屏幕最底部
|
263
|
+
|
264
|
+
y = total
|
265
|
+
lineno = line_count
|
266
|
+
|
267
|
+
while y >= 0 and lineno >= 0:
|
268
|
+
lineno -= 1
|
269
|
+
# Take the next line and copy it in the real screen.
|
270
|
+
display_lines = ui_content.get_height_for_line(lineno, width, None)
|
271
|
+
y -= display_lines
|
272
|
+
line = ui_content.get_line(lineno)
|
273
|
+
visible_line_to_row_col[y] = (lineno, horizontal_scroll)
|
274
|
+
copy_line(line, lineno, 0, y, is_input=True)
|
275
|
+
|
276
|
+
|
277
|
+
else:
|
278
|
+
# 有split window
|
279
|
+
# 复制上半部分,正序复制,确保即使有自动折行时,第一行也保持在屏幕最顶部
|
280
|
+
lineno = start_lineno
|
281
|
+
while y < upper and lineno < line_count:
|
282
|
+
line = ui_content.get_line(lineno)
|
283
|
+
visible_line_to_row_col[y] = (lineno, horizontal_scroll)
|
284
|
+
x = 0
|
285
|
+
x, y = copy_line(line, lineno, x, y, is_input=True)
|
286
|
+
lineno += 1
|
287
|
+
y += 1
|
288
|
+
|
289
|
+
# x = 0
|
290
|
+
# x, y = copy_line([("","-"*width)], lineno, x, y, is_input=False)
|
291
|
+
# y += 1
|
292
|
+
|
293
|
+
# 复制下半部分,倒序复制,确保即使有自动折行时,最后一行也保持在屏幕最底部
|
294
|
+
y = total
|
295
|
+
lineno = line_count
|
296
|
+
|
297
|
+
while y >= below and lineno >= 0:
|
298
|
+
lineno -= 1
|
299
|
+
# Take the next line and copy it in the real screen.
|
300
|
+
display_lines = ui_content.get_height_for_line(lineno, width, None)
|
301
|
+
y -= display_lines
|
302
|
+
line = ui_content.get_line(lineno)
|
303
|
+
visible_line_to_row_col[y] = (lineno, horizontal_scroll)
|
304
|
+
copy_line(line, lineno, 0, y, is_input=True)
|
305
|
+
|
306
|
+
# 最后复制分割线,若上下有由于折行额外占用的内容,都用分割线给覆盖掉
|
307
|
+
copy_line([("","-"*width)], -1, 0, upper + 1, is_input=False)
|
308
|
+
|
309
|
+
return y
|
310
|
+
|
311
|
+
copy()
|
312
|
+
|
313
|
+
def cursor_pos_to_screen_pos(row: int, col: int) -> Point:
|
314
|
+
"Translate row/col from UIContent to real Screen coordinates."
|
315
|
+
try:
|
316
|
+
y, x = rowcol_to_yx[row, col]
|
317
|
+
except KeyError:
|
318
|
+
# Normally this should never happen. (It is a bug, if it happens.)
|
319
|
+
# But to be sure, return (0, 0)
|
320
|
+
return Point(x=0, y=0)
|
321
|
+
|
322
|
+
# raise ValueError(
|
323
|
+
# 'Invalid position. row=%r col=%r, vertical_scroll=%r, '
|
324
|
+
# 'horizontal_scroll=%r, height=%r' %
|
325
|
+
# (row, col, vertical_scroll, horizontal_scroll, write_position.height))
|
326
|
+
else:
|
327
|
+
return Point(x=x, y=y)
|
328
|
+
|
329
|
+
# Set cursor and menu positions.
|
330
|
+
if ui_content.cursor_position:
|
331
|
+
screen_cursor_position = cursor_pos_to_screen_pos(
|
332
|
+
ui_content.cursor_position.y, ui_content.cursor_position.x
|
333
|
+
)
|
334
|
+
|
335
|
+
if has_focus:
|
336
|
+
new_screen.set_cursor_position(self, screen_cursor_position)
|
337
|
+
|
338
|
+
if always_hide_cursor:
|
339
|
+
new_screen.show_cursor = False
|
340
|
+
else:
|
341
|
+
new_screen.show_cursor = ui_content.show_cursor
|
342
|
+
|
343
|
+
self._highlight_digraph(new_screen)
|
344
|
+
|
345
|
+
if highlight_lines:
|
346
|
+
self._highlight_cursorlines(
|
347
|
+
new_screen,
|
348
|
+
screen_cursor_position,
|
349
|
+
xpos,
|
350
|
+
ypos,
|
351
|
+
width,
|
352
|
+
write_position.height,
|
353
|
+
)
|
354
|
+
|
355
|
+
# Draw input characters from the input processor queue.
|
356
|
+
if has_focus and ui_content.cursor_position:
|
357
|
+
self._show_key_processor_key_buffer(new_screen)
|
358
|
+
|
359
|
+
# Set menu position.
|
360
|
+
if ui_content.menu_position:
|
361
|
+
new_screen.set_menu_position(
|
362
|
+
self,
|
363
|
+
cursor_pos_to_screen_pos(
|
364
|
+
ui_content.menu_position.y, ui_content.menu_position.x
|
365
|
+
),
|
366
|
+
)
|
367
|
+
|
368
|
+
# Update output screen height.
|
369
|
+
new_screen.height = max(new_screen.height, ypos + write_position.height)
|
370
|
+
|
371
|
+
return visible_line_to_row_col, rowcol_to_yx
|
372
|
+
|
373
|
+
def _scroll_down(self) -> None:
|
374
|
+
"向下滚屏,处理屏幕分隔"
|
375
|
+
info = self.render_info
|
376
|
+
|
377
|
+
if info is None:
|
378
|
+
return
|
379
|
+
|
380
|
+
if isinstance(self.content, SessionBufferControl):
|
381
|
+
b = self.content.buffer
|
382
|
+
if not b:
|
383
|
+
return
|
384
|
+
start_lineno = b.start_lineno
|
385
|
+
if (start_lineno >= 0) and (start_lineno < b.lineCount - len(info.displayed_lines)):
|
386
|
+
b.start_lineno = b.start_lineno + 1
|
387
|
+
else:
|
388
|
+
b.start_lineno = -1
|
389
|
+
|
390
|
+
def _scroll_up(self) -> None:
|
391
|
+
"向上滚屏,处理屏幕分隔"
|
392
|
+
info = self.render_info
|
393
|
+
|
394
|
+
if info is None:
|
395
|
+
return
|
396
|
+
|
397
|
+
if isinstance(self.content, SessionBufferControl):
|
398
|
+
b = self.content.buffer
|
399
|
+
if not b:
|
400
|
+
return
|
401
|
+
start_lineno = b.start_lineno
|
402
|
+
if start_lineno > 0:
|
403
|
+
b.start_lineno = b.start_lineno - 1
|
404
|
+
|
405
|
+
elif start_lineno == 0:
|
406
|
+
b.start_lineno = 0
|
407
|
+
|
408
|
+
else:
|
409
|
+
b.start_lineno = b.lineCount - len(info.displayed_lines) - 1
|
410
|
+
|
411
|
+
|
412
|
+
class EasternButton(Button):
|
413
|
+
"解决增加中文等东亚全宽字符后不对齐问题"
|
414
|
+
|
415
|
+
def _get_text_fragments(self) -> StyleAndTextTuples:
|
416
|
+
# 主要改动在这里
|
417
|
+
width = self.width - (
|
418
|
+
get_cwidth(self.left_symbol) + get_cwidth(self.right_symbol)
|
419
|
+
) - (get_cwidth(self.text) - len(self.text))
|
420
|
+
|
421
|
+
|
422
|
+
text = (f"{{:^{width}}}").format(self.text)
|
423
|
+
|
424
|
+
def handler(mouse_event: MouseEvent) -> None:
|
425
|
+
if (
|
426
|
+
self.handler is not None
|
427
|
+
and mouse_event.event_type == MouseEventType.MOUSE_UP
|
428
|
+
):
|
429
|
+
self.handler()
|
430
|
+
|
431
|
+
return [
|
432
|
+
("class:button.arrow", self.left_symbol, handler),
|
433
|
+
#("[SetCursorPosition]", ""),
|
434
|
+
("class:button.text", text, handler),
|
435
|
+
("class:button.arrow", self.right_symbol, handler),
|
436
|
+
]
|
437
|
+
|
438
|
+
class EasternMenuContainer(MenuContainer):
|
439
|
+
"解决增加中文等东亚全宽字符后不对齐问题"
|
440
|
+
|
441
|
+
def _submenu(self, level: int = 0) -> Window:
|
442
|
+
def get_text_fragments() -> StyleAndTextTuples:
|
443
|
+
result: StyleAndTextTuples = []
|
444
|
+
if level < len(self.selected_menu):
|
445
|
+
menu = self._get_menu(level)
|
446
|
+
if menu.children:
|
447
|
+
result.append(("class:menu", Border.TOP_LEFT))
|
448
|
+
result.append(("class:menu", Border.HORIZONTAL * (menu.width + 4)))
|
449
|
+
result.append(("class:menu", Border.TOP_RIGHT))
|
450
|
+
result.append(("", "\n"))
|
451
|
+
try:
|
452
|
+
selected_item = self.selected_menu[level + 1]
|
453
|
+
except IndexError:
|
454
|
+
selected_item = -1
|
455
|
+
|
456
|
+
def one_item(
|
457
|
+
i: int, item: MenuItem
|
458
|
+
) -> Iterable[OneStyleAndTextTuple]:
|
459
|
+
def mouse_handler(mouse_event: MouseEvent) -> None:
|
460
|
+
if item.disabled:
|
461
|
+
# The arrow keys can't interact with menu items that are disabled.
|
462
|
+
# The mouse shouldn't be able to either.
|
463
|
+
return
|
464
|
+
hover = mouse_event.event_type == MouseEventType.MOUSE_MOVE
|
465
|
+
if (
|
466
|
+
mouse_event.event_type == MouseEventType.MOUSE_UP
|
467
|
+
or hover
|
468
|
+
):
|
469
|
+
app = get_app()
|
470
|
+
if not hover and item.handler:
|
471
|
+
app.layout.focus_last()
|
472
|
+
item.handler()
|
473
|
+
else:
|
474
|
+
self.selected_menu = self.selected_menu[
|
475
|
+
: level + 1
|
476
|
+
] + [i]
|
477
|
+
|
478
|
+
if i == selected_item:
|
479
|
+
yield ("[SetCursorPosition]", "")
|
480
|
+
style = "class:menu-bar.selected-item"
|
481
|
+
else:
|
482
|
+
style = ""
|
483
|
+
|
484
|
+
yield ("class:menu", Border.VERTICAL)
|
485
|
+
if item.text == "-":
|
486
|
+
yield (
|
487
|
+
style + "class:menu-border",
|
488
|
+
f"{Border.HORIZONTAL * (menu.width + 3)}",
|
489
|
+
mouse_handler,
|
490
|
+
)
|
491
|
+
else:
|
492
|
+
# 主要改动在这里,其他地方都未更改.
|
493
|
+
adj_width = menu.width + 3 - (get_cwidth(item.text) - len(item.text))
|
494
|
+
yield (
|
495
|
+
style,
|
496
|
+
f" {item.text}".ljust(adj_width),
|
497
|
+
mouse_handler,
|
498
|
+
)
|
499
|
+
|
500
|
+
if item.children:
|
501
|
+
yield (style, ">", mouse_handler)
|
502
|
+
else:
|
503
|
+
yield (style, " ", mouse_handler)
|
504
|
+
|
505
|
+
if i == selected_item:
|
506
|
+
yield ("[SetMenuPosition]", "")
|
507
|
+
yield ("class:menu", Border.VERTICAL)
|
508
|
+
|
509
|
+
yield ("", "\n")
|
510
|
+
|
511
|
+
for i, item in enumerate(menu.children):
|
512
|
+
result.extend(one_item(i, item))
|
513
|
+
|
514
|
+
result.append(("class:menu", Border.BOTTOM_LEFT))
|
515
|
+
result.append(("class:menu", Border.HORIZONTAL * (menu.width + 4)))
|
516
|
+
result.append(("class:menu", Border.BOTTOM_RIGHT))
|
517
|
+
return result
|
518
|
+
|
519
|
+
return Window(FormattedTextControl(get_text_fragments), style="class:menu")
|
520
|
+
|
521
|
+
@dataclass
|
522
|
+
class SessionSelectionState:
|
523
|
+
start_row: int = -1
|
524
|
+
end_row: int = -1
|
525
|
+
start_col: int = -1
|
526
|
+
end_col: int = -1
|
527
|
+
def is_valid(self):
|
528
|
+
if self.start_row >= 0 and self.end_row >= 0 and self.start_col >= 0 and self.end_col >= 0:
|
529
|
+
if (self.start_row == self.end_row) and (self.start_col == self.end_col):
|
530
|
+
return False
|
531
|
+
elif self.start_row > self.end_row:
|
532
|
+
srow, scol = self.end_row, self.end_col
|
533
|
+
erow, ecol = self.start_row, self.start_col
|
534
|
+
self.start_row, self.end_row = srow, erow
|
535
|
+
self.start_col, self.end_col = scol, ecol
|
536
|
+
elif self.start_row == self.end_row and self.start_col > self.end_col:
|
537
|
+
scol, ecol = self.end_col, self.start_col
|
538
|
+
self.start_col, self.end_col = scol, ecol
|
539
|
+
|
540
|
+
return True
|
541
|
+
|
542
|
+
return False
|
543
|
+
|
544
|
+
@property
|
545
|
+
def rows(self):
|
546
|
+
if self.is_valid():
|
547
|
+
return self.end_row - self.start_row + 1
|
548
|
+
else:
|
549
|
+
return 0
|
550
|
+
|
551
|
+
|
552
|
+
class SessionBuffer:
|
553
|
+
def __init__(
|
554
|
+
self,
|
555
|
+
name,
|
556
|
+
newline = "\n",
|
557
|
+
max_buffered_lines = 10000,
|
558
|
+
) -> None:
|
559
|
+
|
560
|
+
self.name = name
|
561
|
+
self.lines = []
|
562
|
+
self.newline = newline
|
563
|
+
self.isnewline = True
|
564
|
+
self.max_buffered_lines = max_buffered_lines
|
565
|
+
self.selection = SessionSelectionState(-1, -1, -1, -1)
|
566
|
+
self.start_lineno = -1
|
567
|
+
|
568
|
+
def append(self, line: str):
|
569
|
+
"""
|
570
|
+
追加文本到缓冲区。
|
571
|
+
当文本以换行符结尾时,会自动添加到缓冲区。
|
572
|
+
当文本不以换行符结尾时,会自动添加到上一行。
|
573
|
+
"""
|
574
|
+
newline_after_append = False
|
575
|
+
if line.endswith(self.newline):
|
576
|
+
line = line.rstrip(self.newline)
|
577
|
+
newline_after_append = True
|
578
|
+
if not self.newline in line:
|
579
|
+
if self.isnewline:
|
580
|
+
self.lines.append(line)
|
581
|
+
else:
|
582
|
+
self.lines[-1] += line
|
583
|
+
|
584
|
+
else:
|
585
|
+
lines = line.split(self.newline)
|
586
|
+
if self.isnewline:
|
587
|
+
self.lines.extend(lines)
|
588
|
+
else:
|
589
|
+
self.lines[-1] += lines[0]
|
590
|
+
self.lines.extend(lines[1:])
|
591
|
+
|
592
|
+
self.isnewline = newline_after_append
|
593
|
+
|
594
|
+
## limit buffered lines
|
595
|
+
if len(self.lines) > self.max_buffered_lines:
|
596
|
+
diff = self.max_buffered_lines - len(self.lines)
|
597
|
+
del self.lines[:diff]
|
598
|
+
## adjust selection
|
599
|
+
if self.selection.start_row >= 0:
|
600
|
+
self.selection.start_row -= diff
|
601
|
+
self.selection.end_row -= diff
|
602
|
+
|
603
|
+
get_app().invalidate()
|
604
|
+
|
605
|
+
def clear(self):
|
606
|
+
self.lines.clear()
|
607
|
+
self.selection = SessionSelectionState(-1, -1, -1, -1)
|
608
|
+
|
609
|
+
get_app().invalidate()
|
610
|
+
|
611
|
+
def loadfile(self, filename, encoding = 'utf-8', errors = 'ignore'):
|
612
|
+
with open(filename, 'r', encoding = encoding, errors = errors) as fp:
|
613
|
+
lines = fp.readlines()
|
614
|
+
self.clear()
|
615
|
+
self.lines.extend(lines)
|
616
|
+
|
617
|
+
get_app().invalidate()
|
618
|
+
|
619
|
+
@property
|
620
|
+
def lineCount(self):
|
621
|
+
return len(self.lines)
|
622
|
+
|
623
|
+
def getLine(self, lineno):
|
624
|
+
if lineno < 0 or lineno >= len(self.lines):
|
625
|
+
return ""
|
626
|
+
return self.lines[lineno]
|
627
|
+
|
628
|
+
# 获取指定某行到某行的内容。当start未设置时,从首行开始。当end未设置时,到最后一行结束。
|
629
|
+
# 注意判断首位顺序逻辑,以及给定参数是否越界
|
630
|
+
def getLines(self, start = None, end = None):
|
631
|
+
if start is None:
|
632
|
+
start = 0
|
633
|
+
if end is None:
|
634
|
+
end = len(self.lines) - 1
|
635
|
+
if start < 0:
|
636
|
+
start = 0
|
637
|
+
if end >= len(self.lines):
|
638
|
+
end = len(self.lines) - 1
|
639
|
+
if start > end:
|
640
|
+
return []
|
641
|
+
return self.lines[start:end+1]
|
642
|
+
|
643
|
+
def selection_range_at_line(self, lineno):
|
644
|
+
if self.selection.is_valid():
|
645
|
+
if self.selection.rows > 1:
|
646
|
+
if lineno == self.selection.start_row:
|
647
|
+
return (self.selection.start_col, len(self.lines[lineno]) - 1)
|
648
|
+
elif lineno == self.selection.end_row:
|
649
|
+
return (0, self.selection.end_col)
|
650
|
+
elif lineno > self.selection.start_row and lineno < self.selection.end_row:
|
651
|
+
return (0, len(self.lines[lineno]) - 1)
|
652
|
+
|
653
|
+
elif self.selection.rows == 1:
|
654
|
+
if lineno == self.selection.start_row:
|
655
|
+
return (self.selection.start_col, self.selection.end_col)
|
656
|
+
|
657
|
+
return None
|
658
|
+
|
659
|
+
def exit_selection(self):
|
660
|
+
self.selection = SessionSelectionState(-1, -1, -1, -1)
|
661
|
+
|
662
|
+
def nosplit(self):
|
663
|
+
self.start_lineno = -1
|
664
|
+
get_app().invalidate()
|
665
|
+
|
666
|
+
class SessionBufferControl(UIControl):
|
667
|
+
def __init__(self, buffer: Optional[SessionBuffer]) -> None:
|
668
|
+
self.buffer = buffer
|
669
|
+
|
670
|
+
# 为MUD显示进行校正的处理,包括对齐校正,换行颜色校正等
|
671
|
+
self.FULL_BLOCKS = set("▂▃▅▆▇▄█")
|
672
|
+
self.SINGLE_LINES = set("┌└├┬┼┴╭╰─")
|
673
|
+
self.DOUBLE_LINES = set("╔╚╠╦╪╩═")
|
674
|
+
self.ALL_COLOR_REGX = re.compile(r"(?:\[[\d;]+m)+")
|
675
|
+
self.AVAI_COLOR_REGX = re.compile(r"(?:\[[\d;]+m)+(?!$)")
|
676
|
+
self._color_start = ""
|
677
|
+
self._color_correction = False
|
678
|
+
self._color_line_index = 0
|
679
|
+
|
680
|
+
self._last_click_timestamp = 0
|
681
|
+
|
682
|
+
def reset(self) -> None:
|
683
|
+
# Default reset. (Doesn't have to be implemented.)
|
684
|
+
pass
|
685
|
+
|
686
|
+
def preferred_width(self, max_available_width: int) -> int | None:
|
687
|
+
return None
|
688
|
+
|
689
|
+
def is_focusable(self) -> bool:
|
690
|
+
"""
|
691
|
+
Tell whether this user control is focusable.
|
692
|
+
"""
|
693
|
+
return False
|
694
|
+
|
695
|
+
|
696
|
+
def width_correction(self, line: str) -> str:
|
697
|
+
new_str = []
|
698
|
+
for ch in line:
|
699
|
+
new_str.append(ch)
|
700
|
+
if (east_asian_width(ch) in "FWA") and (wcwidth(ch) == 1):
|
701
|
+
if ch in self.FULL_BLOCKS:
|
702
|
+
new_str.append(ch)
|
703
|
+
elif ch in self.SINGLE_LINES:
|
704
|
+
new_str.append("─")
|
705
|
+
elif ch in self.DOUBLE_LINES:
|
706
|
+
new_str.append("═")
|
707
|
+
else:
|
708
|
+
new_str.append(' ')
|
709
|
+
|
710
|
+
return "".join(new_str)
|
711
|
+
|
712
|
+
def return_correction(self, line: str):
|
713
|
+
return line.replace("\r", "").replace("\x00", "")
|
714
|
+
|
715
|
+
def tab_correction(self, line: str):
|
716
|
+
return line.replace("\t", " " * Settings.client["tabstop"])
|
717
|
+
|
718
|
+
def line_correction(self, line: str):
|
719
|
+
# 处理\r符号(^M)
|
720
|
+
line = self.return_correction(line)
|
721
|
+
# 处理Tab(\r)符号(^I)
|
722
|
+
line = self.tab_correction(line)
|
723
|
+
|
724
|
+
# 美化(解决中文英文在Console中不对齐的问题)
|
725
|
+
if Settings.client["beautify"]:
|
726
|
+
line = self.width_correction(line)
|
727
|
+
|
728
|
+
return line
|
729
|
+
|
730
|
+
def create_content(self, width: int, height: int) -> UIContent:
|
731
|
+
"""
|
732
|
+
Generate the content for this user control.
|
733
|
+
|
734
|
+
Returns a :class:`.UIContent` instance.
|
735
|
+
"""
|
736
|
+
buffer = self.buffer
|
737
|
+
if not buffer:
|
738
|
+
return UIContent(
|
739
|
+
get_line = lambda i: [],
|
740
|
+
line_count = 0,
|
741
|
+
cursor_position = None
|
742
|
+
)
|
743
|
+
|
744
|
+
def get_line(i: int) -> StyleAndTextTuples:
|
745
|
+
line = buffer.getLine(i)
|
746
|
+
# 颜色校正
|
747
|
+
thislinecolors = len(self.AVAI_COLOR_REGX.findall(line))
|
748
|
+
if thislinecolors == 0:
|
749
|
+
lineno = i - 1
|
750
|
+
while lineno >= 0:
|
751
|
+
lastline = buffer.getLine(lineno)
|
752
|
+
allcolors = self.ALL_COLOR_REGX.findall(lastline)
|
753
|
+
|
754
|
+
if len(allcolors) == 0:
|
755
|
+
lineno = lineno - 1
|
756
|
+
|
757
|
+
elif len(allcolors) == 1:
|
758
|
+
colors = self.AVAI_COLOR_REGX.findall(lastline)
|
759
|
+
|
760
|
+
if len(colors) == 1:
|
761
|
+
line = f"{colors[0]}{line}"
|
762
|
+
break
|
763
|
+
|
764
|
+
else:
|
765
|
+
break
|
766
|
+
|
767
|
+
else:
|
768
|
+
break
|
769
|
+
|
770
|
+
|
771
|
+
# 其他校正
|
772
|
+
line = self.line_correction(line)
|
773
|
+
|
774
|
+
# 处理ANSI标记(生成FormmatedText)
|
775
|
+
fragments = to_formatted_text(ANSI(line))
|
776
|
+
|
777
|
+
# 选择内容标识
|
778
|
+
selected_fragment = " class:selected "
|
779
|
+
|
780
|
+
# In case of selection, highlight all matches.
|
781
|
+
selection_at_line = buffer.selection_range_at_line(i)
|
782
|
+
|
783
|
+
if selection_at_line:
|
784
|
+
from_, to = selection_at_line
|
785
|
+
# from_ = source_to_display(from_)
|
786
|
+
# to = source_to_display(to)
|
787
|
+
|
788
|
+
fragments = explode_text_fragments(fragments)
|
789
|
+
|
790
|
+
if from_ == 0 and to == 0 and len(fragments) == 0:
|
791
|
+
# When this is an empty line, insert a space in order to
|
792
|
+
# visualize the selection.
|
793
|
+
return [(selected_fragment, " ")]
|
794
|
+
else:
|
795
|
+
for i in range(from_, to):
|
796
|
+
if i < len(fragments):
|
797
|
+
old_fragment, old_text, *_ = fragments[i]
|
798
|
+
fragments[i] = (old_fragment + selected_fragment, old_text)
|
799
|
+
elif i == len(fragments):
|
800
|
+
fragments.append((selected_fragment, " "))
|
801
|
+
|
802
|
+
return fragments
|
803
|
+
|
804
|
+
content = UIContent(
|
805
|
+
get_line = get_line,
|
806
|
+
line_count = buffer.lineCount,
|
807
|
+
cursor_position = None
|
808
|
+
)
|
809
|
+
|
810
|
+
return content
|
811
|
+
|
812
|
+
def mouse_handler(self, mouse_event: MouseEvent):
|
813
|
+
"""
|
814
|
+
Handle mouse events.
|
815
|
+
|
816
|
+
When `NotImplemented` is returned, it means that the given event is not
|
817
|
+
handled by the `UIControl` itself. The `Window` or key bindings can
|
818
|
+
decide to handle this event as scrolling or changing focus.
|
819
|
+
|
820
|
+
:param mouse_event: `MouseEvent` instance.
|
821
|
+
"""
|
822
|
+
"""
|
823
|
+
鼠标处理,修改内容包括:
|
824
|
+
1. 在CommandLine获得焦点的时候,鼠标对本Control也可以操作
|
825
|
+
2. 鼠标双击为选中行
|
826
|
+
"""
|
827
|
+
buffer = self.buffer
|
828
|
+
position = mouse_event.position
|
829
|
+
|
830
|
+
# Focus buffer when clicked.
|
831
|
+
cur_control = get_app().layout.current_control
|
832
|
+
cur_buffer = get_app().layout.current_buffer
|
833
|
+
# 这里是修改的内容
|
834
|
+
if (cur_control == self) or (cur_buffer and cur_buffer.name == "input"):
|
835
|
+
|
836
|
+
if buffer:
|
837
|
+
# Set the selection position.
|
838
|
+
if mouse_event.event_type == MouseEventType.MOUSE_DOWN:
|
839
|
+
buffer.exit_selection()
|
840
|
+
buffer.selection.start_row = position.y
|
841
|
+
buffer.selection.start_col = position.x
|
842
|
+
|
843
|
+
elif (
|
844
|
+
mouse_event.event_type == MouseEventType.MOUSE_MOVE
|
845
|
+
and mouse_event.button == MouseButton.LEFT
|
846
|
+
):
|
847
|
+
# Click and drag to highlight a selection
|
848
|
+
if buffer.selection.start_row >= 0:
|
849
|
+
buffer.selection.end_row = position.y
|
850
|
+
buffer.selection.end_col = position.x
|
851
|
+
|
852
|
+
elif mouse_event.event_type == MouseEventType.MOUSE_UP:
|
853
|
+
# When the cursor was moved to another place, select the text.
|
854
|
+
# (The >1 is actually a small but acceptable workaround for
|
855
|
+
# selecting text in Vi navigation mode. In navigation mode,
|
856
|
+
# the cursor can never be after the text, so the cursor
|
857
|
+
# will be repositioned automatically.)
|
858
|
+
|
859
|
+
if buffer.selection.start_row >= 0:
|
860
|
+
buffer.selection.end_row = position.y
|
861
|
+
buffer.selection.end_col = position.x
|
862
|
+
|
863
|
+
if buffer.selection.start_row == buffer.selection.end_row and buffer.selection.start_col == buffer.selection.end_col:
|
864
|
+
buffer.selection = SessionSelectionState(-1, -1, -1, -1)
|
865
|
+
|
866
|
+
|
867
|
+
# Select word around cursor on double click.
|
868
|
+
# Two MOUSE_UP events in a short timespan are considered a double click.
|
869
|
+
double_click = (
|
870
|
+
self._last_click_timestamp
|
871
|
+
and time.time() - self._last_click_timestamp < 0.3
|
872
|
+
)
|
873
|
+
self._last_click_timestamp = time.time()
|
874
|
+
|
875
|
+
if double_click:
|
876
|
+
buffer.selection.start_row = position.y
|
877
|
+
buffer.selection.start_col = 0
|
878
|
+
buffer.selection.end_row = position.y
|
879
|
+
buffer.selection.end_col = len(buffer.getLine(position.y))
|
880
|
+
|
881
|
+
get_app().layout.focus("input")
|
882
|
+
|
883
|
+
else:
|
884
|
+
# Don't handle scroll events here.
|
885
|
+
return NotImplemented
|
886
|
+
|
887
|
+
# Not focused, but focusing on click events.
|
888
|
+
else:
|
889
|
+
return NotImplemented
|
890
|
+
|
891
|
+
return None
|
892
|
+
|
893
|
+
class DotDict(dict):
|
894
|
+
"""
|
895
|
+
可以通过点.访问内部key-value对的字典。此类型继承自dict。
|
896
|
+
|
897
|
+
- 由于继承关系,此类型可以使用所有dict可以使用的方法
|
898
|
+
- 额外增加的点.访问方法使用示例如下
|
899
|
+
|
900
|
+
示例:
|
901
|
+
.. code:: Python
|
902
|
+
|
903
|
+
mydict = DotDict()
|
904
|
+
|
905
|
+
# 以下写内容访问等价
|
906
|
+
mydict["key1"] = "value1"
|
907
|
+
mydict.key1 = "value1"
|
908
|
+
|
909
|
+
# 以下读访问等价
|
910
|
+
val = mydict["key1"]
|
911
|
+
val = mydict.key1
|
912
|
+
"""
|
913
|
+
|
914
|
+
def __getattr__(self, __key):
|
915
|
+
if (not __key in self.__dict__) and (not __key.startswith("__")):
|
916
|
+
return self.__getitem__(__key)
|
917
|
+
|
918
|
+
def __setattr__(self, __name: str, __value):
|
919
|
+
if __name in self.__dict__:
|
920
|
+
object.__setattr__(self, __name, __value)
|
921
|
+
else:
|
922
|
+
self.__setitem__(__name, __value)
|
923
|
+
|
924
|
+
def __getstate__(self):
|
925
|
+
return self
|
926
|
+
|
927
|
+
def __setstate__(self, state):
|
928
|
+
self.update(state)
|
929
|
+
|
930
|
+
|
919
931
|
|