Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

69 lines
2.2 KiB
Python

"""Terminal-friendly shell command display helpers."""
from __future__ import annotations
import re
_HEREDOC_DELIMITER_RE = re.compile(r"<<(-)?\s*(?:'([^'\n]+)'|\"([^\"\n]+)\"|([^\s\\|;&<>]+))")
def _delimiter_from_match(match: re.Match[str]) -> tuple[str, bool]:
strip_tabs = match.group(1) == "-"
delimiter = match.group(2) or match.group(3) or match.group(4) or ""
return delimiter, strip_tabs
def _closing_delimiter_line(line: str, *, delimiter: str, strip_tabs: bool) -> bool:
normalized = line.rstrip("\r\n")
if strip_tabs:
normalized = normalized.lstrip("\t")
return normalized == delimiter
def format_shell_command_for_display(command: str) -> str:
"""Return a compact, user-facing command string for the REPL prompt area.
Heredoc bodies (for example the Python script in ``python3 - <<'PY'``) are
collapsed to a single summary line so incidental agent-generated scripts do
not flood the terminal. The full ``command`` is still executed unchanged.
"""
lines = command.splitlines()
if len(lines) <= 1:
return command
display_lines: list[str] = []
index = 0
while index < len(lines):
line = lines[index]
match = _HEREDOC_DELIMITER_RE.search(line)
if match is None:
display_lines.append(line)
index += 1
continue
delimiter, strip_tabs = _delimiter_from_match(match)
body_start = index + 1
close_index: int | None = None
for candidate_index in range(body_start, len(lines)):
if _closing_delimiter_line(
lines[candidate_index],
delimiter=delimiter,
strip_tabs=strip_tabs,
):
close_index = candidate_index
break
body_line_count = 0 if close_index is None else close_index - body_start
if body_line_count <= 0:
summary = f"{line} …"
else:
noun = "line" if body_line_count == 1 else "lines"
summary = f"{line} … ({body_line_count} {noun})"
display_lines.append(summary)
index = len(lines) if close_index is None else close_index + 1
return "\n".join(display_lines)
__all__ = ["format_shell_command_for_display"]