clownfish 0.5.0__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.
- clownfish/__init__.py +132 -0
- clownfish/__main__.py +6 -0
- clownfish/app.py +5 -0
- clownfish/clownfish.png +0 -0
- clownfish/config.py +84 -0
- clownfish/core/__init__.py +34 -0
- clownfish/core/keyboard.py +197 -0
- clownfish/core/models.py +122 -0
- clownfish/core/parser.py +461 -0
- clownfish/core/screen.py +719 -0
- clownfish/core/search.py +153 -0
- clownfish/frontends/__init__.py +1 -0
- clownfish/frontends/tkinter/__init__.py +17 -0
- clownfish/frontends/tkinter/app.py +344 -0
- clownfish/frontends/tkinter/input.py +214 -0
- clownfish/frontends/tkinter/pane.py +390 -0
- clownfish/frontends/tkinter/renderer.py +223 -0
- clownfish/frontends/tkinter/split.py +261 -0
- clownfish/frontends/tkinter/tabs.py +217 -0
- clownfish/main.py +6 -0
- clownfish/models.py +25 -0
- clownfish/parser.py +5 -0
- clownfish/screen.py +5 -0
- clownfish/session/__init__.py +15 -0
- clownfish/session/session.py +294 -0
- clownfish/session/transport.py +53 -0
- clownfish/syntax.py +42 -0
- clownfish/transports/__init__.py +6 -0
- clownfish/transports/pty.py +243 -0
- clownfish/transports/ssh.py +53 -0
- clownfish-0.5.0.dist-info/METADATA +214 -0
- clownfish-0.5.0.dist-info/RECORD +34 -0
- clownfish-0.5.0.dist-info/WHEEL +4 -0
- clownfish-0.5.0.dist-info/entry_points.txt +3 -0
clownfish/__init__.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""clownfish - A modern terminal emulator written in Python and Tkinter."""
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import TYPE_CHECKING, Any
|
|
5
|
+
|
|
6
|
+
from clownfish.config import TerminalConfig
|
|
7
|
+
from clownfish.core.keyboard import encode_key_press, encode_kitty_key
|
|
8
|
+
from clownfish.core.models import (
|
|
9
|
+
Cell,
|
|
10
|
+
PromptMarker,
|
|
11
|
+
Selection,
|
|
12
|
+
Style,
|
|
13
|
+
char_width,
|
|
14
|
+
)
|
|
15
|
+
from clownfish.core.parser import VT100Parser
|
|
16
|
+
from clownfish.core.screen import ScreenBuffer
|
|
17
|
+
from clownfish.core.search import SearchMatch, SearchState, search_screen
|
|
18
|
+
from clownfish.session.session import TerminalSession
|
|
19
|
+
from clownfish.session.transport import Transport
|
|
20
|
+
from clownfish.syntax import highlight_code, is_syntax_available, print_highlighted
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from clownfish.frontends.tkinter.app import TkTerminalApp
|
|
24
|
+
from clownfish.frontends.tkinter.pane import TerminalPane
|
|
25
|
+
from clownfish.frontends.tkinter.split import SplitView
|
|
26
|
+
from clownfish.frontends.tkinter.tabs import TabbedTerminalView
|
|
27
|
+
from clownfish.transports.pty import PTYTransport
|
|
28
|
+
from clownfish.transports.ssh import SSHTransport
|
|
29
|
+
|
|
30
|
+
__version__ = "0.5.0"
|
|
31
|
+
__all__ = [
|
|
32
|
+
"Cell",
|
|
33
|
+
"PTYTransport",
|
|
34
|
+
"PromptMarker",
|
|
35
|
+
"SSHTransport",
|
|
36
|
+
"ScreenBuffer",
|
|
37
|
+
"SearchMatch",
|
|
38
|
+
"SearchState",
|
|
39
|
+
"Selection",
|
|
40
|
+
"SplitView",
|
|
41
|
+
"Style",
|
|
42
|
+
"TabbedTerminalView",
|
|
43
|
+
"TerminalConfig",
|
|
44
|
+
"TerminalPane",
|
|
45
|
+
"TerminalSession",
|
|
46
|
+
"TkTerminalApp",
|
|
47
|
+
"Transport",
|
|
48
|
+
"VT100Parser",
|
|
49
|
+
"char_width",
|
|
50
|
+
"encode_key_press",
|
|
51
|
+
"encode_kitty_key",
|
|
52
|
+
"highlight_code",
|
|
53
|
+
"is_syntax_available",
|
|
54
|
+
"main",
|
|
55
|
+
"print_highlighted",
|
|
56
|
+
"search_screen",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def __getattr__(name: str) -> Any:
|
|
61
|
+
if name == "TkTerminalApp":
|
|
62
|
+
from clownfish.frontends.tkinter.app import TkTerminalApp
|
|
63
|
+
|
|
64
|
+
return TkTerminalApp
|
|
65
|
+
if name == "TerminalPane":
|
|
66
|
+
from clownfish.frontends.tkinter.pane import TerminalPane
|
|
67
|
+
|
|
68
|
+
return TerminalPane
|
|
69
|
+
if name == "SplitView":
|
|
70
|
+
from clownfish.frontends.tkinter.split import SplitView
|
|
71
|
+
|
|
72
|
+
return SplitView
|
|
73
|
+
if name == "TabbedTerminalView":
|
|
74
|
+
from clownfish.frontends.tkinter.tabs import TabbedTerminalView
|
|
75
|
+
|
|
76
|
+
return TabbedTerminalView
|
|
77
|
+
if name == "PTYTransport":
|
|
78
|
+
from clownfish.transports.pty import PTYTransport
|
|
79
|
+
|
|
80
|
+
return PTYTransport
|
|
81
|
+
if name == "SSHTransport":
|
|
82
|
+
from clownfish.transports.ssh import SSHTransport
|
|
83
|
+
|
|
84
|
+
return SSHTransport
|
|
85
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def main(args: list[str] | None = None) -> None:
|
|
89
|
+
"""Launch clownfish terminal emulator."""
|
|
90
|
+
import sys
|
|
91
|
+
|
|
92
|
+
argv = args if args is not None else sys.argv[1:]
|
|
93
|
+
if argv and argv[0] == "--pty-trampoline":
|
|
94
|
+
import fcntl
|
|
95
|
+
import os
|
|
96
|
+
import termios
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
fcntl.ioctl(0, termios.TIOCSCTTY, 0)
|
|
100
|
+
except OSError:
|
|
101
|
+
pass
|
|
102
|
+
os.execvp(argv[1], argv[1:])
|
|
103
|
+
return
|
|
104
|
+
|
|
105
|
+
if argv and argv[0] in ("-v", "--version"):
|
|
106
|
+
print(f"clownfish {__version__}")
|
|
107
|
+
return
|
|
108
|
+
|
|
109
|
+
if argv and argv[0] in ("-h", "--help"):
|
|
110
|
+
print(f"clownfish {__version__} - A modern terminal emulator written in Python and Tkinter")
|
|
111
|
+
print("\nUsage:\n clownfish [options]")
|
|
112
|
+
print("\nOptions:\n -v, --version Show version number and exit")
|
|
113
|
+
print(" -h, --help Show this help message and exit")
|
|
114
|
+
return
|
|
115
|
+
|
|
116
|
+
import tkinter as tk
|
|
117
|
+
|
|
118
|
+
from clownfish.frontends.tkinter.app import TkTerminalApp
|
|
119
|
+
|
|
120
|
+
root = tk.Tk()
|
|
121
|
+
root.title("clownfish")
|
|
122
|
+
|
|
123
|
+
icon_path = Path(__file__).resolve().parent / "clownfish.png"
|
|
124
|
+
if icon_path.exists():
|
|
125
|
+
try:
|
|
126
|
+
app_icon = tk.PhotoImage(file=str(icon_path))
|
|
127
|
+
root.iconphoto(True, app_icon)
|
|
128
|
+
except (tk.TclError, OSError):
|
|
129
|
+
pass
|
|
130
|
+
|
|
131
|
+
_ = TkTerminalApp(root, cols=80, rows=24)
|
|
132
|
+
root.mainloop()
|
clownfish/__main__.py
ADDED
clownfish/app.py
ADDED
clownfish/clownfish.png
ADDED
|
Binary file
|
clownfish/config.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Configuration options and user persistence for clownfish terminal emulator."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import dataclasses
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from clownfish.core.models import DEFAULT_BG, DEFAULT_FG
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclasses.dataclass
|
|
15
|
+
class TerminalConfig:
|
|
16
|
+
"""Configurable terminal options."""
|
|
17
|
+
|
|
18
|
+
cols: int = 80
|
|
19
|
+
rows: int = 24
|
|
20
|
+
max_scrollback: int = 10_000
|
|
21
|
+
font_family: str = "Menlo" if sys.platform == "darwin" else "Monospace"
|
|
22
|
+
font_size: int = 13
|
|
23
|
+
default_fg: tuple[int, int, int] = DEFAULT_FG
|
|
24
|
+
default_bg: tuple[int, int, int] = DEFAULT_BG
|
|
25
|
+
cursor_color: str = "#e5e5e5"
|
|
26
|
+
selection_color: str = "#4a90e2"
|
|
27
|
+
search_bg: str = "#d7ba7d"
|
|
28
|
+
search_fg: str = "#141419"
|
|
29
|
+
tab_bar_bg: str = "#18181c"
|
|
30
|
+
tab_active_bg: str = "#282830"
|
|
31
|
+
tab_fg: str = "#a0a0a8"
|
|
32
|
+
tab_active_fg: str = "#ffffff"
|
|
33
|
+
allow_clipboard_read: bool = False
|
|
34
|
+
allow_clipboard_write: bool = True
|
|
35
|
+
confirm_close_active_process: bool = True
|
|
36
|
+
confirm_multiline_paste: bool = False
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def default_config_path() -> Path:
|
|
40
|
+
"""Return the default configuration file location."""
|
|
41
|
+
return Path.home() / ".config" / "clownfish" / "config.json"
|
|
42
|
+
|
|
43
|
+
def to_dict(self) -> dict[str, Any]:
|
|
44
|
+
"""Convert configuration to a JSON-serializable dictionary."""
|
|
45
|
+
data = dataclasses.asdict(self)
|
|
46
|
+
# Ensure tuples are converted to lists
|
|
47
|
+
data["default_fg"] = list(self.default_fg)
|
|
48
|
+
data["default_bg"] = list(self.default_bg)
|
|
49
|
+
return data
|
|
50
|
+
|
|
51
|
+
@classmethod
|
|
52
|
+
def from_dict(cls, data: dict[str, Any]) -> TerminalConfig:
|
|
53
|
+
"""Create a TerminalConfig from a dictionary."""
|
|
54
|
+
kwargs: dict[str, Any] = {}
|
|
55
|
+
for f in dataclasses.fields(cls):
|
|
56
|
+
if f.name in data:
|
|
57
|
+
val = data[f.name]
|
|
58
|
+
if f.name in ("default_fg", "default_bg") and isinstance(val, (list, tuple)):
|
|
59
|
+
val = tuple(val[:3])
|
|
60
|
+
kwargs[f.name] = val
|
|
61
|
+
return cls(**kwargs)
|
|
62
|
+
|
|
63
|
+
def save(self, path: Path | str | None = None) -> Path:
|
|
64
|
+
"""Save configuration to JSON file."""
|
|
65
|
+
target_path = Path(path) if path is not None else self.default_config_path()
|
|
66
|
+
target_path.parent.mkdir(parents=True, exist_ok=True)
|
|
67
|
+
with open(target_path, "w", encoding="utf-8") as f:
|
|
68
|
+
json.dump(self.to_dict(), f, indent=2)
|
|
69
|
+
return target_path
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def load(cls, path: Path | str | None = None) -> TerminalConfig:
|
|
73
|
+
"""Load configuration from JSON file, returning defaults if not found."""
|
|
74
|
+
target_path = Path(path) if path is not None else cls.default_config_path()
|
|
75
|
+
if not target_path.exists():
|
|
76
|
+
return cls()
|
|
77
|
+
try:
|
|
78
|
+
with open(target_path, encoding="utf-8") as f:
|
|
79
|
+
data = json.load(f)
|
|
80
|
+
if isinstance(data, dict):
|
|
81
|
+
return cls.from_dict(data)
|
|
82
|
+
except (json.JSONDecodeError, OSError):
|
|
83
|
+
pass
|
|
84
|
+
return cls()
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Core terminal models, keyboard encoding, parser, and screen buffer."""
|
|
2
|
+
|
|
3
|
+
from clownfish.core.keyboard import encode_key_press, encode_kitty_key
|
|
4
|
+
from clownfish.core.models import (
|
|
5
|
+
DEFAULT_BG,
|
|
6
|
+
DEFAULT_FG,
|
|
7
|
+
WORD_DELIMITERS,
|
|
8
|
+
Cell,
|
|
9
|
+
PromptMarker,
|
|
10
|
+
Selection,
|
|
11
|
+
Style,
|
|
12
|
+
char_width,
|
|
13
|
+
)
|
|
14
|
+
from clownfish.core.parser import VT100Parser
|
|
15
|
+
from clownfish.core.screen import ScreenBuffer
|
|
16
|
+
from clownfish.core.search import SearchMatch, SearchState, search_screen
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"DEFAULT_BG",
|
|
20
|
+
"DEFAULT_FG",
|
|
21
|
+
"WORD_DELIMITERS",
|
|
22
|
+
"Cell",
|
|
23
|
+
"PromptMarker",
|
|
24
|
+
"ScreenBuffer",
|
|
25
|
+
"SearchMatch",
|
|
26
|
+
"SearchState",
|
|
27
|
+
"Selection",
|
|
28
|
+
"Style",
|
|
29
|
+
"VT100Parser",
|
|
30
|
+
"char_width",
|
|
31
|
+
"encode_key_press",
|
|
32
|
+
"encode_kitty_key",
|
|
33
|
+
"search_screen",
|
|
34
|
+
]
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"""Keyboard encoding routines including standard xterm sequences and Kitty keyboard protocol."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _get_modifier_mask(keysym: str, state: int) -> int:
|
|
9
|
+
"""Calculate xterm/Kitty modifier code (1 + Shift=1, Alt=2, Ctrl=4, Super=8).
|
|
10
|
+
|
|
11
|
+
Correctly filters hardware flags (such as 0x40 on macOS for navigation/Fn keys
|
|
12
|
+
and 0x10 on Linux for NumLock) so arrow keys are not corrupted with spurious modifiers.
|
|
13
|
+
"""
|
|
14
|
+
is_mac = sys.platform == "darwin"
|
|
15
|
+
mod = 1
|
|
16
|
+
if state & 0x1: # Shift
|
|
17
|
+
mod += 1
|
|
18
|
+
|
|
19
|
+
# Alt / Option (Meta):
|
|
20
|
+
# - 0x8: standard Mod1 (Alt / Option across xterm/Kitty and X11)
|
|
21
|
+
# - 0x10: on macOS Aqua Tk, Option is Mod2 (0x10)
|
|
22
|
+
# - 0x80: Mod5 / AltGr
|
|
23
|
+
if (state & 0x8) or (state & 0x80) or (is_mac and (state & 0x10)):
|
|
24
|
+
mod += 2
|
|
25
|
+
|
|
26
|
+
if state & 0x4: # Control
|
|
27
|
+
mod += 4
|
|
28
|
+
|
|
29
|
+
# Super (Windows / Command key):
|
|
30
|
+
# On Linux/X11, Mod4 (0x40) is Super.
|
|
31
|
+
# On macOS, 0x40 is the hardware Fn/Arrow key flag (NSEventModifierFlagFunction)
|
|
32
|
+
# and must never be treated as Super.
|
|
33
|
+
if not is_mac and (state & 0x40):
|
|
34
|
+
mod += 8
|
|
35
|
+
|
|
36
|
+
return mod
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def encode_kitty_key(keysym: str, state: int, char: str = "") -> bytes | None:
|
|
40
|
+
"""Encode key event into Kitty/xterm modified keyboard escape sequences."""
|
|
41
|
+
mod = _get_modifier_mask(keysym, state)
|
|
42
|
+
|
|
43
|
+
if mod <= 1:
|
|
44
|
+
return None
|
|
45
|
+
|
|
46
|
+
# Standard xterm/Kitty modified navigation and cursor keys:
|
|
47
|
+
# Arrow keys use CSI 1;modifier [A-D]
|
|
48
|
+
# Home/End use CSI 1;modifier [H,F]
|
|
49
|
+
# PC edit keys use CSI <key-id>;modifier ~
|
|
50
|
+
nav_keys = {
|
|
51
|
+
"Up": f"\x1b[1;{mod}A".encode("ascii"),
|
|
52
|
+
"Down": f"\x1b[1;{mod}B".encode("ascii"),
|
|
53
|
+
"Right": f"\x1b[1;{mod}C".encode("ascii"),
|
|
54
|
+
"Left": f"\x1b[1;{mod}D".encode("ascii"),
|
|
55
|
+
"Home": f"\x1b[1;{mod}H".encode("ascii"),
|
|
56
|
+
"End": f"\x1b[1;{mod}F".encode("ascii"),
|
|
57
|
+
"Insert": f"\x1b[2;{mod}~".encode("ascii"),
|
|
58
|
+
"Delete": f"\x1b[3;{mod}~".encode("ascii"),
|
|
59
|
+
"Prior": f"\x1b[5;{mod}~".encode("ascii"), # Page Up
|
|
60
|
+
"Next": f"\x1b[6;{mod}~".encode("ascii"), # Page Down
|
|
61
|
+
"F1": f"\x1b[1;{mod}P".encode("ascii"),
|
|
62
|
+
"F2": f"\x1b[1;{mod}Q".encode("ascii"),
|
|
63
|
+
"F3": f"\x1b[1;{mod}R".encode("ascii"),
|
|
64
|
+
"F4": f"\x1b[1;{mod}S".encode("ascii"),
|
|
65
|
+
"F5": f"\x1b[15;{mod}~".encode("ascii"),
|
|
66
|
+
"F6": f"\x1b[17;{mod}~".encode("ascii"),
|
|
67
|
+
"F7": f"\x1b[18;{mod}~".encode("ascii"),
|
|
68
|
+
"F8": f"\x1b[19;{mod}~".encode("ascii"),
|
|
69
|
+
"F9": f"\x1b[20;{mod}~".encode("ascii"),
|
|
70
|
+
"F10": f"\x1b[21;{mod}~".encode("ascii"),
|
|
71
|
+
"F11": f"\x1b[23;{mod}~".encode("ascii"),
|
|
72
|
+
"F12": f"\x1b[24;{mod}~".encode("ascii"),
|
|
73
|
+
}
|
|
74
|
+
if keysym in nav_keys:
|
|
75
|
+
return nav_keys[keysym]
|
|
76
|
+
|
|
77
|
+
# Functional keys with Kitty CSI u encoding:
|
|
78
|
+
kitty_u_keys = {
|
|
79
|
+
"Return": 13,
|
|
80
|
+
"Tab": 9,
|
|
81
|
+
"BackSpace": 127,
|
|
82
|
+
"Escape": 27,
|
|
83
|
+
}
|
|
84
|
+
if keysym in kitty_u_keys:
|
|
85
|
+
code = kitty_u_keys[keysym]
|
|
86
|
+
return f"\x1b[{code};{mod}u".encode("ascii")
|
|
87
|
+
|
|
88
|
+
# Regular character keys with modifier (e.g. Ctrl+C -> 99;5u)
|
|
89
|
+
if len(char) == 1:
|
|
90
|
+
code = ord(char.lower() if (mod & 4) else char)
|
|
91
|
+
return f"\x1b[{code};{mod}u".encode("ascii")
|
|
92
|
+
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def encode_key_press(
|
|
97
|
+
keysym: str,
|
|
98
|
+
state: int = 0,
|
|
99
|
+
char: str = "",
|
|
100
|
+
kitty_keyboard_flags: int = 0,
|
|
101
|
+
application_cursor_keys: bool = False,
|
|
102
|
+
) -> bytes | None:
|
|
103
|
+
"""Encode a keyboard event into terminal bytes."""
|
|
104
|
+
# 1. Kitty keyboard protocol for modified keys
|
|
105
|
+
if kitty_keyboard_flags & 1:
|
|
106
|
+
kitty_encoded = encode_kitty_key(keysym, state, char)
|
|
107
|
+
if kitty_encoded is not None:
|
|
108
|
+
return kitty_encoded
|
|
109
|
+
|
|
110
|
+
mod = _get_modifier_mask(keysym, state)
|
|
111
|
+
|
|
112
|
+
# 2. Shift+Tab in xterm
|
|
113
|
+
if keysym == "ISO_Left_Tab" or (keysym == "Tab" and (state & 0x1)):
|
|
114
|
+
return b"\x1b[Z"
|
|
115
|
+
|
|
116
|
+
# 3. Standard xterm modified navigation and function keys
|
|
117
|
+
if mod > 1:
|
|
118
|
+
modified_keys = {
|
|
119
|
+
"Up": f"\x1b[1;{mod}A".encode("ascii"),
|
|
120
|
+
"Down": f"\x1b[1;{mod}B".encode("ascii"),
|
|
121
|
+
"Right": f"\x1b[1;{mod}C".encode("ascii"),
|
|
122
|
+
"Left": f"\x1b[1;{mod}D".encode("ascii"),
|
|
123
|
+
"Home": f"\x1b[1;{mod}H".encode("ascii"),
|
|
124
|
+
"End": f"\x1b[1;{mod}F".encode("ascii"),
|
|
125
|
+
"Insert": f"\x1b[2;{mod}~".encode("ascii"),
|
|
126
|
+
"Delete": f"\x1b[3;{mod}~".encode("ascii"),
|
|
127
|
+
"Prior": f"\x1b[5;{mod}~".encode("ascii"),
|
|
128
|
+
"Next": f"\x1b[6;{mod}~".encode("ascii"),
|
|
129
|
+
"F1": f"\x1b[1;{mod}P".encode("ascii"),
|
|
130
|
+
"F2": f"\x1b[1;{mod}Q".encode("ascii"),
|
|
131
|
+
"F3": f"\x1b[1;{mod}R".encode("ascii"),
|
|
132
|
+
"F4": f"\x1b[1;{mod}S".encode("ascii"),
|
|
133
|
+
"F5": f"\x1b[15;{mod}~".encode("ascii"),
|
|
134
|
+
"F6": f"\x1b[17;{mod}~".encode("ascii"),
|
|
135
|
+
"F7": f"\x1b[18;{mod}~".encode("ascii"),
|
|
136
|
+
"F8": f"\x1b[19;{mod}~".encode("ascii"),
|
|
137
|
+
"F9": f"\x1b[20;{mod}~".encode("ascii"),
|
|
138
|
+
"F10": f"\x1b[21;{mod}~".encode("ascii"),
|
|
139
|
+
"F11": f"\x1b[23;{mod}~".encode("ascii"),
|
|
140
|
+
"F12": f"\x1b[24;{mod}~".encode("ascii"),
|
|
141
|
+
}
|
|
142
|
+
if keysym in modified_keys:
|
|
143
|
+
return modified_keys[keysym]
|
|
144
|
+
|
|
145
|
+
arrow_prefix = b"\x1bO" if application_cursor_keys else b"\x1b["
|
|
146
|
+
|
|
147
|
+
arrow_map = {
|
|
148
|
+
"Up": arrow_prefix + b"A",
|
|
149
|
+
"Down": arrow_prefix + b"B",
|
|
150
|
+
"Right": arrow_prefix + b"C",
|
|
151
|
+
"Left": arrow_prefix + b"D",
|
|
152
|
+
"Home": arrow_prefix + b"H",
|
|
153
|
+
"End": arrow_prefix + b"F",
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
f_keys_unmodified = {
|
|
157
|
+
"F1": b"\x1bOP",
|
|
158
|
+
"F2": b"\x1bOQ",
|
|
159
|
+
"F3": b"\x1bOR",
|
|
160
|
+
"F4": b"\x1bOS",
|
|
161
|
+
"F5": b"\x1b[15~",
|
|
162
|
+
"F6": b"\x1b[17~",
|
|
163
|
+
"F7": b"\x1b[18~",
|
|
164
|
+
"F8": b"\x1b[19~",
|
|
165
|
+
"F9": b"\x1b[20~",
|
|
166
|
+
"F10": b"\x1b[21~",
|
|
167
|
+
"F11": b"\x1b[23~",
|
|
168
|
+
"F12": b"\x1b[24~",
|
|
169
|
+
}
|
|
170
|
+
if keysym in f_keys_unmodified:
|
|
171
|
+
return f_keys_unmodified[keysym]
|
|
172
|
+
|
|
173
|
+
keymap = {
|
|
174
|
+
"Return": b"\r",
|
|
175
|
+
"BackSpace": b"\x7f",
|
|
176
|
+
"Tab": b"\t",
|
|
177
|
+
"Escape": b"\x1b",
|
|
178
|
+
"Insert": b"\x1b[2~",
|
|
179
|
+
"Delete": b"\x1b[3~",
|
|
180
|
+
"Prior": b"\x1b[5~",
|
|
181
|
+
"Next": b"\x1b[6~",
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if keysym in arrow_map:
|
|
185
|
+
return arrow_map[keysym]
|
|
186
|
+
if keysym in keymap:
|
|
187
|
+
return keymap[keysym]
|
|
188
|
+
is_mac = sys.platform == "darwin"
|
|
189
|
+
has_alt = bool((state & 0x10) if is_mac else (state & 0x8 or state & 0x80))
|
|
190
|
+
if (state & 0x4) and keysym.isalpha() and len(keysym) == 1:
|
|
191
|
+
return bytes([ord(keysym.lower()) - 96])
|
|
192
|
+
if has_alt and char and not (state & 0x4):
|
|
193
|
+
return b"\x1b" + char.encode("utf-8")
|
|
194
|
+
if char:
|
|
195
|
+
return char.encode("utf-8")
|
|
196
|
+
|
|
197
|
+
return None
|
clownfish/core/models.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Data models and constants for clownfish terminal emulator."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import dataclasses
|
|
6
|
+
import unicodedata
|
|
7
|
+
|
|
8
|
+
DEFAULT_FG: tuple[int, int, int] = (229, 229, 229)
|
|
9
|
+
DEFAULT_BG: tuple[int, int, int] = (20, 20, 25)
|
|
10
|
+
WORD_DELIMITERS: set[str] = set(" \t\n\r()[]{}\"'`:;,=")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def char_width(char: str) -> int:
|
|
14
|
+
"""Return the display column width (0, 1, or 2) of a character in monospace terminals."""
|
|
15
|
+
if not char:
|
|
16
|
+
return 0
|
|
17
|
+
cp = ord(char[0])
|
|
18
|
+
# Zero-width control, format, combining marks, Fitzpatrick skin tone modifiers, and variation selectors
|
|
19
|
+
if (
|
|
20
|
+
cp in (0x200B, 0x200C, 0x200D, 0xFE0E, 0xFE0F, 0x20E3)
|
|
21
|
+
or (0x1F3FB <= cp <= 0x1F3FF) # Fitzpatrick emoji modifiers (skin tone)
|
|
22
|
+
or (0xE0020 <= cp <= 0xE007F) # Tags
|
|
23
|
+
or unicodedata.combining(char[0])
|
|
24
|
+
):
|
|
25
|
+
return 0
|
|
26
|
+
# East Asian Width 'W' (Wide) and 'F' (Fullwidth) occupy 2 columns
|
|
27
|
+
eaw = unicodedata.east_asian_width(char[0])
|
|
28
|
+
if eaw in ("W", "F"):
|
|
29
|
+
return 2
|
|
30
|
+
# Standard emoji and pictograph ranges occupying 2 columns in modern terminals
|
|
31
|
+
if (
|
|
32
|
+
(0x1F300 <= cp <= 0x1F5FF)
|
|
33
|
+
or (0x1F600 <= cp <= 0x1F64F)
|
|
34
|
+
or (0x1F680 <= cp <= 0x1F6FF)
|
|
35
|
+
or (0x1F900 <= cp <= 0x1F9FF)
|
|
36
|
+
or (0x1FA70 <= cp <= 0x1FAFF)
|
|
37
|
+
or (0x2600 <= cp <= 0x26FF)
|
|
38
|
+
or (0x2700 <= cp <= 0x27BF)
|
|
39
|
+
):
|
|
40
|
+
return 2
|
|
41
|
+
return 1
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclasses.dataclass(slots=True)
|
|
45
|
+
class Style:
|
|
46
|
+
"""Styling attributes for a terminal cell."""
|
|
47
|
+
|
|
48
|
+
fg: tuple[int, int, int] = DEFAULT_FG
|
|
49
|
+
bg: tuple[int, int, int] = DEFAULT_BG
|
|
50
|
+
bold: bool = False
|
|
51
|
+
italic: bool = False
|
|
52
|
+
underline: bool = False
|
|
53
|
+
reverse: bool = False
|
|
54
|
+
|
|
55
|
+
def copy(self) -> Style:
|
|
56
|
+
return Style(
|
|
57
|
+
self.fg, self.bg, self.bold, self.italic, self.underline, self.reverse
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
def is_default(self) -> bool:
|
|
61
|
+
"""Return True if style has default foreground, background, and no attributes."""
|
|
62
|
+
return (
|
|
63
|
+
self.fg == DEFAULT_FG
|
|
64
|
+
and self.bg == DEFAULT_BG
|
|
65
|
+
and not self.bold
|
|
66
|
+
and not self.italic
|
|
67
|
+
and not self.underline
|
|
68
|
+
and not self.reverse
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclasses.dataclass(slots=True)
|
|
73
|
+
class Cell:
|
|
74
|
+
"""Individual character cell on the terminal grid."""
|
|
75
|
+
|
|
76
|
+
char: str = " "
|
|
77
|
+
style: Style = dataclasses.field(default_factory=Style)
|
|
78
|
+
url: str | None = None # OSC 8 Hyperlink target
|
|
79
|
+
width: int = 1 # 1 for regular, 2 for wide lead cell, 0 for wide continuation spacer
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class Row(list[Cell]):
|
|
83
|
+
"""Physical row of cells with soft-wrap continuation metadata."""
|
|
84
|
+
|
|
85
|
+
def __init__(self, cells: list[Cell] | None = None, is_wrapped: bool = False) -> None:
|
|
86
|
+
super().__init__(cells if cells is not None else [])
|
|
87
|
+
self.is_wrapped = is_wrapped
|
|
88
|
+
|
|
89
|
+
def copy(self) -> Row:
|
|
90
|
+
return Row([dataclasses.replace(c, style=c.style.copy()) for c in self], is_wrapped=self.is_wrapped)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@dataclasses.dataclass
|
|
94
|
+
class PromptMarker:
|
|
95
|
+
"""Semantic prompt boundary captured via OSC 133 with absolute row tracking."""
|
|
96
|
+
|
|
97
|
+
row: int
|
|
98
|
+
command: str = ""
|
|
99
|
+
exit_code: int | None = None
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclasses.dataclass
|
|
103
|
+
class Selection:
|
|
104
|
+
"""Tracks mouse selection coordinates in terminal character space."""
|
|
105
|
+
|
|
106
|
+
active: bool = False
|
|
107
|
+
start_col: int = 0
|
|
108
|
+
start_row: int = 0
|
|
109
|
+
end_col: int = 0
|
|
110
|
+
end_row: int = 0
|
|
111
|
+
|
|
112
|
+
def is_empty(self) -> bool:
|
|
113
|
+
return not self.active or (
|
|
114
|
+
self.start_col == self.end_col and self.start_row == self.end_row
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def normalized(self) -> tuple[tuple[int, int], tuple[int, int]]:
|
|
118
|
+
p1 = (self.start_col, self.start_row)
|
|
119
|
+
p2 = (self.end_col, self.end_row)
|
|
120
|
+
if (p1[1], p1[0]) <= (p2[1], p2[0]):
|
|
121
|
+
return p1, p2
|
|
122
|
+
return p2, p1
|