zettcode-ssh 0.5.2__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.
- zettcode/ssh/__init__.py +6 -0
- zettcode/ssh/config.py +74 -0
- zettcode/ssh/panel.py +682 -0
- zettcode/ssh/plugin.py +362 -0
- zettcode/ssh/presenter.py +34 -0
- zettcode/ssh/py.typed +0 -0
- zettcode/ssh/remote.py +421 -0
- zettcode/ssh/tools.py +144 -0
- zettcode/ssh/transport.py +500 -0
- zettcode_ssh-0.5.2.dist-info/METADATA +204 -0
- zettcode_ssh-0.5.2.dist-info/RECORD +14 -0
- zettcode_ssh-0.5.2.dist-info/WHEEL +4 -0
- zettcode_ssh-0.5.2.dist-info/entry_points.txt +2 -0
- zettcode_ssh-0.5.2.dist-info/licenses/LICENSE +21 -0
zettcode/ssh/panel.py
ADDED
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
"""The bottom panel ``/ssh`` opens: a target, how to sign in, and the two things to do with it.
|
|
2
|
+
|
|
3
|
+
The page keeps the shape the other plugin panels use — a title with the state at the right, rows with the mark on
|
|
4
|
+
the left and the value beside it, one line saying what the marked row does, and the keys — rather than boxes, so it
|
|
5
|
+
reads like ``/monitor`` and ``/pet``. A value that is typed is drawn where the others are, with a caret, and a
|
|
6
|
+
password is painted as bullets.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import asyncio
|
|
12
|
+
from collections.abc import Awaitable, Callable
|
|
13
|
+
from time import monotonic
|
|
14
|
+
|
|
15
|
+
from zettcode.plugin_api import (
|
|
16
|
+
SWITCH_WIDTH,
|
|
17
|
+
Canvas,
|
|
18
|
+
Host,
|
|
19
|
+
MouseAction,
|
|
20
|
+
MouseEvent,
|
|
21
|
+
PasteEvent,
|
|
22
|
+
Rail,
|
|
23
|
+
Style,
|
|
24
|
+
TextEvent,
|
|
25
|
+
Widget,
|
|
26
|
+
blend,
|
|
27
|
+
display_width,
|
|
28
|
+
draw_switch,
|
|
29
|
+
pressed,
|
|
30
|
+
truncate,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from .transport import Target
|
|
34
|
+
|
|
35
|
+
#: A blank, the title, a blank, the seven rows, a blank, what the marked row does, and the keys.
|
|
36
|
+
ROWS = 13
|
|
37
|
+
|
|
38
|
+
#: The first row of the list, counted from the top of the panel.
|
|
39
|
+
LIST_TOP = 3
|
|
40
|
+
|
|
41
|
+
#: Columns a row's name takes, and the width of the switch's longer word ("key/agent").
|
|
42
|
+
NAME_WIDTH = 12
|
|
43
|
+
WORD_WIDTH = 9
|
|
44
|
+
|
|
45
|
+
#: The fewest columns a value keeps before the note beside it is dropped instead of squeezing it.
|
|
46
|
+
MIN_VALUE = 12
|
|
47
|
+
|
|
48
|
+
MASK = "\u2022"
|
|
49
|
+
MARK = "\u25b8"
|
|
50
|
+
ON, OFF = "\u25c9", "\u25cb"
|
|
51
|
+
|
|
52
|
+
#: What the right-hand column of each row says, in the few words a row has room for. The sign-in rows say
|
|
53
|
+
#: something else once the switch is on password (:meth:`SSHPage._summary`).
|
|
54
|
+
SUMMARIES: tuple[str, ...] = (
|
|
55
|
+
"select or type a host",
|
|
56
|
+
"remote working directory",
|
|
57
|
+
"from ~/.ssh/config",
|
|
58
|
+
"no password needed",
|
|
59
|
+
"not needed",
|
|
60
|
+
"check, then use it",
|
|
61
|
+
"close the remote channel",
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
#: The rows, in order: the label, and the sentence the line under the list shows while the mark is on it.
|
|
65
|
+
ROWS_INFO: tuple[tuple[str, str], ...] = (
|
|
66
|
+
("Host", "an alias from ~/.ssh/config, or user@hostname typed by hand"),
|
|
67
|
+
("Workspace", "the absolute remote directory every remote tool works in"),
|
|
68
|
+
("User", "leave empty to use the user ~/.ssh/config gives this host"),
|
|
69
|
+
("Sign in", "key/agent signs in with your SSH key and needs no password"),
|
|
70
|
+
("Password", "kept in memory for this session only; never saved or inherited"),
|
|
71
|
+
("Connect", "check the remote workspace, then use this target"),
|
|
72
|
+
("Disconnect", "close the channel; the local tools keep working either way"),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
HOST, WORKSPACE, USER, SIGN_IN, PASSWORD, CONNECT, LOCAL = range(7)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class SSHPage(Widget):
|
|
79
|
+
"""Choose a remote workspace and how to sign in, then connect or go back to local tools.
|
|
80
|
+
|
|
81
|
+
Shape::
|
|
82
|
+
|
|
83
|
+
SSH ● connected · full
|
|
84
|
+
|
|
85
|
+
▸ Host build-host-1 select or type a host
|
|
86
|
+
Workspace /srv/project remote working directory
|
|
87
|
+
User (from config) from ~/.ssh/config
|
|
88
|
+
Sign in ●───────── key/agent no password needed
|
|
89
|
+
Password (none needed) not needed
|
|
90
|
+
Connect ↵ enter check, then use it
|
|
91
|
+
Disconnect ↵ enter close the remote channel
|
|
92
|
+
|
|
93
|
+
Host: an alias from ~/.ssh/config, or user@hostname typed by hand
|
|
94
|
+
↑ ↓ row · ← → switch · F2 hosts · enter hosts/next · esc cancel
|
|
95
|
+
|
|
96
|
+
The value column gives a long value the room the note on its right does not need, so a remote path is shown
|
|
97
|
+
from its start. A note is left out when the panel is too narrow for it (``left < 18``), and on a panel shorter
|
|
98
|
+
than the rows the list scrolls to keep the marked row on screen. Picking a host first replaces these rows with
|
|
99
|
+
the aliases from ``~/.ssh/config`` and "Enter a host manually…", drawn the same way.
|
|
100
|
+
Up, Down and Tab move the mark; Left, Right and Space set the sign-in method; printing and pasting edit the row
|
|
101
|
+
the mark is on. Typing in the password row selects password sign-in by itself, and an empty password is refused
|
|
102
|
+
before a connection is attempted. Enter on Connect (or Disconnect) does it; on Host with aliases it opens the
|
|
103
|
+
host picker; elsewhere it moves down one row.
|
|
104
|
+
Escape cancels, and cancelling while a connection is being checked keeps the previous target.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
HINT = "\u2191 \u2193 row \u00b7 \u2190 \u2192 switch \u00b7 F2 hosts \u00b7 enter hosts/next \u00b7 esc cancel"
|
|
108
|
+
HINT_ACTION = "\u2191 \u2193 row \u00b7 enter do it \u00b7 esc cancel"
|
|
109
|
+
HINT_HOSTS = "\u2191 \u2193 host \u00b7 enter choose \u00b7 esc back"
|
|
110
|
+
|
|
111
|
+
def __init__(
|
|
112
|
+
self,
|
|
113
|
+
*,
|
|
114
|
+
target: Target | None,
|
|
115
|
+
status: Callable[[], str],
|
|
116
|
+
on_apply: Callable[[Target | None, str], Awaitable[None]],
|
|
117
|
+
on_close: Callable[[], None],
|
|
118
|
+
aliases: tuple[str, ...] = (),
|
|
119
|
+
) -> None:
|
|
120
|
+
super().__init__()
|
|
121
|
+
self.target = target
|
|
122
|
+
if target is None:
|
|
123
|
+
self.values = ["", "", ""]
|
|
124
|
+
else:
|
|
125
|
+
self.values = [target.host, target.path, target.user or ""]
|
|
126
|
+
self.cursors = [len(value) for value in self.values]
|
|
127
|
+
self.rail = Rail(("key/agent", "password"), 0 if target is None or target.auth == "key" else 1)
|
|
128
|
+
self.password = ""
|
|
129
|
+
self.mark = 0
|
|
130
|
+
self.error = ""
|
|
131
|
+
self.notice = ""
|
|
132
|
+
self.closed = False
|
|
133
|
+
self._started = monotonic()
|
|
134
|
+
self._cancelling = False
|
|
135
|
+
self.status = status
|
|
136
|
+
self.on_apply = on_apply
|
|
137
|
+
self.on_close = on_close
|
|
138
|
+
self.pending: asyncio.Task[None] | None = None
|
|
139
|
+
self.aliases = aliases
|
|
140
|
+
self.choosing = bool(aliases) and target is None
|
|
141
|
+
self.choice = 0
|
|
142
|
+
self.scroll = 0
|
|
143
|
+
self._token = f"ssh-panel:{id(self)}"
|
|
144
|
+
|
|
145
|
+
# -- the switch and its slide ----------------------------------------------------------
|
|
146
|
+
@property
|
|
147
|
+
def auth(self) -> str:
|
|
148
|
+
return "key" if self.rail.selected == 0 else "password"
|
|
149
|
+
|
|
150
|
+
@auth.setter
|
|
151
|
+
def auth(self, value: str) -> None:
|
|
152
|
+
self.rail.choose(0 if value == "key" else 1, self._now(), instant=True)
|
|
153
|
+
|
|
154
|
+
def _now(self) -> float:
|
|
155
|
+
return self.app.clock() if self.app is not None else monotonic()
|
|
156
|
+
|
|
157
|
+
def _choose(self, index: int) -> None:
|
|
158
|
+
"""Send the knob to key/agent (0) or password (1), sliding unless the reader asked for no motion."""
|
|
159
|
+
reduced = self.app is not None and self.app.reduced_motion
|
|
160
|
+
if self.rail.choose(index, self._now(), instant=reduced) and self.app is not None:
|
|
161
|
+
self.app.scheduler.animate(self._token)
|
|
162
|
+
|
|
163
|
+
def on_mount(self) -> None:
|
|
164
|
+
"""Keep a frame loop while the knob is moving or a connection is being checked."""
|
|
165
|
+
self._animate(self.rail.moving or self._connecting())
|
|
166
|
+
|
|
167
|
+
def on_unmount(self) -> None:
|
|
168
|
+
"""Cancel a probe still in flight and put the frame loop away.
|
|
169
|
+
|
|
170
|
+
Ctrl-C on a page closes it without asking it (``Overlay`` has no interrupt), so this is the one hook that
|
|
171
|
+
runs: a connection checked after the panel is gone must not take the tools over with no panel to say so.
|
|
172
|
+
"""
|
|
173
|
+
self.closed = True
|
|
174
|
+
if self._connecting() and self.pending is not None:
|
|
175
|
+
self.pending.cancel()
|
|
176
|
+
self.rail.moving = False
|
|
177
|
+
self._animate(False)
|
|
178
|
+
|
|
179
|
+
def _animate(self, active: bool) -> None:
|
|
180
|
+
if self.app is not None and not self.app.reduced_motion:
|
|
181
|
+
self.app.scheduler.animate(self._token, active=active)
|
|
182
|
+
|
|
183
|
+
def on_tick(self) -> bool:
|
|
184
|
+
"""Arrive the knob and say whether anything is still moving; the dots advance with the clock."""
|
|
185
|
+
landed = self.rail.arrive(self._now())
|
|
186
|
+
if not self.rail.moving and not self._connecting():
|
|
187
|
+
self._animate(False)
|
|
188
|
+
return self.rail.moving or landed or self._connecting()
|
|
189
|
+
|
|
190
|
+
def _connecting(self) -> bool:
|
|
191
|
+
return self.pending is not None and not self.pending.done()
|
|
192
|
+
|
|
193
|
+
def notice_now(self, now: float | None = None) -> str:
|
|
194
|
+
"""Return the working line: the word with one to three dots, from the clock rather than the frame count."""
|
|
195
|
+
if not self.notice:
|
|
196
|
+
return ""
|
|
197
|
+
elapsed = (self._now() if now is None else now) - self._started
|
|
198
|
+
return self.notice + "." * (1 + int(max(0.0, elapsed) / 0.4) % 3)
|
|
199
|
+
|
|
200
|
+
# -- what the rows hold ---------------------------------------------------------------
|
|
201
|
+
@property
|
|
202
|
+
def focusable(self) -> bool:
|
|
203
|
+
return True
|
|
204
|
+
|
|
205
|
+
def field(self, row: int) -> int | None:
|
|
206
|
+
"""Return which typed value a row edits, if it edits one."""
|
|
207
|
+
return {HOST: 0, WORKSPACE: 1, USER: 2, PASSWORD: 3}.get(row)
|
|
208
|
+
|
|
209
|
+
def caret(self, row: int) -> int:
|
|
210
|
+
return self.cursors[row] if row != PASSWORD else len(self.password)
|
|
211
|
+
|
|
212
|
+
def value(self, row: int) -> str:
|
|
213
|
+
if row == PASSWORD:
|
|
214
|
+
return self.password
|
|
215
|
+
index = self.field(row)
|
|
216
|
+
return "" if index is None else self.values[index]
|
|
217
|
+
|
|
218
|
+
def target_now(self) -> Target:
|
|
219
|
+
"""Return the target the rows describe, refusing a password sign-in with no password."""
|
|
220
|
+
if self.auth == "password" and not self.password:
|
|
221
|
+
raise ValueError("Enter the password for password sign-in, or switch to key/agent")
|
|
222
|
+
host, path, user = (self.values + ["", "", ""])[:3]
|
|
223
|
+
return Target(host, path, user or None, self.auth)
|
|
224
|
+
|
|
225
|
+
# -- choosing a configured host -------------------------------------------------------
|
|
226
|
+
def choose(self, index: int) -> None:
|
|
227
|
+
"""Take a host from the list, or go to the host row to type one."""
|
|
228
|
+
if index < len(self.aliases):
|
|
229
|
+
self.values[0] = self.aliases[index]
|
|
230
|
+
self.cursors[0] = len(self.values[0])
|
|
231
|
+
self.mark = WORKSPACE
|
|
232
|
+
else:
|
|
233
|
+
self.mark = HOST
|
|
234
|
+
self.choosing = False
|
|
235
|
+
self.error = ""
|
|
236
|
+
|
|
237
|
+
def _hosts(self) -> None:
|
|
238
|
+
if self.aliases:
|
|
239
|
+
self.choosing = True
|
|
240
|
+
self.choice = min(self.choice, len(self.aliases))
|
|
241
|
+
|
|
242
|
+
# -- doing it -------------------------------------------------------------------------
|
|
243
|
+
def confirm(self, host: Host) -> None:
|
|
244
|
+
if self.pending is not None and not self.pending.done():
|
|
245
|
+
return
|
|
246
|
+
try:
|
|
247
|
+
target = None if self.mark == LOCAL else self.target_now()
|
|
248
|
+
except ValueError as error:
|
|
249
|
+
self.error = str(error)
|
|
250
|
+
return
|
|
251
|
+
|
|
252
|
+
async def apply() -> None:
|
|
253
|
+
self.error = ""
|
|
254
|
+
self._cancelling = False
|
|
255
|
+
self.notice = "Disconnecting" if target is None else "Connecting"
|
|
256
|
+
self._started = self._now()
|
|
257
|
+
self._animate(True)
|
|
258
|
+
host.invalidate()
|
|
259
|
+
try:
|
|
260
|
+
await self.on_apply(target, "" if target is None else self.password)
|
|
261
|
+
except asyncio.CancelledError:
|
|
262
|
+
if not self.closed:
|
|
263
|
+
self.error = "Cancelled; the previous target is unchanged"
|
|
264
|
+
except Exception as error:
|
|
265
|
+
if not self.closed:
|
|
266
|
+
self.error = str(error)
|
|
267
|
+
else:
|
|
268
|
+
if not self.closed:
|
|
269
|
+
self.on_close()
|
|
270
|
+
finally:
|
|
271
|
+
self.notice = ""
|
|
272
|
+
host.invalidate()
|
|
273
|
+
|
|
274
|
+
self.pending = asyncio.create_task(apply())
|
|
275
|
+
|
|
276
|
+
# -- editing --------------------------------------------------------------------------
|
|
277
|
+
def insert(self, text: str) -> None:
|
|
278
|
+
index = self.field(self.mark)
|
|
279
|
+
if index is None:
|
|
280
|
+
return
|
|
281
|
+
# A host copied out of a terminal arrives with the line's newline; take the line, refuse anything else.
|
|
282
|
+
if text.endswith("\n"):
|
|
283
|
+
text = text[:-1].rstrip("\r")
|
|
284
|
+
if any(ord(char) < 32 or ord(char) == 127 for char in text):
|
|
285
|
+
self.error = "This row takes single-line text"
|
|
286
|
+
return
|
|
287
|
+
if not text:
|
|
288
|
+
return
|
|
289
|
+
if index == 3:
|
|
290
|
+
if self.auth != "password":
|
|
291
|
+
self.auth = "password"
|
|
292
|
+
value = self.password
|
|
293
|
+
if len(value) + len(text) > 1024:
|
|
294
|
+
self.error = "Password exceeds 1024 characters"
|
|
295
|
+
return
|
|
296
|
+
cursor = len(value)
|
|
297
|
+
self.password = value[:cursor] + text
|
|
298
|
+
else:
|
|
299
|
+
cursor = self.cursors[index]
|
|
300
|
+
value = self.values[index]
|
|
301
|
+
if len(value) + len(text) > 4096:
|
|
302
|
+
self.error = "This row exceeds 4096 characters"
|
|
303
|
+
return
|
|
304
|
+
self.values[index] = value[:cursor] + text + value[cursor:]
|
|
305
|
+
self.cursors[index] = cursor + len(text)
|
|
306
|
+
self.error = ""
|
|
307
|
+
|
|
308
|
+
def _edit_key(self, key: str) -> bool:
|
|
309
|
+
index = self.field(self.mark)
|
|
310
|
+
if index is None:
|
|
311
|
+
return False
|
|
312
|
+
value = self.password if index == 3 else self.values[index]
|
|
313
|
+
cursor = len(value) if index == 3 else self.cursors[index]
|
|
314
|
+
if key == "left":
|
|
315
|
+
cursor = max(0, cursor - 1)
|
|
316
|
+
elif key == "right":
|
|
317
|
+
cursor = min(len(value), cursor + 1)
|
|
318
|
+
elif key == "home":
|
|
319
|
+
cursor = 0
|
|
320
|
+
elif key == "end":
|
|
321
|
+
cursor = len(value)
|
|
322
|
+
elif key == "backspace" and cursor:
|
|
323
|
+
value, cursor = value[: cursor - 1] + value[cursor:], cursor - 1
|
|
324
|
+
elif key == "delete":
|
|
325
|
+
value = value[:cursor] + value[cursor + 1 :]
|
|
326
|
+
else:
|
|
327
|
+
return False
|
|
328
|
+
if index == 3:
|
|
329
|
+
self.password = value
|
|
330
|
+
else:
|
|
331
|
+
self.values[index], self.cursors[index] = value, cursor
|
|
332
|
+
self.error = ""
|
|
333
|
+
return True
|
|
334
|
+
|
|
335
|
+
def capture_event(self, event: object, host: Host) -> bool:
|
|
336
|
+
if pressed(event) == "escape":
|
|
337
|
+
if self._connecting():
|
|
338
|
+
# Cancel once: a second Escape must not abort the close that frees the password.
|
|
339
|
+
if not self._cancelling and self.pending is not None:
|
|
340
|
+
self._cancelling = True
|
|
341
|
+
self.notice = "Cancelling"
|
|
342
|
+
self.pending.cancel()
|
|
343
|
+
elif self.choosing:
|
|
344
|
+
self.choosing = False
|
|
345
|
+
else:
|
|
346
|
+
self.on_close()
|
|
347
|
+
return True
|
|
348
|
+
return False
|
|
349
|
+
|
|
350
|
+
def handle(self, event: object, host: Host) -> bool:
|
|
351
|
+
if self.pending is not None and not self.pending.done():
|
|
352
|
+
return True
|
|
353
|
+
if self.choosing:
|
|
354
|
+
return self._handle_hosts(event, host)
|
|
355
|
+
if isinstance(event, MouseEvent):
|
|
356
|
+
return self._handle_mouse(event, host)
|
|
357
|
+
if isinstance(event, (TextEvent, PasteEvent)):
|
|
358
|
+
if isinstance(event, TextEvent) and (event.control or event.alt):
|
|
359
|
+
return False
|
|
360
|
+
# A space on the sign-in row is the switch's key, not text for a field that is not editable.
|
|
361
|
+
if self.mark == SIGN_IN and event.text == " ":
|
|
362
|
+
self._choose(1 - self.rail.selected)
|
|
363
|
+
self.error = ""
|
|
364
|
+
return True
|
|
365
|
+
self.insert(event.text)
|
|
366
|
+
return True
|
|
367
|
+
key = pressed(event)
|
|
368
|
+
if key is None:
|
|
369
|
+
return False
|
|
370
|
+
if key == "f2":
|
|
371
|
+
self._hosts()
|
|
372
|
+
return True
|
|
373
|
+
if key in {"tab", "down", "backtab", "up"}:
|
|
374
|
+
step = -1 if key in {"up", "backtab"} else 1
|
|
375
|
+
self.mark = (self.mark + step) % len(ROWS_INFO)
|
|
376
|
+
self.error = ""
|
|
377
|
+
return True
|
|
378
|
+
if key == "enter":
|
|
379
|
+
if self.mark == HOST and self.aliases:
|
|
380
|
+
self._hosts()
|
|
381
|
+
elif self.mark in (CONNECT, LOCAL):
|
|
382
|
+
self.confirm(host)
|
|
383
|
+
else:
|
|
384
|
+
self.mark = (self.mark + 1) % len(ROWS_INFO)
|
|
385
|
+
return True
|
|
386
|
+
if self.mark == SIGN_IN and key in {"left", "right", "space"}:
|
|
387
|
+
if key == "left":
|
|
388
|
+
self._choose(0)
|
|
389
|
+
elif key == "right":
|
|
390
|
+
self._choose(1)
|
|
391
|
+
else:
|
|
392
|
+
self._choose(1 - self.rail.selected)
|
|
393
|
+
self.error = ""
|
|
394
|
+
return True
|
|
395
|
+
return self._edit_key(key)
|
|
396
|
+
|
|
397
|
+
def _handle_hosts(self, event: object, host: Host) -> bool:
|
|
398
|
+
count = len(self.aliases) + 1
|
|
399
|
+
visible = self._visible()
|
|
400
|
+
if isinstance(event, MouseEvent):
|
|
401
|
+
row = event.y - self.rect.y - LIST_TOP
|
|
402
|
+
if event.action is MouseAction.DOWN and visible > 0 and 0 <= row < visible and self.scroll + row < count:
|
|
403
|
+
self.choose(self.scroll + row)
|
|
404
|
+
return True
|
|
405
|
+
return False
|
|
406
|
+
key = pressed(event)
|
|
407
|
+
if key in {"up", "down", "tab", "backtab"}:
|
|
408
|
+
self.choice = (self.choice + (-1 if key in {"up", "backtab"} else 1)) % count
|
|
409
|
+
self.scroll = max(0, min(self.scroll, self.choice))
|
|
410
|
+
if self.choice >= self.scroll + visible:
|
|
411
|
+
self.scroll = self.choice - visible + 1
|
|
412
|
+
elif key == "enter":
|
|
413
|
+
self.choose(self.choice)
|
|
414
|
+
else:
|
|
415
|
+
return False
|
|
416
|
+
return True
|
|
417
|
+
|
|
418
|
+
def _handle_mouse(self, event: MouseEvent, host: Host) -> bool:
|
|
419
|
+
if event.action is not MouseAction.DOWN:
|
|
420
|
+
return False
|
|
421
|
+
top, visible = self._top(), self._visible()
|
|
422
|
+
row = event.y - (self.rect.y + LIST_TOP) + top
|
|
423
|
+
if visible <= 0 or not top <= row < top + visible:
|
|
424
|
+
return False
|
|
425
|
+
was = self.mark
|
|
426
|
+
self.mark, self.error = row, ""
|
|
427
|
+
switch_end = self._switch_column() + SWITCH_WIDTH + 1
|
|
428
|
+
if row == SIGN_IN and self._switch_column() <= event.x <= switch_end:
|
|
429
|
+
self._choose(1 - self.rail.selected)
|
|
430
|
+
elif row in (CONNECT, LOCAL) and was == row:
|
|
431
|
+
self.confirm(host)
|
|
432
|
+
elif row == HOST and was == row:
|
|
433
|
+
self._hosts()
|
|
434
|
+
return True
|
|
435
|
+
|
|
436
|
+
# -- painting -------------------------------------------------------------------------
|
|
437
|
+
def _switch_column(self) -> int:
|
|
438
|
+
return self.rect.x + 2 + 2 + NAME_WIDTH
|
|
439
|
+
|
|
440
|
+
def _state(self) -> tuple[str, str]:
|
|
441
|
+
"""Return the word at the title's right and the colour role it wears."""
|
|
442
|
+
if self._connecting():
|
|
443
|
+
return self.notice_now() or "Working", "accent"
|
|
444
|
+
if self.target is None:
|
|
445
|
+
return "off", "muted"
|
|
446
|
+
return self.status(), "accent"
|
|
447
|
+
|
|
448
|
+
def render(self, canvas: Canvas) -> None:
|
|
449
|
+
if self.rect.empty:
|
|
450
|
+
return
|
|
451
|
+
theme = self.theme
|
|
452
|
+
surface = theme.surface_alt
|
|
453
|
+
canvas.fill(self.rect.x, self.rect.y, self.rect.width, self.rect.height, Style(background=surface))
|
|
454
|
+
x = self.rect.x + 2
|
|
455
|
+
room = max(0, self.rect.width - 4)
|
|
456
|
+
muted = Style(foreground=theme.muted, background=surface)
|
|
457
|
+
bold = Style(foreground=theme.text, background=surface, bold=True)
|
|
458
|
+
quiet = blend(theme.muted, surface, 0.25)
|
|
459
|
+
quiet_style = Style(foreground=quiet, background=surface)
|
|
460
|
+
accent = Style(foreground=theme.accent, background=surface, bold=True)
|
|
461
|
+
canvas.draw_text(x, self.rect.y + 1, "SSH", bold, max_width=room)
|
|
462
|
+
state, role = self._state()
|
|
463
|
+
style = accent if role == "accent" else muted
|
|
464
|
+
canvas.draw_text(self.rect.right - 2 - display_width(state), self.rect.y + 1, state, style, max_width=room)
|
|
465
|
+
if self.choosing:
|
|
466
|
+
self._render_hosts(canvas, x, room, muted, bold, accent, surface)
|
|
467
|
+
return
|
|
468
|
+
groove = blend(theme.muted, surface, 0.55)
|
|
469
|
+
column = self._switch_column()
|
|
470
|
+
top = self._top()
|
|
471
|
+
visible = self._visible()
|
|
472
|
+
for row in range(top, min(len(ROWS_INFO), top + visible)):
|
|
473
|
+
y = self.rect.y + LIST_TOP + row - top
|
|
474
|
+
marked = row == self.mark
|
|
475
|
+
if marked:
|
|
476
|
+
canvas.draw_text(x, y, MARK, Style(foreground=theme.accent, background=surface, bold=True))
|
|
477
|
+
canvas.draw_text(
|
|
478
|
+
x + 2,
|
|
479
|
+
y,
|
|
480
|
+
ROWS_INFO[row][0],
|
|
481
|
+
bold if marked else Style(foreground=theme.text, background=surface),
|
|
482
|
+
max_width=NAME_WIDTH,
|
|
483
|
+
)
|
|
484
|
+
note = self._summary(row)
|
|
485
|
+
note_x = self._render_value(canvas, row, y, column, surface, groove, muted, quiet_style, accent, note)
|
|
486
|
+
left = self.rect.right - 2 - note_x
|
|
487
|
+
if note_x and left >= 18:
|
|
488
|
+
canvas.draw_text(
|
|
489
|
+
note_x,
|
|
490
|
+
y,
|
|
491
|
+
truncate(note, left),
|
|
492
|
+
Style(foreground=blend(theme.muted, surface, 0.15), background=surface),
|
|
493
|
+
max_width=left,
|
|
494
|
+
)
|
|
495
|
+
error = self.error
|
|
496
|
+
if error:
|
|
497
|
+
line, style = error, Style(foreground=theme.error, background=surface)
|
|
498
|
+
elif self.notice:
|
|
499
|
+
line = self.notice_now()
|
|
500
|
+
style = accent
|
|
501
|
+
else:
|
|
502
|
+
line = f"{ROWS_INFO[self.mark][0]}: {ROWS_INFO[self.mark][1]}"
|
|
503
|
+
style = muted
|
|
504
|
+
canvas.draw_text(x, self.rect.bottom - 2, truncate(line, room), style, max_width=room)
|
|
505
|
+
hint = self.HINT_ACTION if self.mark in (CONNECT, LOCAL) else self.HINT
|
|
506
|
+
canvas.draw_text(x, self.rect.bottom - 1, truncate(hint, room), muted, max_width=room)
|
|
507
|
+
|
|
508
|
+
def _visible(self) -> int:
|
|
509
|
+
"""Return how many rows fit between the title and the two lines under the list; none on a tiny panel."""
|
|
510
|
+
return max(0, self.rect.height - LIST_TOP - 2)
|
|
511
|
+
|
|
512
|
+
def _top(self) -> int:
|
|
513
|
+
"""Return the first row drawn, so the marked row stays on screen on a short terminal."""
|
|
514
|
+
visible = self._visible()
|
|
515
|
+
if self.mark < visible:
|
|
516
|
+
return 0
|
|
517
|
+
return min(self.mark - visible + 1, max(0, len(ROWS_INFO) - visible))
|
|
518
|
+
|
|
519
|
+
def _summary(self, row: int) -> str:
|
|
520
|
+
"""Return the words at the row's right, which follow the switch where they must."""
|
|
521
|
+
if row == USER and self.values[2]:
|
|
522
|
+
return "explicit account"
|
|
523
|
+
if row == SIGN_IN:
|
|
524
|
+
return "no password needed" if self.auth == "key" else "asks for a password"
|
|
525
|
+
if row == PASSWORD:
|
|
526
|
+
return "not needed" if self.auth == "key" else "this session only"
|
|
527
|
+
return SUMMARIES[row]
|
|
528
|
+
|
|
529
|
+
def _layout(self, column: int, shown: str, note: str) -> tuple[int, int]:
|
|
530
|
+
"""Return the value's width and where its note may start (0 when there is no room for a note).
|
|
531
|
+
|
|
532
|
+
The value always gets its room first: a note takes the space left when the value is short, starts one cell
|
|
533
|
+
after a value that grows into it, and is dropped rather than squeezing the value below ``MIN_VALUE``.
|
|
534
|
+
"""
|
|
535
|
+
right = self.rect.right - 2
|
|
536
|
+
available = right - column
|
|
537
|
+
if available <= 0:
|
|
538
|
+
return 0, 0
|
|
539
|
+
aligned = column + SWITCH_WIDTH + 1 + WORD_WIDTH + 1
|
|
540
|
+
wanted = max(aligned, column + min(display_width(shown), available) + 1)
|
|
541
|
+
note_x = min(wanted, right - 18)
|
|
542
|
+
if note_x - 1 - column < MIN_VALUE or right - note_x < 18 or display_width(note) < 1:
|
|
543
|
+
return available, 0
|
|
544
|
+
return note_x - 1 - column, note_x
|
|
545
|
+
|
|
546
|
+
def _render_value(
|
|
547
|
+
self,
|
|
548
|
+
canvas: Canvas,
|
|
549
|
+
row: int,
|
|
550
|
+
y: int,
|
|
551
|
+
column: int,
|
|
552
|
+
surface: object,
|
|
553
|
+
groove: object,
|
|
554
|
+
muted: Style,
|
|
555
|
+
quiet: Style,
|
|
556
|
+
accent: Style,
|
|
557
|
+
note: str,
|
|
558
|
+
) -> int:
|
|
559
|
+
theme = self.theme
|
|
560
|
+
if row == SIGN_IN:
|
|
561
|
+
word = self.rail.words[self.rail.selected]
|
|
562
|
+
start = column + SWITCH_WIDTH + 1
|
|
563
|
+
shown = f"{' ' * SWITCH_WIDTH} {word}"
|
|
564
|
+
width, note_x = self._layout(column, shown, note)
|
|
565
|
+
# The switch's word does not shrink: if the note would cut it, the note goes instead.
|
|
566
|
+
if note_x and note_x - 1 - start < display_width(word):
|
|
567
|
+
width, note_x = self.rect.right - 2 - column, 0
|
|
568
|
+
# Index 0 (key/agent) is the knob at the left and index 1 (password) at the right, so Right moves it right.
|
|
569
|
+
draw_switch(
|
|
570
|
+
canvas,
|
|
571
|
+
theme,
|
|
572
|
+
y,
|
|
573
|
+
column,
|
|
574
|
+
self.rail.position(self._now()),
|
|
575
|
+
groove, # type: ignore[arg-type]
|
|
576
|
+
)
|
|
577
|
+
style = Style(
|
|
578
|
+
foreground=theme.accent if self.rail.selected == 0 else theme.warning,
|
|
579
|
+
background=surface, # type: ignore[arg-type]
|
|
580
|
+
bold=self.mark == row,
|
|
581
|
+
)
|
|
582
|
+
room_for_word = max(0, min(width, max(0, self.rect.right - 2 - start)))
|
|
583
|
+
canvas.draw_text(start, y, word, style, max_width=room_for_word)
|
|
584
|
+
return note_x
|
|
585
|
+
if row in (CONNECT, LOCAL):
|
|
586
|
+
width, note_x = self._layout(column, "\u21b5 enter", note)
|
|
587
|
+
canvas.draw_text(
|
|
588
|
+
column,
|
|
589
|
+
y,
|
|
590
|
+
"\u21b5 enter",
|
|
591
|
+
accent if self.mark == row else quiet,
|
|
592
|
+
max_width=width,
|
|
593
|
+
)
|
|
594
|
+
return note_x
|
|
595
|
+
index = self.field(row)
|
|
596
|
+
value = self.value(row)
|
|
597
|
+
cursor = self.caret(row)
|
|
598
|
+
if row == PASSWORD:
|
|
599
|
+
# In key/agent mode the row holds nothing to edit: no dash to put a caret on.
|
|
600
|
+
shown = MASK * len(value) if self.auth == "password" else ""
|
|
601
|
+
placeholder = "(required)" if self.auth == "password" else "(none needed)"
|
|
602
|
+
else:
|
|
603
|
+
assert index is not None
|
|
604
|
+
value = self.values[index]
|
|
605
|
+
cursor = self.cursors[index]
|
|
606
|
+
placeholder = ("(type a host)", "(remote path)", "(from config)")[index]
|
|
607
|
+
shown = value
|
|
608
|
+
# A focused row asks for one cell past its value so the caret can sit after the last character instead of
|
|
609
|
+
# painting over it; an unmarked row asks for none.
|
|
610
|
+
reserve = " " if self.mark == row and row != PASSWORD and shown else ""
|
|
611
|
+
width, note_x = self._layout(column, (shown or placeholder) + reserve, note)
|
|
612
|
+
editing = self.mark == row and self.field(row) is not None
|
|
613
|
+
if editing and width:
|
|
614
|
+
# The row being edited is a field: a raised band of its own, marked at its left edge like a composer's
|
|
615
|
+
# prompt, so it is visible that typing goes here.
|
|
616
|
+
canvas.fill(column - 1, y, width + 1, 1, Style(background=theme.surface))
|
|
617
|
+
canvas.draw_text(column - 1, y, "\u258f", Style(foreground=theme.accent, background=theme.surface))
|
|
618
|
+
field_background = theme.surface if editing else surface
|
|
619
|
+
if not shown:
|
|
620
|
+
canvas.draw_text(
|
|
621
|
+
column,
|
|
622
|
+
y,
|
|
623
|
+
truncate(placeholder, width),
|
|
624
|
+
Style(foreground=theme.muted, background=field_background), # type: ignore[arg-type]
|
|
625
|
+
max_width=width,
|
|
626
|
+
)
|
|
627
|
+
start = 0
|
|
628
|
+
else:
|
|
629
|
+
# The caret wants a cell of its own: a value that exactly fills the room scrolls by one so its last
|
|
630
|
+
# character stays visible. Only the edited row scrolls; the others show the value from its first
|
|
631
|
+
# character, so a remote path is identified by the part that names it.
|
|
632
|
+
scrolls = self.mark == row and row != PASSWORD and display_width(shown) + 1 > width
|
|
633
|
+
start = max(0, cursor - width + 1) if scrolls else 0
|
|
634
|
+
body = shown[start:] if row != PASSWORD else shown
|
|
635
|
+
canvas.draw_text(
|
|
636
|
+
column,
|
|
637
|
+
y,
|
|
638
|
+
truncate(body, width),
|
|
639
|
+
Style(foreground=theme.text, background=field_background), # type: ignore[arg-type]
|
|
640
|
+
max_width=width,
|
|
641
|
+
)
|
|
642
|
+
if self.mark == row and shown:
|
|
643
|
+
at = min(max(0, width - 1), display_width(shown[start:cursor]))
|
|
644
|
+
char = (MASK if row == PASSWORD else shown[cursor : cursor + 1]) or " "
|
|
645
|
+
# The block is painted first and the character on it, because a two-cell glyph (a Chinese character)
|
|
646
|
+
# does not fit in the one-cell draw the caret used to be and disappeared behind the field's band.
|
|
647
|
+
cells = max(1, display_width(char))
|
|
648
|
+
canvas.fill(column + at, y, cells, 1, Style(background=theme.accent))
|
|
649
|
+
canvas.draw_text(
|
|
650
|
+
column + at,
|
|
651
|
+
y,
|
|
652
|
+
char,
|
|
653
|
+
Style(foreground=field_background, background=theme.accent), # type: ignore[arg-type]
|
|
654
|
+
max_width=cells,
|
|
655
|
+
)
|
|
656
|
+
return note_x
|
|
657
|
+
|
|
658
|
+
def _render_hosts(
|
|
659
|
+
self,
|
|
660
|
+
canvas: Canvas,
|
|
661
|
+
x: int,
|
|
662
|
+
room: int,
|
|
663
|
+
muted: Style,
|
|
664
|
+
bold: Style,
|
|
665
|
+
accent: Style,
|
|
666
|
+
surface: object,
|
|
667
|
+
) -> None:
|
|
668
|
+
choices = (*self.aliases, "Enter a host manually\u2026")
|
|
669
|
+
canvas.draw_text(
|
|
670
|
+
x, self.rect.y + 2, f"{len(self.aliases)} configured host(s) in ~/.ssh/config", muted, max_width=room
|
|
671
|
+
)
|
|
672
|
+
visible = self._visible()
|
|
673
|
+
for row, index in enumerate(range(self.scroll, min(len(choices), self.scroll + visible))):
|
|
674
|
+
y = self.rect.y + LIST_TOP + row
|
|
675
|
+
selected = index == self.choice
|
|
676
|
+
if selected:
|
|
677
|
+
canvas.draw_text(x, y, MARK, accent)
|
|
678
|
+
canvas.draw_text(
|
|
679
|
+
x + 2, y, truncate(choices[index], room - 2), bold if selected else muted, max_width=room - 2
|
|
680
|
+
)
|
|
681
|
+
canvas.draw_text(x, self.rect.bottom - 2, "Host: the host to connect to", muted, max_width=room)
|
|
682
|
+
canvas.draw_text(x, self.rect.bottom - 1, self.HINT_HOSTS, muted, max_width=room)
|