summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/lib/views/wire.py
blob: 38b5e8ecbbb4a54d53649920a32fe1a7e17ea8d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""A scan as the views read it, with nothing in it that does not print."""

from __future__ import annotations

import json
import struct
import unicodedata
from typing import TYPE_CHECKING, cast

from lib import display, strictjson

if TYPE_CHECKING:
    from collections.abc import Sequence

    from lib.live.atlas import Atlas
    from lib.live.scan import Scan

_MOST_CHARS = 64 * 1024


def _printable(value: object) -> object:
    if isinstance(value, str):
        return "\n".join(map(display.written_out, display.capped(value, _MOST_CHARS).splitlines()))
    if strictjson.is_map(value):
        return {key: _printable(held) for key, held in value.items()}
    if isinstance(value, (list, tuple)):
        return [_printable(held) for held in cast("Sequence[object]", value)]
    return value


def _line(value: object) -> str:
    return json.dumps(value, separators=(",", ":"), ensure_ascii=False)


def _joins_the_last(ch: str) -> bool:
    # Hangul's conjoining vowels and finals are letters, but take no cell of their own.
    mark = unicodedata.category(ch) in {"Mn", "Me"}
    return mark or "\u1160" <= ch <= "\u11ff" or "\ud7b0" <= ch <= "\ud7ff"


def atlas(tree: Atlas) -> bytes:
    """Each plot's path, line count and outline."""
    names = (display.written_out(plot.path).encode()[:0xFFFF] for plot in tree.plots)
    return struct.pack("<I", len(tree.plots)) + b"".join(
        struct.pack("<H", len(name))
        + name
        + struct.pack("<IH", min(plot.lines, 0xFFFFFFFF), len(plot.outline) // 2)
        + plot.outline
        for name, plot in zip(names, tree.plots)
    )


def delta(scan: Scan, since: int) -> str:
    """Scan.delta(since) on one line, with the characters that do not take one terminal cell."""
    change = {key: _printable(held) for key, held in scan.delta(since).items()}
    text = _line(change)
    paths = "" if since else "".join(plot.path for plot in scan.atlas.plots)
    if text.isascii() and paths.isascii():
        return text
    seen = sorted(ch for ch in {*text, *paths} if ch.isprintable())
    wide = "".join(ch for ch in seen if unicodedata.east_asian_width(ch) in {"W", "F"})
    marks = "".join(filter(_joins_the_last, seen))
    return _line({**change, "wide": wide, "marks": marks})