Files
strukto-ai--mirage/python/mirage/commands/builtin/generic/cat.py
T
wehub-resource-sync bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:44 +08:00

83 lines
2.6 KiB
Python

from collections.abc import AsyncIterator
from mirage.utils.stream import ensure_stream
def _visible(line: bytes, show_tabs: bool, show_nonprinting: bool) -> bytes:
"""Render a line GNU cat -T / -v style.
Tabs become ^I under -T; under -v control bytes become ^X, DEL
becomes ^?, and high bytes get the M- prefix with the same rules
applied to the low seven bits. Newlines never appear here (the
caller splits on them).
Args:
line (bytes): one line without its trailing newline.
show_tabs (bool): -T, render tab as ^I.
show_nonprinting (bool): -v, render control and high bytes.
"""
out = bytearray()
for byte in line:
if byte == 9:
out += b"^I" if show_tabs else b"\t"
elif not show_nonprinting:
out.append(byte)
elif byte < 32:
out += bytes((94, byte + 64))
elif byte == 127:
out += b"^?"
elif byte >= 128:
out += b"M-"
low = byte - 128
if low < 32:
out += bytes((94, low + 64))
elif low == 127:
out += b"^?"
else:
out.append(low)
else:
out.append(byte)
return bytes(out)
async def cat(
src: bytes | AsyncIterator[bytes],
*,
number_lines: bool = False,
show_ends: bool = False,
squeeze_blank: bool = False,
show_tabs: bool = False,
show_nonprinting: bool = False,
) -> AsyncIterator[bytes]:
needs_line_processing = (number_lines or show_ends or squeeze_blank
or show_tabs or show_nonprinting)
if not needs_line_processing:
async for chunk in ensure_stream(src):
yield chunk
return
line_no = 0
buf = b""
prev_blank = False
async for chunk in ensure_stream(src):
buf += chunk
while b"\n" in buf:
line, buf = buf.split(b"\n", 1)
if squeeze_blank and not line and prev_blank:
prev_blank = True
continue
line_no += 1
prefix = f"{line_no:6d}\t".encode() if number_lines else b""
suffix = b"$\n" if show_ends else b"\n"
if show_tabs or show_nonprinting:
line = _visible(line, show_tabs, show_nonprinting)
yield prefix + line + suffix
prev_blank = not line
if buf:
line_no += 1
prefix = f"{line_no:6d}\t".encode() if number_lines else b""
if show_tabs or show_nonprinting:
buf = _visible(buf, show_tabs, show_nonprinting)
yield prefix + buf