File size: 8,012 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import datetime
import html
import math
import sys
import time as _time
import traceback

from telethon.custom import Message
from telethon.extensions import html as _telethon_html

from custom.entities import normalize_parsed


class FmtMessage(str):
    """String-compatible formatter result sent through Layer 230 as HTML."""

    __slots__ = ()
    __rich_format__ = "html"


class FmtKit:
    """Formatted-message builders (bars, bytes, exception blocks).

    One instance (``fmt_kit``); the module-level functions below are
    backward-compatible aliases.
    """

    def fmtbar(
        self,
        current: int,
        total: int,
        bars: int = 10,
        empty: str = "-",
        fill: str = "#",
    ) -> str:
        bars = int(bars)
        if bars < 1:
            raise ValueError("bars must be at least 1")
        current, total = float(current), float(total)
        if not math.isfinite(current) or not math.isfinite(total) or total < 0:
            raise ValueError("current and total must be finite; total cannot be negative")
        ratio = min(1.0, max(0.0, current / total if total else 0.0))
        filled = round(ratio * bars)
        percent = f"{ratio * 100:.2f}".rstrip("0").rstrip(".")
        return f"[ {fill * filled + empty * (bars - filled)} ] {percent}%"

    def fmtbyte(self, byte: int | float, si: bool = False) -> str:
        byte = float(byte)
        if not math.isfinite(byte):
            raise ValueError("byte count must be finite")
        sign = "-" if byte < 0 else ""
        byte = abs(byte)
        if si:
            units = (
                ("TB", 1000**4),
                ("GB", 1000**3),
                ("MB", 1000**2),
                ("KB", 1000),
                ("B", 1),
            )
        else:
            units = (
                ("TiB", 1024**4),
                ("GiB", 1024**3),
                ("MiB", 1024**2),
                ("KiB", 1024),
                ("B", 1),
            )

        for unit, factor in units:
            if byte >= factor or factor == 1:
                value = f"{(byte / factor):.2f}".rstrip("0").rstrip(".")
                return f"{sign}{value} {unit}"

    def fmtexc(self) -> str:
        exc = traceback.TracebackException(*sys.exc_info())
        fmt = exc.exc_type.__name__
        if exc._str:
            fmt += f":\n  {exc._str}"

        ftb = traceback.format_list(
            f for f in exc.stack if "/site-packages/" in f.filename
        )
        if ftb:
            fmt += f"\n\nTraceback:\n{''.join(ftb)}"

        return fmt

    def _fmt_value(self, v) -> str:
        if isinstance(v, rawhtml):
            return str(v)
        if isinstance(v, (tuple, list)) and len(v) == 2:
            text, entities = normalize_parsed(v)
            return _telethon_html.unparse(text, entities)
        return f"<code>{html.escape(str(v))}</code>"

    def _escape_or_raw(self, value) -> str:
        if isinstance(value, rawhtml):
            return str(value)
        if isinstance(value, (tuple, list)) and len(value) == 2:
            text, entities = normalize_parsed(value)
            return _telethon_html.unparse(text, entities)
        return html.escape(str(value))

    def fmtmsg(
        self, head: str, data: object = None, foot: str = "", msgs: str = ""
    ) -> str:
        body = ""
        if isinstance(data, dict):
            padd = max((len(str(k)) for k in data.keys()), default=0)
            body = "\n".join(
                f"  <code>{html.escape(str(k)).ljust(padd)}</code> : {self._fmt_value(v)}"
                for k, v in data.items()
            )
        elif isinstance(data, (list, set, tuple)):
            body = "\n".join(
                f"  <code>{n}</code>. {self._fmt_value(item)}"
                for n, item in enumerate(data, start=1)
            )
        elif data:
            body = f"  <code>{html.escape(str(data))}</code>"

        text = [f"<b>{self._escape_or_raw(head)}</b>"]
        if body:
            text.append(body)

        if msgs:
            text.append(f"<blockquote expandable>{self._escape_or_raw(msgs)}</blockquote>")

        if foot:
            text.append(f"<b><blockquote>{self._escape_or_raw(foot)}</blockquote></b>")

        return FmtMessage("\n\n".join(text))

    def fmtsec(self, sec: object, part: int = 3, human: bool = False) -> str:
        if isinstance(sec, datetime.timedelta):
            total_seconds = sec.total_seconds()
        elif isinstance(sec, datetime.datetime):
            total_seconds = (
                datetime.datetime.now(datetime.UTC) - sec.astimezone(datetime.UTC)
            ).total_seconds()
        elif isinstance(sec, (float, int)):
            total_seconds = float(sec)
        else:
            raise TypeError("sec must be a timedelta, datetime, or number")

        if not math.isfinite(total_seconds):
            raise ValueError("duration must be finite")
        part = int(part)
        if part < 1:
            raise ValueError("part must be at least 1")
        negative = total_seconds < 0
        total_microseconds = round(abs(total_seconds) * 1_000_000)
        total, micro = divmod(total_microseconds, 1_000_000)
        units = (
            ("week", 60**2 * 24 * 7),
            ("day", 60**2 * 24),
            ("hour", 60**2),
            ("minute", 60),
            ("second", 1),
        )
        parts = []
        for unit, second in units:
            value, total = divmod(total, second)
            if value:
                parts.append(f"{value} {unit}{'' if value == 1 else 's'}")

            if len(parts) >= part:
                break

        if len(parts) < part and not human:
            ms, us = divmod(micro, 1000)
            if ms:
                parts.append(f"{ms} ms")

            if us and len(parts) < part:
                parts.append(f"{us} µs")

        result = ", ".join(parts) if parts else "0 seconds"
        return f"-{result}" if negative else result

    async def prog(
        self, current: int, total: int, event: Message, title: str = ""
    ) -> None:
        try:
            now = event._client.loop.time()
        except (AttributeError, Exception):
            now = _time.time()

        if not hasattr(event, "_start"):
            event._start = now
            event._last = now
            return

        if now - event._last >= 2.5:
            delta = now - event._start
            speed = current / delta if delta > 0 else 0
            text_to_send = fmtmsg(
                f"{title.title()} Progress".lstrip(),
                {
                    "Current": fmtbyte(current),
                    "Total": f"{fmtbyte(total)}\n",
                    "Speed": f"{fmtbyte(speed)}/s\n",
                    "Elapsed": fmtsec(delta, human=True),
                    "Estimated": fmtsec(
                        (total - current) / speed if speed > 0 else 0, human=True
                    ),
                },
                fmtbar(current, total),
            )

            try:
                await event.edit(text_to_send, parse_mode="html")
            except Exception:
                pass

            event._last = now


fmt_kit = FmtKit()


def fmtbar(*args, **kwargs):
    return fmt_kit.fmtbar(*args, **kwargs)


def fmtbyte(*args, **kwargs):
    return fmt_kit.fmtbyte(*args, **kwargs)


def fmtexc(*args, **kwargs):
    return fmt_kit.fmtexc(*args, **kwargs)


def _fmt_value(*args, **kwargs):
    return fmt_kit._fmt_value(*args, **kwargs)


def fmtmsg(*args, **kwargs):
    return fmt_kit.fmtmsg(*args, **kwargs)


def fmtsec(*args, **kwargs):
    return fmt_kit.fmtsec(*args, **kwargs)


def prog(*args, **kwargs):
    return fmt_kit.prog(*args, **kwargs)


class rawhtml(str):
    """Marks a string as pre-built, already-safe HTML.

    ``fmtmsg`` renders ``rawhtml`` values verbatim (no escaping, no
    ``<code>`` wrap), so callers can pass working ``<a href>`` links inside
    ``data`` - plain values keep the escaped ``<code>`` treatment.
    """