codis 1.1.1__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.
- codis-1.1.1.dist-info/METADATA +194 -0
- codis-1.1.1.dist-info/RECORD +329 -0
- codis-1.1.1.dist-info/WHEEL +5 -0
- codis-1.1.1.dist-info/entry_points.txt +5 -0
- codis-1.1.1.dist-info/licenses/LICENSE +21 -0
- codis-1.1.1.dist-info/top_level.txt +2 -0
- run.py +190 -0
- src/__init__.py +1 -0
- src/__main__.py +81 -0
- src/_app_build.py +1507 -0
- src/_app_editor.py +1274 -0
- src/_app_kui.py +825 -0
- src/_app_lsp.py +625 -0
- src/_app_recovery.py +189 -0
- src/_app_settings.py +1385 -0
- src/_app_tools.py +2471 -0
- src/_app_webpreview.py +126 -0
- src/_font_safety.py +127 -0
- src/_shells.py +172 -0
- src/app.py +1488 -0
- src/chat/__init__.py +1 -0
- src/chat/adapters/__init__.py +33 -0
- src/chat/adapters/base.py +209 -0
- src/chat/adapters/discord.py +190 -0
- src/chat/adapters/http.py +78 -0
- src/chat/adapters/irc.py +360 -0
- src/chat/adapters/matrix.py +219 -0
- src/chat/adapters/mattermost.py +178 -0
- src/chat/adapters/mqtt.py +165 -0
- src/chat/adapters/slack.py +204 -0
- src/chat/adapters/telegram.py +117 -0
- src/chat/adapters/ws.py +154 -0
- src/chat/adapters/xmpp.py +337 -0
- src/chat/history.py +76 -0
- src/chat/models.py +207 -0
- src/chat/probe.py +79 -0
- src/chat/session.py +446 -0
- src/chat/util.py +125 -0
- src/collab/__init__.py +1 -0
- src/collab/net.py +411 -0
- src/collab/ot.py +311 -0
- src/collab/session.py +141 -0
- src/collab/tk_adapter.py +134 -0
- src/config.py +335 -0
- src/constants.py +2552 -0
- src/dep_catalog.py +446 -0
- src/diff/__init__.py +1 -0
- src/diff/engine.py +247 -0
- src/dpi.py +30 -0
- src/editor/__init__.py +1 -0
- src/editor/_pane_lsp.py +2937 -0
- src/editor/_pane_utils.py +171 -0
- src/editor/autocomplete.py +607 -0
- src/editor/code_folding.py +836 -0
- src/editor/color_preview.py +382 -0
- src/editor/complexity_lens.py +281 -0
- src/editor/diff_pane.py +623 -0
- src/editor/file_watcher.py +144 -0
- src/editor/focus_block.py +236 -0
- src/editor/heatmap.py +141 -0
- src/editor/inlay_hints.py +366 -0
- src/editor/inline_blame.py +429 -0
- src/editor/kdocs.py +3376 -0
- src/editor/large_file.py +488 -0
- src/editor/ligatures.py +180 -0
- src/editor/line_numbers.py +598 -0
- src/editor/minimap.py +691 -0
- src/editor/multi_cursor.py +269 -0
- src/editor/pane.py +2082 -0
- src/editor/rhythm.py +206 -0
- src/editor/save_hooks.py +43 -0
- src/editor/smart_selection.py +425 -0
- src/editor/smart_tips.py +719 -0
- src/editor/snippets.py +411 -0
- src/editor/sticky_note_gutter.py +410 -0
- src/editor/sticky_scroll.py +385 -0
- src/editor/suggest.py +464 -0
- src/editor/text_transforms.py +310 -0
- src/editor/vim_mode.py +1074 -0
- src/events/__init__.py +84 -0
- src/events/bus.py +427 -0
- src/events/correlator.py +244 -0
- src/events/kinds.py +125 -0
- src/events/pipeline.py +264 -0
- src/events/stream.py +416 -0
- src/events/window.py +204 -0
- src/extcompat/__init__.py +19 -0
- src/extcompat/code/__init__.py +13 -0
- src/extcompat/code/consent.py +162 -0
- src/extcompat/code/host.py +353 -0
- src/extcompat/code/hosts/py_host.py +763 -0
- src/extcompat/code/locate.py +105 -0
- src/extcompat/code/manager.py +325 -0
- src/extcompat/code/permissions.py +154 -0
- src/extcompat/code/sandbox.py +60 -0
- src/extcompat/colors.py +131 -0
- src/extcompat/common.py +184 -0
- src/extcompat/cson.py +269 -0
- src/extcompat/formats/__init__.py +22 -0
- src/extcompat/formats/_tm.py +189 -0
- src/extcompat/formats/_util.py +31 -0
- src/extcompat/formats/base16.py +66 -0
- src/extcompat/formats/brackets.py +172 -0
- src/extcompat/formats/eclipse.py +193 -0
- src/extcompat/formats/emacs.py +361 -0
- src/extcompat/formats/geany.py +147 -0
- src/extcompat/formats/generic.py +184 -0
- src/extcompat/formats/gtksource.py +117 -0
- src/extcompat/formats/helix.py +91 -0
- src/extcompat/formats/jetbrains.py +343 -0
- src/extcompat/formats/kate.py +130 -0
- src/extcompat/formats/micro.py +131 -0
- src/extcompat/formats/nano.py +111 -0
- src/extcompat/formats/notepadpp.py +140 -0
- src/extcompat/formats/pulsar.py +165 -0
- src/extcompat/formats/pyides.py +163 -0
- src/extcompat/formats/sublime.py +340 -0
- src/extcompat/jsonc.py +65 -0
- src/extcompat/lessish.py +128 -0
- src/extcompat/manager.py +122 -0
- src/extcompat/market/__init__.py +63 -0
- src/extcompat/market/sources.py +211 -0
- src/extcompat/model.py +88 -0
- src/extcompat/net.py +228 -0
- src/extcompat/openvsx.py +63 -0
- src/extcompat/readers.py +362 -0
- src/extcompat/registry.py +73 -0
- src/extcompat/safexml.py +63 -0
- src/extcompat/sexp.py +187 -0
- src/extcompat/sources.py +105 -0
- src/extcompat/store.py +276 -0
- src/extcompat/textmate.py +214 -0
- src/extcompat/themes.py +487 -0
- src/extcompat/yamlite.py +294 -0
- src/fileio.py +94 -0
- src/fs_ops.py +106 -0
- src/i18n.py +233 -0
- src/ia/__init__.py +1 -0
- src/ia/actions.py +295 -0
- src/ia/approval_gate.py +672 -0
- src/ia/checkpoints.py +308 -0
- src/ia/context_builder.py +921 -0
- src/ia/dialdiff.py +408 -0
- src/ia/iaspace.py +2078 -0
- src/ia/pair.py +296 -0
- src/ia/providers.py +954 -0
- src/ia/session.py +335 -0
- src/ia/toolbox.py +2524 -0
- src/launcher_actions.py +204 -0
- src/lint/__init__.py +1 -0
- src/lint/manager.py +1058 -0
- src/locales/.translate_manifest.json +64170 -0
- src/locales/de.json +4936 -0
- src/locales/en.json +4936 -0
- src/locales/es.json +4936 -0
- src/locales/fr.json +4936 -0
- src/locales/it.json +4936 -0
- src/locales/ja.json +4936 -0
- src/locales/ko.json +4936 -0
- src/locales/nl.json +4936 -0
- src/locales/pl.json +4936 -0
- src/locales/pt-BR.json +4936 -0
- src/locales/ru.json +4936 -0
- src/locales/tr.json +4936 -0
- src/locales/uk.json +4936 -0
- src/locales/zh-CN.json +4936 -0
- src/lsp/__init__.py +26 -0
- src/lsp/client.py +2300 -0
- src/lsp/dap_client.py +480 -0
- src/lsp/edits.py +84 -0
- src/lsp/framing.py +73 -0
- src/lsp/protocol.py +931 -0
- src/main.py +260 -0
- src/memtrim.py +91 -0
- src/plugins/__init__.py +1 -0
- src/plugins/api.py +1761 -0
- src/plugins/loader.py +571 -0
- src/plugins/process_runner.py +519 -0
- src/plugins/registry.py +342 -0
- src/plugins/safety.py +367 -0
- src/plugins/startup.py +195 -0
- src/plugins/store.py +1178 -0
- src/proc_registry.py +278 -0
- src/pyexec.py +394 -0
- src/recovery.py +259 -0
- src/stall_trace.py +92 -0
- src/standalone.py +1070 -0
- src/startup_profile.py +35 -0
- src/syntax/__init__.py +1 -0
- src/syntax/highlighter.py +776 -0
- src/syntax/patterns.py +584 -0
- src/system_profile.py +430 -0
- src/terminal/backends.py +658 -0
- src/terminal/internal.py +346 -0
- src/terminal/keys.py +138 -0
- src/terminal/palette.py +67 -0
- src/terminal/session.py +283 -0
- src/terminal/shells.py +79 -0
- src/terminal/view.py +489 -0
- src/terminal/vt.py +870 -0
- src/textfmt.py +22 -0
- src/theme/__init__.py +1 -0
- src/theme/base.py +1281 -0
- src/theme/colors.py +49 -0
- src/theme/manager.py +602 -0
- src/theme/utils.py +312 -0
- src/toolbox.py +329 -0
- src/trash_core.py +531 -0
- src/ui/__init__.py +1 -0
- src/ui/_perf_utils.py +285 -0
- src/ui/_persistence_utils.py +93 -0
- src/ui/accessibility.py +843 -0
- src/ui/activity_bar.py +860 -0
- src/ui/app_embed.py +434 -0
- src/ui/archive_panel.py +608 -0
- src/ui/bookmarks.py +314 -0
- src/ui/breadcrumb.py +383 -0
- src/ui/calculator_panel.py +982 -0
- src/ui/calendar_panel.py +1218 -0
- src/ui/chat_panel.py +1271 -0
- src/ui/chat_profile_dialog.py +278 -0
- src/ui/chat_render.py +128 -0
- src/ui/clock_panel.py +810 -0
- src/ui/code_outline.py +364 -0
- src/ui/code_runner.py +729 -0
- src/ui/code_stats.py +546 -0
- src/ui/collab_panel.py +1009 -0
- src/ui/color_picker.py +547 -0
- src/ui/command_palette.py +452 -0
- src/ui/cursor_manager.py +750 -0
- src/ui/custom_keywords_panel.py +413 -0
- src/ui/debug_panel.py +1037 -0
- src/ui/dependency_graph.py +692 -0
- src/ui/diff_panel.py +381 -0
- src/ui/docs_panel.py +514 -0
- src/ui/doom_embed.py +349 -0
- src/ui/dropdown.py +321 -0
- src/ui/easter_eggs.py +979 -0
- src/ui/env_manager.py +666 -0
- src/ui/extcode_bridge.py +215 -0
- src/ui/extcode_dialogs.py +412 -0
- src/ui/extcompat_dialog.py +583 -0
- src/ui/file_timeline.py +319 -0
- src/ui/file_tree.py +1208 -0
- src/ui/find_in_files.py +1355 -0
- src/ui/flowchart_panel.py +727 -0
- src/ui/formatter_panel.py +490 -0
- src/ui/git_auth.py +763 -0
- src/ui/git_clone_dialog.py +487 -0
- src/ui/git_panel.py +1483 -0
- src/ui/grab_safe.py +84 -0
- src/ui/internal_python_dialog.py +98 -0
- src/ui/k_ui/__init__.py +1305 -0
- src/ui/k_ui/_bgrenderer.py +317 -0
- src/ui/k_ui/_colors.py +213 -0
- src/ui/k_ui/_dock.py +545 -0
- src/ui/k_ui/_easter.py +290 -0
- src/ui/k_ui/_engine.py +5448 -0
- src/ui/k_ui/_perf.py +333 -0
- src/ui/k_ui/_tkutil.py +7 -0
- src/ui/k_ui/_tooltip.py +365 -0
- src/ui/k_ui/_widgets.py +1204 -0
- src/ui/k_ui/_world.py +837 -0
- src/ui/key_remapper.py +856 -0
- src/ui/launcher.py +1252 -0
- src/ui/lazy_panel.py +73 -0
- src/ui/macro_recorder.py +610 -0
- src/ui/menu_bar.py +898 -0
- src/ui/output_panel.py +321 -0
- src/ui/paint_canvas.py +1000 -0
- src/ui/paint_engine.py +535 -0
- src/ui/paint_panel.py +888 -0
- src/ui/panel_detach.py +112 -0
- src/ui/performance_panel.py +861 -0
- src/ui/pip_manager.py +1758 -0
- src/ui/plugin_panel.py +1586 -0
- src/ui/pomodoro_panel.py +447 -0
- src/ui/problems.py +566 -0
- src/ui/process_manager.py +1657 -0
- src/ui/project_templates.py +775 -0
- src/ui/recovery_dialog.py +122 -0
- src/ui/regex_playground.py +304 -0
- src/ui/remote_adapters.py +1398 -0
- src/ui/remote_browser.py +1224 -0
- src/ui/remote_profile_dialog.py +342 -0
- src/ui/render_panel.py +1293 -0
- src/ui/rest_client.py +549 -0
- src/ui/scratchpad.py +548 -0
- src/ui/search_bar.py +1274 -0
- src/ui/settings_editor.py +3349 -0
- src/ui/settings_window.py +1554 -0
- src/ui/snippets_panel.py +597 -0
- src/ui/sql_studio.py +1815 -0
- src/ui/status_bar.py +438 -0
- src/ui/tab_bar.py +654 -0
- src/ui/task_manager.py +1773 -0
- src/ui/terminal.py +525 -0
- src/ui/theme_preview_panel.py +851 -0
- src/ui/thread_safe_tk.py +152 -0
- src/ui/time_tracker.py +574 -0
- src/ui/tkbind.py +43 -0
- src/ui/todo_panel.py +358 -0
- src/ui/toolbox_panels.py +658 -0
- src/ui/trash_manager.py +657 -0
- src/ui/ui_scale.py +294 -0
- src/ui/ux_enhancements.py +41 -0
- src/ui/web_preview.py +397 -0
- src/ui/webdev.py +630 -0
- src/ui/welcome.py +567 -0
- src/ui/window_embed.py +1500 -0
- src/ui/wizard.py +1550 -0
- src/ui/workspace_switcher.py +418 -0
- src/ui/workspace_trust.py +370 -0
- src/ui/zen_audio.py +354 -0
- src/ui/zen_media.py +2271 -0
- src/ui/zen_media_client.py +303 -0
- src/ui/zen_media_host.py +329 -0
- src/ui/zen_player.py +907 -0
- src/ui/zen_player_widgets.py +700 -0
- src/updater/__init__.py +11 -0
- src/updater/assets.py +145 -0
- src/updater/backup.py +101 -0
- src/updater/channel.py +221 -0
- src/updater/dialog.py +492 -0
- src/updater/integrity.py +111 -0
- src/updater/manager.py +727 -0
- src/updater/result.py +114 -0
- src/venv_runtime.py +531 -0
- src/workspace_state.py +25 -0
src/terminal/vt.py
ADDED
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
"""Emulador de terminal VT100/xterm em Python puro, sem dependência de Tkinter.
|
|
2
|
+
|
|
3
|
+
``VTScreen`` interpreta o fluxo de bytes (já decodificado como texto) de um
|
|
4
|
+
pseudo-terminal e mantém a grade de células, o cursor, os modos e as linhas
|
|
5
|
+
que saíram do topo da tela. A camada de interface só precisa ler as linhas
|
|
6
|
+
alteradas (``take_dirty``) e as roladas para o histórico (``take_scrolled``).
|
|
7
|
+
|
|
8
|
+
Suporte principal: movimento de cursor, apagamento, inserção e remoção de
|
|
9
|
+
linhas e caracteres, regiões de rolagem, tela alternativa, SGR com 16, 256 e
|
|
10
|
+
24 bits, caracteres largos, conjunto de linhas do DEC, modos de cursor, colagem
|
|
11
|
+
com colchetes e respostas a consultas (posição do cursor e atributos do
|
|
12
|
+
dispositivo).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
import unicodedata
|
|
19
|
+
from collections import deque
|
|
20
|
+
|
|
21
|
+
DEFAULT_COLOR = -1
|
|
22
|
+
|
|
23
|
+
FLAG_BOLD = 1
|
|
24
|
+
FLAG_DIM = 2
|
|
25
|
+
FLAG_ITALIC = 4
|
|
26
|
+
FLAG_UNDERLINE = 8
|
|
27
|
+
FLAG_REVERSE = 16
|
|
28
|
+
FLAG_HIDDEN = 32
|
|
29
|
+
FLAG_STRIKE = 64
|
|
30
|
+
|
|
31
|
+
Attr = tuple[int, int, int]
|
|
32
|
+
Run = tuple[str, int]
|
|
33
|
+
|
|
34
|
+
_PLAIN = re.compile(r"[^\x00-\x1f\x7f\x80-\x9f]+")
|
|
35
|
+
_CSI = re.compile(r"\x1b\[([0-?]*)([ -/]*)([@-~])")
|
|
36
|
+
_OSC = re.compile(r"\x1b\]([^\x07\x1b]*)(?:\x07|\x1b\\)")
|
|
37
|
+
_ESC_SIMPLE = re.compile(r"\x1b([ -/]*)([0-~])")
|
|
38
|
+
_CSI_PREFIX = re.compile(r"\x1b\[[0-?]*[ -/]*\Z")
|
|
39
|
+
_OSC_PREFIX = re.compile(r"\x1b\][^\x07\x1b]*\x1b?\Z")
|
|
40
|
+
_ESC_PREFIX = re.compile(r"\x1b[ -/]*\Z")
|
|
41
|
+
# Corpo de DCS/SOS/PM/APC em "loop desenrolado": corridas de não-ESC separadas por
|
|
42
|
+
# ESC que não inicia o terminador ST (ESC \). Cada ESC é literal, então não há
|
|
43
|
+
# ambiguidade entre iterações e o casamento é linear (sem backtracking exponencial).
|
|
44
|
+
_STRING_BODY = r"[^\x07\x1b]*(?:\x1b(?!\\)[^\x07\x1b]*)*"
|
|
45
|
+
_STRING_TERMINATED = re.compile(r"\x1b[PX^_]" + _STRING_BODY + r"(?:\x1b\\|\x07)")
|
|
46
|
+
_STRING_PREFIX = re.compile(r"\x1b[PX^_]" + _STRING_BODY + r"\Z")
|
|
47
|
+
|
|
48
|
+
_MAX_PENDING = 8192
|
|
49
|
+
_MAX_SCROLLED = 5000
|
|
50
|
+
|
|
51
|
+
_DEC_SPECIAL = {
|
|
52
|
+
"`": "\u25c6", "a": "\u2592", "f": "\u00b0", "g": "\u00b1", "j": "\u2518",
|
|
53
|
+
"k": "\u2510", "l": "\u250c", "m": "\u2514", "n": "\u253c", "o": "\u23ba",
|
|
54
|
+
"p": "\u23bb", "q": "\u2500", "r": "\u23bc", "s": "\u23bd", "t": "\u251c",
|
|
55
|
+
"u": "\u2524", "v": "\u2534", "w": "\u252c", "x": "\u2502", "y": "\u2264",
|
|
56
|
+
"z": "\u2265", "{": "\u03c0", "|": "\u2260", "}": "\u00a3", "~": "\u00b7",
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def char_width(ch: str) -> int:
|
|
61
|
+
"""Largura em colunas de ``ch``: 0 (combinante), 1 ou 2 (largo)."""
|
|
62
|
+
if ch < "\u0300":
|
|
63
|
+
return 1
|
|
64
|
+
if unicodedata.combining(ch):
|
|
65
|
+
return 0
|
|
66
|
+
return 2 if unicodedata.east_asian_width(ch) in ("W", "F") else 1
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class _Row:
|
|
70
|
+
"""Uma linha da grade: caracteres e ids de atributo por coluna."""
|
|
71
|
+
|
|
72
|
+
__slots__ = ("attrs", "chars")
|
|
73
|
+
|
|
74
|
+
def __init__(self, cols: int, blank_attr: int = 0) -> None:
|
|
75
|
+
self.chars: list[str] = [" "] * cols
|
|
76
|
+
self.attrs: list[int] = [blank_attr] * cols
|
|
77
|
+
|
|
78
|
+
def resize(self, cols: int) -> None:
|
|
79
|
+
n = len(self.chars)
|
|
80
|
+
if cols < n:
|
|
81
|
+
del self.chars[cols:]
|
|
82
|
+
del self.attrs[cols:]
|
|
83
|
+
elif cols > n:
|
|
84
|
+
self.chars.extend([" "] * (cols - n))
|
|
85
|
+
self.attrs.extend([0] * (cols - n))
|
|
86
|
+
|
|
87
|
+
def copy(self) -> _Row:
|
|
88
|
+
other = _Row(0)
|
|
89
|
+
other.chars = self.chars[:]
|
|
90
|
+
other.attrs = self.attrs[:]
|
|
91
|
+
return other
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class VTScreen:
|
|
95
|
+
"""Estado e interpretador de um terminal virtual."""
|
|
96
|
+
|
|
97
|
+
def __init__(self, rows: int = 24, cols: int = 80) -> None:
|
|
98
|
+
self.rows = max(1, rows)
|
|
99
|
+
self.cols = max(1, cols)
|
|
100
|
+
self.attr_table: list[Attr] = [(DEFAULT_COLOR, DEFAULT_COLOR, 0)]
|
|
101
|
+
self._attr_ids: dict[Attr, int] = {self.attr_table[0]: 0}
|
|
102
|
+
self.title = ""
|
|
103
|
+
self.cwd = ""
|
|
104
|
+
self.responses: list[bytes] = []
|
|
105
|
+
self._pending = ""
|
|
106
|
+
self._scrolled: deque[tuple[list[str], list[int]]] = deque(maxlen=_MAX_SCROLLED)
|
|
107
|
+
self._dirty: set[int] = set()
|
|
108
|
+
self.all_dirty = True
|
|
109
|
+
self._reset_state()
|
|
110
|
+
|
|
111
|
+
def _reset_state(self) -> None:
|
|
112
|
+
self.grid = [_Row(self.cols) for _ in range(self.rows)]
|
|
113
|
+
self._saved_main: tuple[list[_Row], int, int] | None = None
|
|
114
|
+
self.alt_screen = False
|
|
115
|
+
self.cx = 0
|
|
116
|
+
self.cy = 0
|
|
117
|
+
self._wrap_pending = False
|
|
118
|
+
self._pen: Attr = (DEFAULT_COLOR, DEFAULT_COLOR, 0)
|
|
119
|
+
self._pen_id = 0
|
|
120
|
+
self._saved_cursor = (0, 0, self._pen, 0, ("B", "B"))
|
|
121
|
+
self.top = 0
|
|
122
|
+
self.bottom = self.rows - 1
|
|
123
|
+
self.app_cursor = False
|
|
124
|
+
self.app_keypad = False
|
|
125
|
+
self.autowrap = True
|
|
126
|
+
self.cursor_visible = True
|
|
127
|
+
self.bracketed_paste = False
|
|
128
|
+
self.focus_events = False
|
|
129
|
+
self.mouse_mode = 0
|
|
130
|
+
self.mouse_sgr = False
|
|
131
|
+
self.insert_mode = False
|
|
132
|
+
self.origin_mode = False
|
|
133
|
+
self.tabs = set(range(0, self.cols, 8))
|
|
134
|
+
self._charsets = ["B", "B"]
|
|
135
|
+
self._gl = 0
|
|
136
|
+
self.all_dirty = True
|
|
137
|
+
|
|
138
|
+
def reset(self) -> None:
|
|
139
|
+
"""Reinicia a tela e os modos (equivalente a ``ESC c``)."""
|
|
140
|
+
self._reset_state()
|
|
141
|
+
|
|
142
|
+
def _attr_id(self, attr: Attr) -> int:
|
|
143
|
+
found = self._attr_ids.get(attr)
|
|
144
|
+
if found is None:
|
|
145
|
+
found = len(self.attr_table)
|
|
146
|
+
self.attr_table.append(attr)
|
|
147
|
+
self._attr_ids[attr] = found
|
|
148
|
+
return found
|
|
149
|
+
|
|
150
|
+
def take_dirty(self) -> tuple[bool, set[int]]:
|
|
151
|
+
"""Devolve ``(tudo_sujo, linhas_sujas)`` e zera o controle de alterações."""
|
|
152
|
+
full, rows = self.all_dirty, self._dirty
|
|
153
|
+
self.all_dirty = False
|
|
154
|
+
self._dirty = set()
|
|
155
|
+
return full, rows
|
|
156
|
+
|
|
157
|
+
def take_scrolled(self) -> list[tuple[list[str], list[int]]]:
|
|
158
|
+
"""Linhas que saíram pelo topo da tela principal desde a última chamada."""
|
|
159
|
+
out = list(self._scrolled)
|
|
160
|
+
self._scrolled.clear()
|
|
161
|
+
return out
|
|
162
|
+
|
|
163
|
+
def take_responses(self) -> bytes:
|
|
164
|
+
"""Bytes a enviar de volta ao programa (respostas a consultas)."""
|
|
165
|
+
out = b"".join(self.responses)
|
|
166
|
+
self.responses.clear()
|
|
167
|
+
return out
|
|
168
|
+
|
|
169
|
+
def row_runs(self, r: int, min_len: int = 0) -> list[Run]:
|
|
170
|
+
"""Linha ``r`` como trechos ``(texto, atributo)``, sem espaços finais padrão.
|
|
171
|
+
|
|
172
|
+
``min_len`` garante pelo menos essa quantidade de colunas (usado para
|
|
173
|
+
desenhar o cursor depois do fim do texto).
|
|
174
|
+
"""
|
|
175
|
+
row = self.grid[r]
|
|
176
|
+
chars, attrs = row.chars, row.attrs
|
|
177
|
+
if not any(attrs):
|
|
178
|
+
text = "".join(chars).rstrip(" ")
|
|
179
|
+
if len(text) < min_len:
|
|
180
|
+
text += " " * (min_len - len(text))
|
|
181
|
+
return [(text, 0)] if text else []
|
|
182
|
+
end = len(chars)
|
|
183
|
+
while end > min_len and chars[end - 1] == " " and attrs[end - 1] == 0:
|
|
184
|
+
end -= 1
|
|
185
|
+
runs: list[Run] = []
|
|
186
|
+
start = 0
|
|
187
|
+
while start < end:
|
|
188
|
+
attr = attrs[start]
|
|
189
|
+
stop = start + 1
|
|
190
|
+
while stop < end and attrs[stop] == attr:
|
|
191
|
+
stop += 1
|
|
192
|
+
text = "".join(chars[start:stop])
|
|
193
|
+
if text:
|
|
194
|
+
runs.append((text, attr))
|
|
195
|
+
start = stop
|
|
196
|
+
return runs
|
|
197
|
+
|
|
198
|
+
def text_col(self, r: int, col: int) -> int:
|
|
199
|
+
"""Índice de caractere (no texto da linha) correspondente à célula ``col``.
|
|
200
|
+
|
|
201
|
+
Células de continuação de caracteres largos não têm texto, e marcas
|
|
202
|
+
combinantes acrescentam caracteres, então os índices diferem das colunas.
|
|
203
|
+
"""
|
|
204
|
+
return sum(len(c) for c in self.grid[r].chars[:col])
|
|
205
|
+
|
|
206
|
+
def line_text(self, r: int) -> str:
|
|
207
|
+
"""Texto simples da linha ``r`` sem espaços à direita."""
|
|
208
|
+
return "".join(self.grid[r].chars).rstrip()
|
|
209
|
+
|
|
210
|
+
def text(self) -> str:
|
|
211
|
+
"""Conteúdo visível da tela, uma linha por fileira."""
|
|
212
|
+
return "\n".join(self.line_text(r) for r in range(self.rows))
|
|
213
|
+
|
|
214
|
+
@staticmethod
|
|
215
|
+
def runs_of(chars: list[str], attrs: list[int]) -> list[Run]:
|
|
216
|
+
"""Converte uma linha rolada (listas paralelas) em trechos."""
|
|
217
|
+
if not any(attrs):
|
|
218
|
+
text = "".join(chars).rstrip(" ")
|
|
219
|
+
return [(text, 0)] if text else []
|
|
220
|
+
end = len(chars)
|
|
221
|
+
while end > 0 and chars[end - 1] == " " and attrs[end - 1] == 0:
|
|
222
|
+
end -= 1
|
|
223
|
+
runs: list[Run] = []
|
|
224
|
+
start = 0
|
|
225
|
+
while start < end:
|
|
226
|
+
attr = attrs[start]
|
|
227
|
+
stop = start + 1
|
|
228
|
+
while stop < end and attrs[stop] == attr:
|
|
229
|
+
stop += 1
|
|
230
|
+
text = "".join(chars[start:stop])
|
|
231
|
+
if text:
|
|
232
|
+
runs.append((text, attr))
|
|
233
|
+
start = stop
|
|
234
|
+
return runs
|
|
235
|
+
|
|
236
|
+
def feed(self, data: str) -> None:
|
|
237
|
+
"""Interpreta ``data``; sequências incompletas ficam para a próxima chamada."""
|
|
238
|
+
if self._pending:
|
|
239
|
+
data = self._pending + data
|
|
240
|
+
self._pending = ""
|
|
241
|
+
i, n = 0, len(data)
|
|
242
|
+
plain_match = _PLAIN.match
|
|
243
|
+
while i < n:
|
|
244
|
+
ch = data[i]
|
|
245
|
+
if ch >= " " and ch != "\x7f" and not ("\x80" <= ch <= "\x9f"):
|
|
246
|
+
m = plain_match(data, i)
|
|
247
|
+
self._put_text(m.group(0))
|
|
248
|
+
i = m.end()
|
|
249
|
+
continue
|
|
250
|
+
if ch == "\x1b":
|
|
251
|
+
consumed = self._escape(data, i)
|
|
252
|
+
if consumed < 0:
|
|
253
|
+
rest = data[i:]
|
|
254
|
+
if len(rest) <= _MAX_PENDING:
|
|
255
|
+
self._pending = rest
|
|
256
|
+
return
|
|
257
|
+
i = consumed
|
|
258
|
+
continue
|
|
259
|
+
self._control(ch)
|
|
260
|
+
i += 1
|
|
261
|
+
|
|
262
|
+
def _control(self, ch: str) -> None:
|
|
263
|
+
if ch == "\n" or ch == "\x0b" or ch == "\x0c":
|
|
264
|
+
self._index()
|
|
265
|
+
elif ch == "\r":
|
|
266
|
+
self.cx = 0
|
|
267
|
+
self._wrap_pending = False
|
|
268
|
+
elif ch == "\x08":
|
|
269
|
+
if self.cx > 0:
|
|
270
|
+
self.cx -= 1
|
|
271
|
+
self._wrap_pending = False
|
|
272
|
+
elif ch == "\t":
|
|
273
|
+
self._tab(1)
|
|
274
|
+
elif ch == "\x0e":
|
|
275
|
+
self._gl = 1
|
|
276
|
+
elif ch == "\x0f":
|
|
277
|
+
self._gl = 0
|
|
278
|
+
|
|
279
|
+
def _tab(self, count: int) -> None:
|
|
280
|
+
for _ in range(count):
|
|
281
|
+
nxt = self.cx + 1
|
|
282
|
+
while nxt < self.cols - 1 and nxt not in self.tabs:
|
|
283
|
+
nxt += 1
|
|
284
|
+
self.cx = min(nxt, self.cols - 1)
|
|
285
|
+
self._wrap_pending = False
|
|
286
|
+
|
|
287
|
+
def _put_text(self, text: str) -> None:
|
|
288
|
+
charset = self._charsets[self._gl]
|
|
289
|
+
if charset == "0":
|
|
290
|
+
text = "".join(_DEC_SPECIAL.get(c, c) for c in text)
|
|
291
|
+
if text.isascii() and not self.insert_mode:
|
|
292
|
+
self._put_ascii(text)
|
|
293
|
+
return
|
|
294
|
+
pen = self._pen_id
|
|
295
|
+
for ch in text:
|
|
296
|
+
width = char_width(ch)
|
|
297
|
+
if width == 0:
|
|
298
|
+
self._combine(ch)
|
|
299
|
+
continue
|
|
300
|
+
if self._wrap_pending:
|
|
301
|
+
if self.autowrap:
|
|
302
|
+
self.cx = 0
|
|
303
|
+
self._index()
|
|
304
|
+
self._wrap_pending = False
|
|
305
|
+
if self.cx + width > self.cols:
|
|
306
|
+
if self.autowrap:
|
|
307
|
+
self.cx = 0
|
|
308
|
+
self._index()
|
|
309
|
+
else:
|
|
310
|
+
self.cx = self.cols - width
|
|
311
|
+
row = self.grid[self.cy]
|
|
312
|
+
if self.insert_mode:
|
|
313
|
+
self._insert_chars(width)
|
|
314
|
+
self._clear_wide_overlap(row, self.cx, width)
|
|
315
|
+
row.chars[self.cx] = ch
|
|
316
|
+
row.attrs[self.cx] = pen
|
|
317
|
+
if width == 2:
|
|
318
|
+
row.chars[self.cx + 1] = ""
|
|
319
|
+
row.attrs[self.cx + 1] = pen
|
|
320
|
+
self._dirty.add(self.cy)
|
|
321
|
+
self.cx += width
|
|
322
|
+
if self.cx >= self.cols:
|
|
323
|
+
self.cx = self.cols - 1
|
|
324
|
+
self._wrap_pending = True
|
|
325
|
+
|
|
326
|
+
def _put_ascii(self, text: str) -> None:
|
|
327
|
+
"""Escreve texto ASCII em trechos por linha, sem tratar caractere a caractere."""
|
|
328
|
+
pen = self._pen_id
|
|
329
|
+
i, n = 0, len(text)
|
|
330
|
+
cols = self.cols
|
|
331
|
+
while i < n:
|
|
332
|
+
if self._wrap_pending:
|
|
333
|
+
if self.autowrap:
|
|
334
|
+
self.cx = 0
|
|
335
|
+
self._index()
|
|
336
|
+
self._wrap_pending = False
|
|
337
|
+
row = self.grid[self.cy]
|
|
338
|
+
room = cols - self.cx
|
|
339
|
+
take = min(room, n - i)
|
|
340
|
+
if not self.autowrap and n - i > room:
|
|
341
|
+
seg = text[i:i + room - 1] + text[-1]
|
|
342
|
+
i = n
|
|
343
|
+
else:
|
|
344
|
+
seg = text[i:i + take]
|
|
345
|
+
i += take
|
|
346
|
+
width = len(seg)
|
|
347
|
+
self._clear_wide_overlap(row, self.cx, width)
|
|
348
|
+
row.chars[self.cx:self.cx + width] = seg
|
|
349
|
+
row.attrs[self.cx:self.cx + width] = [pen] * width
|
|
350
|
+
self._dirty.add(self.cy)
|
|
351
|
+
self.cx += width
|
|
352
|
+
if self.cx >= cols:
|
|
353
|
+
self.cx = cols - 1
|
|
354
|
+
self._wrap_pending = True
|
|
355
|
+
|
|
356
|
+
def _combine(self, ch: str) -> None:
|
|
357
|
+
col = self.cx - 1 if not self._wrap_pending else self.cx
|
|
358
|
+
row = self.grid[self.cy]
|
|
359
|
+
while col > 0 and row.chars[col] == "":
|
|
360
|
+
col -= 1
|
|
361
|
+
if 0 <= col < self.cols:
|
|
362
|
+
row.chars[col] += ch
|
|
363
|
+
self._dirty.add(self.cy)
|
|
364
|
+
|
|
365
|
+
@staticmethod
|
|
366
|
+
def _clear_wide_overlap(row: _Row, col: int, width: int) -> None:
|
|
367
|
+
if row.chars[col] == "" and col > 0:
|
|
368
|
+
row.chars[col - 1] = " "
|
|
369
|
+
end = col + width
|
|
370
|
+
if end < len(row.chars) and row.chars[end] == "":
|
|
371
|
+
row.chars[end] = " "
|
|
372
|
+
|
|
373
|
+
def _index(self) -> None:
|
|
374
|
+
if self.cy == self.bottom:
|
|
375
|
+
self._scroll_up(1)
|
|
376
|
+
elif self.cy < self.rows - 1:
|
|
377
|
+
self.cy += 1
|
|
378
|
+
self._wrap_pending = False
|
|
379
|
+
|
|
380
|
+
def _reverse_index(self) -> None:
|
|
381
|
+
if self.cy == self.top:
|
|
382
|
+
self._scroll_down(1)
|
|
383
|
+
elif self.cy > 0:
|
|
384
|
+
self.cy -= 1
|
|
385
|
+
self._wrap_pending = False
|
|
386
|
+
|
|
387
|
+
def _scroll_up(self, n: int) -> None:
|
|
388
|
+
n = min(n, self.bottom - self.top + 1)
|
|
389
|
+
if n <= 0:
|
|
390
|
+
return
|
|
391
|
+
keep_history = not self.alt_screen and self.top == 0
|
|
392
|
+
for _ in range(n):
|
|
393
|
+
gone = self.grid.pop(self.top)
|
|
394
|
+
if keep_history:
|
|
395
|
+
self._scrolled.append((gone.chars, gone.attrs))
|
|
396
|
+
self.grid.insert(self.bottom, _Row(self.cols))
|
|
397
|
+
self._mark_range(self.top, self.bottom)
|
|
398
|
+
|
|
399
|
+
def _scroll_down(self, n: int) -> None:
|
|
400
|
+
n = min(n, self.bottom - self.top + 1)
|
|
401
|
+
if n <= 0:
|
|
402
|
+
return
|
|
403
|
+
for _ in range(n):
|
|
404
|
+
self.grid.pop(self.bottom)
|
|
405
|
+
self.grid.insert(self.top, _Row(self.cols))
|
|
406
|
+
self._mark_range(self.top, self.bottom)
|
|
407
|
+
|
|
408
|
+
def _mark_range(self, first: int, last: int) -> None:
|
|
409
|
+
self._dirty.update(range(first, last + 1))
|
|
410
|
+
|
|
411
|
+
def _blank_row(self, r: int) -> None:
|
|
412
|
+
self.grid[r] = _Row(self.cols, self._erase_attr())
|
|
413
|
+
self._dirty.add(r)
|
|
414
|
+
|
|
415
|
+
def _erase_attr(self) -> int:
|
|
416
|
+
fg, bg, _flags = self._pen
|
|
417
|
+
if bg == DEFAULT_COLOR:
|
|
418
|
+
return 0
|
|
419
|
+
return self._attr_id((DEFAULT_COLOR, bg, 0))
|
|
420
|
+
|
|
421
|
+
def _insert_chars(self, n: int) -> None:
|
|
422
|
+
row = self.grid[self.cy]
|
|
423
|
+
n = min(n, self.cols - self.cx)
|
|
424
|
+
blank = self._erase_attr()
|
|
425
|
+
row.chars[self.cx:] = [" "] * n + row.chars[self.cx: self.cols - n]
|
|
426
|
+
row.attrs[self.cx:] = [blank] * n + row.attrs[self.cx: self.cols - n]
|
|
427
|
+
self._dirty.add(self.cy)
|
|
428
|
+
|
|
429
|
+
def _delete_chars(self, n: int) -> None:
|
|
430
|
+
row = self.grid[self.cy]
|
|
431
|
+
n = min(n, self.cols - self.cx)
|
|
432
|
+
blank = self._erase_attr()
|
|
433
|
+
row.chars[self.cx:] = row.chars[self.cx + n:] + [" "] * n
|
|
434
|
+
row.attrs[self.cx:] = row.attrs[self.cx + n:] + [blank] * n
|
|
435
|
+
self._dirty.add(self.cy)
|
|
436
|
+
|
|
437
|
+
def _erase_chars(self, n: int) -> None:
|
|
438
|
+
row = self.grid[self.cy]
|
|
439
|
+
end = min(self.cols, self.cx + n)
|
|
440
|
+
blank = self._erase_attr()
|
|
441
|
+
for c in range(self.cx, end):
|
|
442
|
+
row.chars[c] = " "
|
|
443
|
+
row.attrs[c] = blank
|
|
444
|
+
self._dirty.add(self.cy)
|
|
445
|
+
|
|
446
|
+
def _erase_line(self, mode: int) -> None:
|
|
447
|
+
row = self.grid[self.cy]
|
|
448
|
+
blank = self._erase_attr()
|
|
449
|
+
if mode == 0:
|
|
450
|
+
span = range(self.cx, self.cols)
|
|
451
|
+
elif mode == 1:
|
|
452
|
+
span = range(0, min(self.cx + 1, self.cols))
|
|
453
|
+
else:
|
|
454
|
+
span = range(self.cols)
|
|
455
|
+
for c in span:
|
|
456
|
+
row.chars[c] = " "
|
|
457
|
+
row.attrs[c] = blank
|
|
458
|
+
self._dirty.add(self.cy)
|
|
459
|
+
|
|
460
|
+
def _erase_display(self, mode: int) -> None:
|
|
461
|
+
if mode == 0:
|
|
462
|
+
self._erase_line(0)
|
|
463
|
+
for r in range(self.cy + 1, self.rows):
|
|
464
|
+
self._blank_row(r)
|
|
465
|
+
elif mode == 1:
|
|
466
|
+
for r in range(0, self.cy):
|
|
467
|
+
self._blank_row(r)
|
|
468
|
+
self._erase_line(1)
|
|
469
|
+
elif mode == 2:
|
|
470
|
+
for r in range(self.rows):
|
|
471
|
+
self._blank_row(r)
|
|
472
|
+
elif mode == 3:
|
|
473
|
+
self._scrolled.clear()
|
|
474
|
+
self.clear_history_requested = True
|
|
475
|
+
|
|
476
|
+
clear_history_requested = False
|
|
477
|
+
|
|
478
|
+
def _insert_lines(self, n: int) -> None:
|
|
479
|
+
if not self.top <= self.cy <= self.bottom:
|
|
480
|
+
return
|
|
481
|
+
n = min(n, self.bottom - self.cy + 1)
|
|
482
|
+
for _ in range(n):
|
|
483
|
+
self.grid.pop(self.bottom)
|
|
484
|
+
self.grid.insert(self.cy, _Row(self.cols, self._erase_attr()))
|
|
485
|
+
self._mark_range(self.cy, self.bottom)
|
|
486
|
+
self.cx = 0
|
|
487
|
+
self._wrap_pending = False
|
|
488
|
+
|
|
489
|
+
def _delete_lines(self, n: int) -> None:
|
|
490
|
+
if not self.top <= self.cy <= self.bottom:
|
|
491
|
+
return
|
|
492
|
+
n = min(n, self.bottom - self.cy + 1)
|
|
493
|
+
for _ in range(n):
|
|
494
|
+
self.grid.pop(self.cy)
|
|
495
|
+
self.grid.insert(self.bottom, _Row(self.cols, self._erase_attr()))
|
|
496
|
+
self._mark_range(self.cy, self.bottom)
|
|
497
|
+
self.cx = 0
|
|
498
|
+
self._wrap_pending = False
|
|
499
|
+
|
|
500
|
+
def _escape(self, data: str, i: int) -> int:
|
|
501
|
+
"""Processa a sequência em ``data[i]``; devolve o próximo índice ou -1 se incompleta."""
|
|
502
|
+
if i + 1 >= len(data):
|
|
503
|
+
return -1
|
|
504
|
+
nxt = data[i + 1]
|
|
505
|
+
if nxt == "[":
|
|
506
|
+
m = _CSI.match(data, i)
|
|
507
|
+
if m:
|
|
508
|
+
self._csi(m.group(1), m.group(2), m.group(3))
|
|
509
|
+
return m.end()
|
|
510
|
+
return -1 if _CSI_PREFIX.match(data, i) else self._skip_bad(data, i)
|
|
511
|
+
if nxt == "]":
|
|
512
|
+
m = _OSC.match(data, i)
|
|
513
|
+
if m:
|
|
514
|
+
self._osc(m.group(1))
|
|
515
|
+
return m.end()
|
|
516
|
+
return -1 if _OSC_PREFIX.match(data, i) else self._skip_bad(data, i)
|
|
517
|
+
if nxt in "PX^_":
|
|
518
|
+
m = _STRING_TERMINATED.match(data, i)
|
|
519
|
+
if m:
|
|
520
|
+
return m.end()
|
|
521
|
+
return -1 if _STRING_PREFIX.match(data, i) else self._skip_bad(data, i)
|
|
522
|
+
m = _ESC_SIMPLE.match(data, i)
|
|
523
|
+
if m:
|
|
524
|
+
self._esc(m.group(1), m.group(2))
|
|
525
|
+
return m.end()
|
|
526
|
+
return -1 if _ESC_PREFIX.match(data, i) else self._skip_bad(data, i)
|
|
527
|
+
|
|
528
|
+
@staticmethod
|
|
529
|
+
def _skip_bad(data: str, i: int) -> int:
|
|
530
|
+
return i + 2 if i + 2 <= len(data) else len(data)
|
|
531
|
+
|
|
532
|
+
def _esc(self, inter: str, final: str) -> None:
|
|
533
|
+
if inter in ("(", ")"):
|
|
534
|
+
self._charsets[0 if inter == "(" else 1] = final
|
|
535
|
+
return
|
|
536
|
+
if inter:
|
|
537
|
+
return
|
|
538
|
+
if final == "7":
|
|
539
|
+
self._save_cursor()
|
|
540
|
+
elif final == "8":
|
|
541
|
+
self._restore_cursor()
|
|
542
|
+
elif final == "M":
|
|
543
|
+
self._reverse_index()
|
|
544
|
+
elif final == "D":
|
|
545
|
+
self._index()
|
|
546
|
+
elif final == "E":
|
|
547
|
+
self.cx = 0
|
|
548
|
+
self._index()
|
|
549
|
+
elif final == "H":
|
|
550
|
+
self.tabs.add(self.cx)
|
|
551
|
+
elif final == "c":
|
|
552
|
+
self.reset()
|
|
553
|
+
elif final == "=":
|
|
554
|
+
self.app_keypad = True
|
|
555
|
+
elif final == ">":
|
|
556
|
+
self.app_keypad = False
|
|
557
|
+
|
|
558
|
+
def _save_cursor(self) -> None:
|
|
559
|
+
self._saved_cursor = (
|
|
560
|
+
self.cx, self.cy, self._pen, int(self._wrap_pending), tuple(self._charsets),
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
def _restore_cursor(self) -> None:
|
|
564
|
+
cx, cy, pen, wrap, charsets = self._saved_cursor
|
|
565
|
+
self.cx = min(cx, self.cols - 1)
|
|
566
|
+
self.cy = min(cy, self.rows - 1)
|
|
567
|
+
self._pen = pen
|
|
568
|
+
self._pen_id = self._attr_id(pen)
|
|
569
|
+
self._wrap_pending = bool(wrap)
|
|
570
|
+
self._charsets = list(charsets)
|
|
571
|
+
|
|
572
|
+
@staticmethod
|
|
573
|
+
def _params(raw: str, default: int = 0) -> list[int]:
|
|
574
|
+
if not raw:
|
|
575
|
+
return [default]
|
|
576
|
+
out: list[int] = []
|
|
577
|
+
for part in raw.split(";"):
|
|
578
|
+
head = part.split(":", 1)[0]
|
|
579
|
+
out.append(int(head) if head.isdigit() else default)
|
|
580
|
+
return out
|
|
581
|
+
|
|
582
|
+
def _csi(self, raw: str, inter: str, final: str) -> None:
|
|
583
|
+
private = raw[:1] if raw[:1] in "?<=>" else ""
|
|
584
|
+
if private:
|
|
585
|
+
raw = raw[1:]
|
|
586
|
+
if private == "?" and final in "hl":
|
|
587
|
+
self._dec_mode(self._params(raw), final == "h")
|
|
588
|
+
return
|
|
589
|
+
if private == ">":
|
|
590
|
+
if final == "c":
|
|
591
|
+
self.responses.append(b"\x1b[>0;0;0c")
|
|
592
|
+
return
|
|
593
|
+
if private or inter:
|
|
594
|
+
return
|
|
595
|
+
p = self._params(raw)
|
|
596
|
+
p0 = p[0]
|
|
597
|
+
n1 = p0 or 1
|
|
598
|
+
if final == "A":
|
|
599
|
+
self._move_y(-n1)
|
|
600
|
+
elif final in "Be":
|
|
601
|
+
self._move_y(n1)
|
|
602
|
+
elif final in "Ca":
|
|
603
|
+
self.cx = min(self.cols - 1, self.cx + n1)
|
|
604
|
+
self._wrap_pending = False
|
|
605
|
+
elif final == "D":
|
|
606
|
+
self.cx = max(0, self.cx - n1)
|
|
607
|
+
self._wrap_pending = False
|
|
608
|
+
elif final == "E":
|
|
609
|
+
self._move_y(n1)
|
|
610
|
+
self.cx = 0
|
|
611
|
+
elif final == "F":
|
|
612
|
+
self._move_y(-n1)
|
|
613
|
+
self.cx = 0
|
|
614
|
+
elif final in "G`":
|
|
615
|
+
self.cx = min(self.cols - 1, max(0, n1 - 1))
|
|
616
|
+
self._wrap_pending = False
|
|
617
|
+
elif final in "Hf":
|
|
618
|
+
row = (p[0] or 1) - 1
|
|
619
|
+
col = ((p[1] if len(p) > 1 else 1) or 1) - 1
|
|
620
|
+
self._goto(row, col)
|
|
621
|
+
elif final == "d":
|
|
622
|
+
self._goto(n1 - 1, self.cx, keep_col=True)
|
|
623
|
+
elif final == "J":
|
|
624
|
+
self._erase_display(p0)
|
|
625
|
+
elif final == "K":
|
|
626
|
+
self._erase_line(p0)
|
|
627
|
+
elif final == "L":
|
|
628
|
+
self._insert_lines(n1)
|
|
629
|
+
elif final == "M":
|
|
630
|
+
self._delete_lines(n1)
|
|
631
|
+
elif final == "@":
|
|
632
|
+
self._insert_chars(n1)
|
|
633
|
+
elif final == "P":
|
|
634
|
+
self._delete_chars(n1)
|
|
635
|
+
elif final == "X":
|
|
636
|
+
self._erase_chars(n1)
|
|
637
|
+
elif final == "S":
|
|
638
|
+
self._scroll_up(n1)
|
|
639
|
+
elif final == "T":
|
|
640
|
+
self._scroll_down(n1)
|
|
641
|
+
elif final == "m":
|
|
642
|
+
self._sgr(raw)
|
|
643
|
+
elif final == "r":
|
|
644
|
+
self._set_region(p)
|
|
645
|
+
elif final == "s":
|
|
646
|
+
self._save_cursor()
|
|
647
|
+
elif final == "u":
|
|
648
|
+
self._restore_cursor()
|
|
649
|
+
elif final == "n":
|
|
650
|
+
if p0 == 5:
|
|
651
|
+
self.responses.append(b"\x1b[0n")
|
|
652
|
+
elif p0 == 6:
|
|
653
|
+
row = self.cy - (self.top if self.origin_mode else 0) + 1
|
|
654
|
+
self.responses.append(f"\x1b[{row};{self.cx + 1}R".encode())
|
|
655
|
+
elif final == "c":
|
|
656
|
+
if p0 == 0:
|
|
657
|
+
self.responses.append(b"\x1b[?62;c")
|
|
658
|
+
elif final == "g":
|
|
659
|
+
if p0 == 3:
|
|
660
|
+
self.tabs.clear()
|
|
661
|
+
elif p0 == 0:
|
|
662
|
+
self.tabs.discard(self.cx)
|
|
663
|
+
elif final == "h":
|
|
664
|
+
if 4 in p:
|
|
665
|
+
self.insert_mode = True
|
|
666
|
+
elif final == "l" and 4 in p:
|
|
667
|
+
self.insert_mode = False
|
|
668
|
+
|
|
669
|
+
def _move_y(self, delta: int) -> None:
|
|
670
|
+
low, high = (self.top, self.bottom) if self.top <= self.cy <= self.bottom else (0, self.rows - 1)
|
|
671
|
+
self.cy = min(high, max(low, self.cy + delta))
|
|
672
|
+
self._wrap_pending = False
|
|
673
|
+
|
|
674
|
+
def _goto(self, row: int, col: int, keep_col: bool = False) -> None:
|
|
675
|
+
if self.origin_mode:
|
|
676
|
+
row = min(self.bottom, max(self.top, self.top + row))
|
|
677
|
+
else:
|
|
678
|
+
row = min(self.rows - 1, max(0, row))
|
|
679
|
+
self.cy = row
|
|
680
|
+
if not keep_col:
|
|
681
|
+
self.cx = min(self.cols - 1, max(0, col))
|
|
682
|
+
self._wrap_pending = False
|
|
683
|
+
|
|
684
|
+
def _set_region(self, p: list[int]) -> None:
|
|
685
|
+
top = (p[0] or 1) - 1
|
|
686
|
+
bottom = ((p[1] if len(p) > 1 else self.rows) or self.rows) - 1
|
|
687
|
+
if 0 <= top < bottom < self.rows:
|
|
688
|
+
self.top, self.bottom = top, bottom
|
|
689
|
+
else:
|
|
690
|
+
self.top, self.bottom = 0, self.rows - 1
|
|
691
|
+
self._goto(0, 0)
|
|
692
|
+
|
|
693
|
+
def _dec_mode(self, params: list[int], enable: bool) -> None:
|
|
694
|
+
for mode in params:
|
|
695
|
+
if mode == 1:
|
|
696
|
+
self.app_cursor = enable
|
|
697
|
+
elif mode == 6:
|
|
698
|
+
self.origin_mode = enable
|
|
699
|
+
self._goto(0, 0)
|
|
700
|
+
elif mode == 7:
|
|
701
|
+
self.autowrap = enable
|
|
702
|
+
elif mode == 25:
|
|
703
|
+
self.cursor_visible = enable
|
|
704
|
+
elif mode in (47, 1047, 1049):
|
|
705
|
+
self._set_alt_screen(enable, save_cursor=(mode == 1049))
|
|
706
|
+
elif mode in (1000, 1002, 1003):
|
|
707
|
+
self.mouse_mode = mode if enable else 0
|
|
708
|
+
elif mode == 1006:
|
|
709
|
+
self.mouse_sgr = enable
|
|
710
|
+
elif mode == 1004:
|
|
711
|
+
self.focus_events = enable
|
|
712
|
+
elif mode == 2004:
|
|
713
|
+
self.bracketed_paste = enable
|
|
714
|
+
|
|
715
|
+
def _set_alt_screen(self, enable: bool, save_cursor: bool) -> None:
|
|
716
|
+
if enable == self.alt_screen:
|
|
717
|
+
return
|
|
718
|
+
if enable:
|
|
719
|
+
if save_cursor:
|
|
720
|
+
self._save_cursor()
|
|
721
|
+
self._saved_main = (self.grid, self.top, self.bottom)
|
|
722
|
+
self.grid = [_Row(self.cols) for _ in range(self.rows)]
|
|
723
|
+
self.alt_screen = True
|
|
724
|
+
self.top, self.bottom = 0, self.rows - 1
|
|
725
|
+
else:
|
|
726
|
+
if self._saved_main is not None:
|
|
727
|
+
self.grid, self.top, self.bottom = self._saved_main
|
|
728
|
+
self._saved_main = None
|
|
729
|
+
self.alt_screen = False
|
|
730
|
+
if save_cursor:
|
|
731
|
+
self._restore_cursor()
|
|
732
|
+
self.all_dirty = True
|
|
733
|
+
|
|
734
|
+
def _sgr(self, raw: str) -> None:
|
|
735
|
+
fg, bg, flags = self._pen
|
|
736
|
+
parts = raw.split(";") if raw else ["0"]
|
|
737
|
+
i = 0
|
|
738
|
+
while i < len(parts):
|
|
739
|
+
token = parts[i]
|
|
740
|
+
sub = token.split(":")
|
|
741
|
+
code = int(sub[0]) if sub[0].isdigit() else 0
|
|
742
|
+
if code == 0:
|
|
743
|
+
fg, bg, flags = DEFAULT_COLOR, DEFAULT_COLOR, 0
|
|
744
|
+
elif code == 1:
|
|
745
|
+
flags |= FLAG_BOLD
|
|
746
|
+
elif code == 2:
|
|
747
|
+
flags |= FLAG_DIM
|
|
748
|
+
elif code == 3:
|
|
749
|
+
flags |= FLAG_ITALIC
|
|
750
|
+
elif code in (4, 21):
|
|
751
|
+
flags |= FLAG_UNDERLINE
|
|
752
|
+
elif code == 7:
|
|
753
|
+
flags |= FLAG_REVERSE
|
|
754
|
+
elif code == 8:
|
|
755
|
+
flags |= FLAG_HIDDEN
|
|
756
|
+
elif code == 9:
|
|
757
|
+
flags |= FLAG_STRIKE
|
|
758
|
+
elif code == 22:
|
|
759
|
+
flags &= ~(FLAG_BOLD | FLAG_DIM)
|
|
760
|
+
elif code == 23:
|
|
761
|
+
flags &= ~FLAG_ITALIC
|
|
762
|
+
elif code == 24:
|
|
763
|
+
flags &= ~FLAG_UNDERLINE
|
|
764
|
+
elif code == 27:
|
|
765
|
+
flags &= ~FLAG_REVERSE
|
|
766
|
+
elif code == 28:
|
|
767
|
+
flags &= ~FLAG_HIDDEN
|
|
768
|
+
elif code == 29:
|
|
769
|
+
flags &= ~FLAG_STRIKE
|
|
770
|
+
elif 30 <= code <= 37:
|
|
771
|
+
fg = code - 30
|
|
772
|
+
elif code == 39:
|
|
773
|
+
fg = DEFAULT_COLOR
|
|
774
|
+
elif 40 <= code <= 47:
|
|
775
|
+
bg = code - 40
|
|
776
|
+
elif code == 49:
|
|
777
|
+
bg = DEFAULT_COLOR
|
|
778
|
+
elif 90 <= code <= 97:
|
|
779
|
+
fg = code - 90 + 8
|
|
780
|
+
elif 100 <= code <= 107:
|
|
781
|
+
bg = code - 100 + 8
|
|
782
|
+
elif code in (38, 48):
|
|
783
|
+
color, used = self._extended_color(sub, parts, i)
|
|
784
|
+
i += used
|
|
785
|
+
if color is not None:
|
|
786
|
+
if code == 38:
|
|
787
|
+
fg = color
|
|
788
|
+
else:
|
|
789
|
+
bg = color
|
|
790
|
+
i += 1
|
|
791
|
+
self._pen = (fg, bg, flags)
|
|
792
|
+
self._pen_id = self._attr_id(self._pen)
|
|
793
|
+
|
|
794
|
+
@staticmethod
|
|
795
|
+
def _extended_color(sub: list[str], parts: list[str], i: int) -> tuple[int | None, int]:
|
|
796
|
+
"""Lê ``38``/``48`` em formato com ``;`` ou ``:``; devolve (cor, partes extras usadas)."""
|
|
797
|
+
if len(sub) > 1:
|
|
798
|
+
nums = [int(x) if x.isdigit() else 0 for x in sub[1:]]
|
|
799
|
+
if nums and nums[0] == 5 and len(nums) >= 2:
|
|
800
|
+
return min(255, nums[1]), 0
|
|
801
|
+
if nums and nums[0] == 2:
|
|
802
|
+
rgb = nums[-3:] if len(nums) >= 4 else nums[1:4]
|
|
803
|
+
if len(rgb) == 3:
|
|
804
|
+
return 0x1000000 | (min(255, rgb[0]) << 16) | (min(255, rgb[1]) << 8) | min(255, rgb[2]), 0
|
|
805
|
+
return None, 0
|
|
806
|
+
mode = parts[i + 1] if i + 1 < len(parts) else ""
|
|
807
|
+
if mode == "5" and i + 2 < len(parts):
|
|
808
|
+
value = parts[i + 2]
|
|
809
|
+
return (min(255, int(value)) if value.isdigit() else None), 2
|
|
810
|
+
if mode == "2" and i + 4 < len(parts):
|
|
811
|
+
try:
|
|
812
|
+
r, g, b = (min(255, int(x)) for x in parts[i + 2: i + 5])
|
|
813
|
+
except ValueError:
|
|
814
|
+
return None, 4
|
|
815
|
+
return 0x1000000 | (r << 16) | (g << 8) | b, 4
|
|
816
|
+
return None, 0
|
|
817
|
+
|
|
818
|
+
def _osc(self, payload: str) -> None:
|
|
819
|
+
code, _, value = payload.partition(";")
|
|
820
|
+
if code in ("0", "2"):
|
|
821
|
+
self.title = value[:256]
|
|
822
|
+
elif code == "7":
|
|
823
|
+
match = re.match(r"file://[^/]*(/.*)", value)
|
|
824
|
+
if match:
|
|
825
|
+
from urllib.parse import unquote
|
|
826
|
+
|
|
827
|
+
self.cwd = unquote(match.group(1))
|
|
828
|
+
|
|
829
|
+
def resize(self, rows: int, cols: int) -> None:
|
|
830
|
+
"""Ajusta a grade para ``rows`` x ``cols``, preservando o conteúdo possível."""
|
|
831
|
+
rows, cols = max(1, rows), max(1, cols)
|
|
832
|
+
if rows == self.rows and cols == self.cols:
|
|
833
|
+
return
|
|
834
|
+
if rows < self.rows:
|
|
835
|
+
surplus = self.rows - rows
|
|
836
|
+
drop_top = min(surplus, max(0, self.cy + 1 - rows)) if self.cy >= rows else 0
|
|
837
|
+
for _ in range(drop_top):
|
|
838
|
+
gone = self.grid.pop(0)
|
|
839
|
+
if not self.alt_screen:
|
|
840
|
+
self._scrolled.append((gone.chars, gone.attrs))
|
|
841
|
+
self.cy -= 1
|
|
842
|
+
del self.grid[rows:]
|
|
843
|
+
else:
|
|
844
|
+
for _ in range(rows - self.rows):
|
|
845
|
+
self.grid.append(_Row(cols))
|
|
846
|
+
for row in self.grid:
|
|
847
|
+
row.resize(cols)
|
|
848
|
+
if self._saved_main is not None:
|
|
849
|
+
saved_grid = self._saved_main[0]
|
|
850
|
+
while len(saved_grid) < rows:
|
|
851
|
+
saved_grid.append(_Row(cols))
|
|
852
|
+
del saved_grid[rows:]
|
|
853
|
+
for row in saved_grid:
|
|
854
|
+
row.resize(cols)
|
|
855
|
+
self.rows, self.cols = rows, cols
|
|
856
|
+
self.cx = min(self.cx, cols - 1)
|
|
857
|
+
self.cy = min(self.cy, rows - 1)
|
|
858
|
+
self.top, self.bottom = 0, rows - 1
|
|
859
|
+
self.tabs = set(range(0, cols, 8))
|
|
860
|
+
self._wrap_pending = False
|
|
861
|
+
self.all_dirty = True
|
|
862
|
+
|
|
863
|
+
def clear_screen_and_history(self) -> None:
|
|
864
|
+
"""Limpa a tela e descarta o histórico pendente (botão de limpar)."""
|
|
865
|
+
for r in range(self.rows):
|
|
866
|
+
self._blank_row(r)
|
|
867
|
+
self.cx = self.cy = 0
|
|
868
|
+
self._scrolled.clear()
|
|
869
|
+
self._wrap_pending = False
|
|
870
|
+
self.all_dirty = True
|