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