Codebergdnkl
foot
master
1#!/usr/bin/env python3
2import argparse
3import enum
4import fcntl
5import random
6import signal
7import struct
8import sys
9import termios
10from typing import Any
13class ColorVariant(enum.IntEnum):
14 NONE = enum.auto()
15 REGULAR = enum.auto()
16 BRIGHT = enum.auto()
17 CUBE = enum.auto()
18 RGB = enum.auto()
21def main() -> None:
22 parser = argparse.ArgumentParser()
23 parser.add_argument(
24 'out', type=argparse.FileType(mode='w'), nargs='?', help='name of output file')
25 parser.add_argument('--cols', type=int)
26 parser.add_argument('--rows', type=int)
27 parser.add_argument('--colors-regular', action='store_true')
28 parser.add_argument('--colors-bright', action='store_true')
29 parser.add_argument('--colors-256', action='store_true')
30 parser.add_argument('--colors-rgb', action='store_true')
31 parser.add_argument('--scroll', action='store_true')
32 parser.add_argument('--scroll-region', action='store_true')
33 parser.add_argument('--attr-bold', action='store_true')
34 parser.add_argument('--attr-italic', action='store_true')
35 parser.add_argument('--attr-underline', action='store_true')
36 parser.add_argument('--sixel', action='store_true')
37 parser.add_argument('--seed', type=int)
39 opts = parser.parse_args()
40 out = opts.out if opts.out is not None else sys.stdout
42 lines: int | None = None
43 cols: int | None = None
44 width: int | None = None
45 height: int | None = None
47 if opts.rows is None or opts.cols is None:
48 try:
49 def dummy(*args: Any) -> None:
50 """Need a handler installed for sigwait() to trigger."""
51 _ = args
52 signal.signal(signal.SIGWINCH, dummy)
54 while True:
55 with open('/dev/tty', 'rb') as pty:
56 lines, cols, height, width = struct.unpack(
57 'HHHH',
58 fcntl.ioctl(pty,
59 termios.TIOCGWINSZ,
60 struct.pack('HHHH', 0, 0, 0, 0)))
62 assert width is not None
63 assert height is not None
65 if width > 0 and height > 0:
66 break
68 # We’re early; the foot window hasn’t been mapped yet. Or,
69 # to be more precise, fonts haven’t yet been loaded,
70 # meaning it doesn’t have any cell geometry yet.
71 signal.sigwait([signal.SIGWINCH])
73 signal.signal(signal.SIGWINCH, signal.SIG_DFL)
75 except OSError:
76 lines = None
77 cols = None
78 height = None
79 width = None
81 if opts.rows is not None:
82 lines = opts.rows
83 assert lines is not None
84 height = 15 * lines # PGO helper binary hardcodes cell height to 15px
85 if opts.cols is not None:
86 cols = opts.cols
87 assert cols is not None
88 width = 8 * cols # PGO help binary hardcodes cell width to 8px
90 if lines is None or cols is None or height is None or width is None:
91 raise ValueError('could not get terminal width/height; use --rows and --cols')
93 assert lines > 0, f'{lines}'
94 assert cols > 0, f'{cols}'
95 assert width > 0, f'{width}'
96 assert height > 0, f'{height}'
98 # Number of characters to write to screen
99 count = 256 * 1024**1
101 # Characters to choose from
102 alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSTUVWXYZ0123456789 öäå 👨👩🧒👩🏽🔬🇸🇪'
104 color_variants = ([ColorVariant.NONE] +
105 ([ColorVariant.REGULAR] if opts.colors_regular else []) +
106 ([ColorVariant.BRIGHT] if opts.colors_bright else []) +
107 ([ColorVariant.CUBE] if opts.colors_256 else []) +
108 ([ColorVariant.RGB] if opts.colors_rgb else []))
110 # Enter alt screen
111 out.write('\033[?1049h')
113 # uses system time or /dev/urandom if available if opt.seed == None
114 # pin seeding method to make seeding stable across future versions
115 random.seed(a=opts.seed, version=2)
117 for _ in range(count):
118 if opts.scroll and random.randrange(256) == 0:
119 out.write('\033[m')
121 if opts.scroll_region and random.randrange(256) == 0:
122 top = random.randrange(3)
123 bottom = random.randrange(3)
124 out.write(f'\033[{top};{lines - bottom}r')
126 lines_to_scroll = random.randrange(lines - 1)
127 rev = random.randrange(2)
128 if not rev and random.randrange(2):
129 out.write(f'\033[{lines};{cols}H')
130 out.write('\n' * lines_to_scroll)
131 else:
132 out.write(f'\033[{lines_to_scroll + 1}{"T" if rev == 1 else "S"}')
133 continue
135 # Generate a random location and a random character
136 row = random.randrange(lines)
137 col = random.randrange(cols)
138 c = random.choice(alphabet)
140 repeat = random.randrange((cols - col) + 1)
141 assert col + repeat <= cols
143 color_variant = random.choice(color_variants)
145 # Position cursor
146 out.write(f'\033[{row + 1};{col + 1}H')
148 if color_variant in [ColorVariant.REGULAR, ColorVariant.BRIGHT]:
149 do_bg = random.randrange(2)
150 base = 40 if do_bg else 30
151 base += 60 if color_variant == ColorVariant.BRIGHT else 0
153 idx = random.randrange(8)
154 out.write(f'\033[{base + idx}m')
156 elif color_variant == ColorVariant.CUBE:
157 do_bg = random.randrange(2)
158 base = 48 if do_bg else 38
160 idx = random.randrange(256)
161 if random.randrange(2):
162 # Old-style
163 out.write(f'\033[{base};5;{idx}m')
164 else:
165 # New-style (sub-parameter based)
166 out.write(f'\033[{base}:5:{idx}m')
168 elif color_variant == ColorVariant.RGB:
169 do_bg = random.randrange(2)
170 base = 48 if do_bg else 38
172 # use list comprehension in favor of randbytes(n)
173 # which is only available for Python >= 3.9
174 rgb = [random.randrange(256) for _ in range(3)]
176 if random.randrange(2):
177 # Old-style
178 out.write(f'\033[{base};2;{rgb[0]};{rgb[1]};{rgb[2]}m')
179 else:
180 # New-style (sub-parameter based)
181 out.write(f'\033[{base}:2::{rgb[0]}:{rgb[1]}:{rgb[2]}m')
183 if opts.attr_bold and random.randrange(5) == 0:
184 out.write('\033[1m')
185 if opts.attr_italic and random.randrange(5) == 0:
186 out.write('\033[3m')
187 if opts.attr_underline and random.randrange(5) == 0:
188 out.write('\033[4m')
190 out.write(c * repeat)
192 do_sgr_reset = random.randrange(2)
193 if do_sgr_reset:
194 reset_actions = ['\033[m', '\033[39m', '\033[49m']
195 out.write(random.choice(reset_actions))
197 # Reset colors
198 out.write('\033[m\033[r')
200 if opts.sixel:
201 # The sixel 'alphabet'
202 sixels = '?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
204 last_pos: tuple[int, int] | None = None
205 last_size: tuple[int, int] = 0, 0
207 for _ in range(20):
208 if last_pos is not None and random.randrange(2):
209 # Overwrite last sixel. I.e. use same position and
210 # size as last sixel
211 pass
212 else:
213 # Random origin in upper left quadrant
214 last_pos = random.randrange(lines // 2) + 1, random.randrange(cols // 2) + 1
215 last_size = random.randrange((height + 1) // 2), random.randrange((width + 1) // 2)
217 out.write(f'\033[{last_pos[0]};{last_pos[1]}H')
218 six_height, six_width = last_size
219 six_rows = (six_height + 5) // 6 # Round up; each sixel is 6 pixels
221 # Begin sixel (with P2 set to either 0 or 1 - opaque or transparent)
222 sixel_p2 = random.randrange(2)
223 out.write(f'\033P;{sixel_p2}q')
225 # Sixel size. Without this, sixels will be
226 # auto-resized on cell-boundaries.
227 out.write(f'"1;1;{six_width};{six_height}')
229 # Set up 256 random colors
230 for idx in range(256):
231 # param 2: 1=HLS, 2=RGB.
232 # param 3/4/5: HLS/RGB values in range 0-100
233 # (except 'hue' which is 0..360)
234 out.write(f'#{idx};2;{random.randrange(101)};{random.randrange(101)};{random.randrange(101)}')
236 for row in range(six_rows):
237 band_count = random.randrange(4, 33)
238 for band in range(band_count):
239 # Choose a random color
240 out.write(f'#{random.randrange(256)}')
242 if random.randrange(2):
243 for col in range(six_width):
244 out.write(f'{random.choice(sixels)}')
245 else:
246 pix_left = six_width
247 while pix_left > 0:
248 repeat_count = random.randrange(1, pix_left + 1)
249 out.write(f'!{repeat_count}{random.choice(sixels)}')
250 pix_left -= repeat_count
252 # Next line
253 if band + 1 < band_count:
254 # Move cursor to beginning of current row
255 out.write('$')
256 elif row + 1 < six_rows:
257 # Newline
258 out.write('-')
260 # End sixel
261 out.write('\033\\')
263 # Leave alt screen
264 out.write('\033[?1049l')
267if __name__ == '__main__':
268 main()
Diagnostics5 git calls · 583 ms
CallStatusSizeTime0583 ms
discover200178 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=10322 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchtree:0 · deepen 102009.3 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetch2002.9 KiB577 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=577 git.response.bytes=2948 git.response.cache=MISS git.response.duration_ms=577 git.fetch.wants=bebe4b0e1f12150504aa781b61a846840239dbbe git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2948 git.pkt.payload.max=2921 git.sideband.band1.bytes=2921 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e14da1f807a140
- Request
- 5a3ec64f…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format