Load local sessions in the background for the Sessions popup
command_choose ran `herdr session list --json` (up to its 5 s timeout) before the popup drew anything, contradicting the documented immediate display. Run the listing on a worker thread and hand the selector a Future: the popup appears at once with a "… 本机 正在查询" placeholder, then fills in the local rows and any discovery warning when the result arrives, mirroring how remote hosts already load. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -196,6 +196,7 @@ def build_main_rows(
|
||||
loading_targets: set[str],
|
||||
*,
|
||||
local_sessions: Iterable[str] = (),
|
||||
local_loading: bool = False,
|
||||
registry_error: str | None = None,
|
||||
) -> list[PickerRow]:
|
||||
window_list = list(windows)
|
||||
@@ -239,6 +240,16 @@ def build_main_rows(
|
||||
)
|
||||
for session in available_local_sessions
|
||||
)
|
||||
if local_loading:
|
||||
rows.append(
|
||||
PickerRow(
|
||||
"loading:local",
|
||||
"loading",
|
||||
"… 本机",
|
||||
"正在查询",
|
||||
selectable=False,
|
||||
)
|
||||
)
|
||||
|
||||
remote_windows = {
|
||||
(window.target, window.session): window
|
||||
@@ -525,9 +536,13 @@ class SessionSelector:
|
||||
delete_session_handler: DeleteSessionHandler | None = None,
|
||||
stop_session_handler: StopSessionHandler | None = None,
|
||||
activate_handler: ActivateHandler | None = None,
|
||||
pending_local_sessions: "Future[tuple[Iterable[str], Iterable[str]]] | None" = None,
|
||||
) -> None:
|
||||
self.windows = list(windows)
|
||||
self.local_sessions = list(local_sessions)
|
||||
# Local discovery runs `herdr session list` (up to 5 s); the popup
|
||||
# draws immediately and fills these rows in when the result arrives.
|
||||
self.pending_local_sessions = pending_local_sessions
|
||||
self.aliases = dict(aliases or {})
|
||||
self.activate_handler = activate_handler
|
||||
self.rename_handler = rename_handler
|
||||
@@ -602,6 +617,7 @@ class SessionSelector:
|
||||
while True:
|
||||
for result in self.pool.poll():
|
||||
self.results[result.entry.target] = result
|
||||
self._poll_local_sessions()
|
||||
self._poll_action()
|
||||
rows = self._rows()
|
||||
filtered = (
|
||||
@@ -653,9 +669,28 @@ class SessionSelector:
|
||||
self.results,
|
||||
{target for target in self.entries if self.pool.is_loading(target)},
|
||||
local_sessions=self.local_sessions,
|
||||
local_loading=self.pending_local_sessions is not None,
|
||||
registry_error=self.registry_error,
|
||||
)
|
||||
|
||||
def _poll_local_sessions(self) -> None:
|
||||
future = self.pending_local_sessions
|
||||
if future is None or not future.done():
|
||||
return
|
||||
self.pending_local_sessions = None
|
||||
try:
|
||||
sessions, warnings = future.result()
|
||||
except Exception as error: # noqa: BLE001 - shown inside the popup
|
||||
self._append_message(f"本机 Session 查询失败:{error}")
|
||||
return
|
||||
self.local_sessions = list(sessions)
|
||||
warning_list = list(warnings)
|
||||
if warning_list:
|
||||
self._append_message("; ".join(warning_list))
|
||||
|
||||
def _append_message(self, text: str) -> None:
|
||||
self.message = "; ".join(part for part in (self.message, text) if part)
|
||||
|
||||
def _confirmation_rows(self) -> list[PickerRow]:
|
||||
target = self.input_target or ""
|
||||
session = self.input_session or ""
|
||||
|
||||
Reference in New Issue
Block a user