Codebergdnkl

foot

master
ply5 git calls1.4 s25 KiB3 cachedprotocol v2
271 lines6.7 KiBRaw
1#include "extract.h"
2#include <string.h>
4#define LOG_MODULE "extract"
5#define LOG_ENABLE_DBG 0
6#include "log.h"
7#include "char32.h"
9struct extraction_context {
10 char32_t *buf;
11 size_t size;
12 size_t idx;
13 size_t tab_spaces_left;
14 size_t empty_count;
15 size_t newline_count;
16 bool strip_trailing_empty;
17 bool failed;
18 const struct row *last_row;
19 const struct cell *last_cell;
20 enum selection_kind selection_kind;
21};
23struct extraction_context *
24extract_begin(enum selection_kind kind, bool strip_trailing_empty)
26 struct extraction_context *ctx = malloc(sizeof(*ctx));
27 if (unlikely(ctx == NULL)) {
28 LOG_ERRNO("malloc() failed");
29 return NULL;
30 }
32 *ctx = (struct extraction_context){
33 .selection_kind = kind,
34 .strip_trailing_empty = strip_trailing_empty,
35 };
36 return ctx;
39static bool
40ensure_size(struct extraction_context *ctx, size_t additional_chars)
42 while (ctx->size < ctx->idx + additional_chars) {
43 size_t new_size = ctx->size == 0 ? 512 : ctx->size * 2;
44 char32_t *new_buf = realloc(ctx->buf, new_size * sizeof(new_buf[0]));
46 if (new_buf == NULL)
47 return false;
49 ctx->buf = new_buf;
50 ctx->size = new_size;
51 }
53 xassert(ctx->size >= ctx->idx + additional_chars);
54 return true;
57bool
58extract_finish_wide(struct extraction_context *ctx, char32_t **text, size_t *len)
60 if (text == NULL)
61 return false;
63 *text = NULL;
64 if (len != NULL)
65 *len = 0;
67 if (ctx->failed)
68 goto err;
70 if (!ctx->strip_trailing_empty) {
71 /* Insert pending newlines, and replace empty cells with spaces */
72 if (!ensure_size(ctx, ctx->newline_count + ctx->empty_count))
73 goto err;
75 for (size_t i = 0; i < ctx->newline_count; i++)
76 ctx->buf[ctx->idx++] = U'\n';
78 for (size_t i = 0; i < ctx->empty_count; i++)
79 ctx->buf[ctx->idx++] = U' ';
80 }
82 if (ctx->idx == 0) {
83 /* Selection of empty cells only */
84 if (!ensure_size(ctx, 1))
85 goto err;
86 ctx->buf[ctx->idx++] = U'\0';
87 } else {
88 xassert(ctx->idx > 0);
89 xassert(ctx->idx <= ctx->size);
91 switch (ctx->selection_kind) {
92 default:
93 if (ctx->buf[ctx->idx - 1] == U'\n')
94 ctx->buf[ctx->idx - 1] = U'\0';
95 break;
97 case SELECTION_LINE_WISE:
98 if (ctx->buf[ctx->idx - 1] != U'\n') {
99 if (!ensure_size(ctx, 1))
100 goto err;
101 ctx->buf[ctx->idx++] = U'\n';
102 }
103 break;
105 }
107 if (ctx->buf[ctx->idx - 1] != U'\0') {
108 if (!ensure_size(ctx, 1))
109 goto err;
110 ctx->buf[ctx->idx++] = U'\0';
111 }
112 }
114 *text = ctx->buf;
115 if (len != NULL)
116 *len = ctx->idx - 1;
117 free(ctx);
118 return true;
120err:
121 free(ctx->buf);
122 free(ctx);
123 return false;
126bool
127extract_finish(struct extraction_context *ctx, char **text, size_t *len)
129 if (text == NULL)
130 return false;
131 if (len != NULL)
132 *len = 0;
134 char32_t *wtext;
135 if (!extract_finish_wide(ctx, &wtext, NULL))
136 return false;
138 bool ret = false;
140 *text = ac32tombs(wtext);
141 if (*text == NULL) {
142 LOG_ERR("failed to convert selection to UTF-8");
143 goto out;
144 }
146 if (len != NULL)
147 *len = strlen(*text);
148 ret = true;
150out:
151 free(wtext);
152 return ret;
155bool
156extract_one(const struct terminal *term, const struct row *row,
157 const struct cell *cell, int col, void *context)
159 struct extraction_context *ctx = context;
161 if (cell->wc >= CELL_SPACER)
162 return true;
164 if (ctx->last_row != NULL && row != ctx->last_row) {
165 /* New row - determine if we should insert a newline or not */
167 if (ctx->selection_kind != SELECTION_BLOCK) {
168 if (ctx->last_row->linebreak ||
169 ctx->empty_count > 0 ||
170 cell->wc == 0)
171 {
172 /* Row has a hard linebreak, or either last cell or
173 * current cell is empty */
175 /* Don't emit newline just yet - only if there are
176 * non-empty cells following it */
177 ctx->newline_count++;
179 if (!ctx->strip_trailing_empty) {
180 if (!ensure_size(ctx, ctx->empty_count))
181 goto err;
182 for (size_t i = 0; i < ctx->empty_count; i++)
183 ctx->buf[ctx->idx++] = U' ';
184 }
185 ctx->empty_count = 0;
186 }
187 } else {
188 /* Always insert a linebreak */
189 if (!ensure_size(ctx, 1))
190 goto err;
192 ctx->buf[ctx->idx++] = U'\n';
194 if (!ctx->strip_trailing_empty) {
195 if (!ensure_size(ctx, ctx->empty_count))
196 goto err;
197 for (size_t i = 0; i < ctx->empty_count; i++)
198 ctx->buf[ctx->idx++] = U' ';
199 }
200 ctx->empty_count = 0;
201 }
203 ctx->tab_spaces_left = 0;
204 }
206 if (cell->wc == U' ' && ctx->tab_spaces_left > 0) {
207 ctx->tab_spaces_left--;
208 return true;
209 }
211 ctx->tab_spaces_left = 0;
213 if (cell->wc == 0) {
214 ctx->empty_count++;
215 ctx->last_row = row;
216 ctx->last_cell = cell;
217 return true;
218 }
220 /* Insert pending newlines, and replace empty cells with spaces */
221 if (!ensure_size(ctx, ctx->newline_count + ctx->empty_count))
222 goto err;
224 for (size_t i = 0; i < ctx->newline_count; i++)
225 ctx->buf[ctx->idx++] = U'\n';
227 for (size_t i = 0; i < ctx->empty_count; i++)
228 ctx->buf[ctx->idx++] = U' ';
230 ctx->newline_count = 0;
231 ctx->empty_count = 0;
233 if (cell->wc >= CELL_COMB_CHARS_LO && cell->wc <= CELL_COMB_CHARS_HI)
234 {
235 const struct composed *composed = composed_lookup(
236 term->composed, cell->wc - CELL_COMB_CHARS_LO);
238 if (!ensure_size(ctx, composed->count))
239 goto err;
241 for (size_t i = 0; i < composed->count; i++)
242 ctx->buf[ctx->idx++] = composed->chars[i];
243 }
245 else {
246 if (!ensure_size(ctx, 1))
247 goto err;
248 ctx->buf[ctx->idx++] = cell->wc;
250 if (cell->wc == U'\t') {
251 int next_tab_stop = term->cols - 1;
252 tll_foreach(term->tab_stops, it) {
253 if (it->item > col) {
254 next_tab_stop = it->item;
255 break;
256 }
257 }
259 if (next_tab_stop > col)
260 ctx->tab_spaces_left = next_tab_stop - col - 1;
261 }
262 }
264 ctx->last_row = row;
265 ctx->last_cell = cell;
266 return true;
268err:
269 ctx->failed = true;
270 return false;
Diagnostics5 git calls · 1.4 s
CallStatusSizeTime01.4 s
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 KiB1.4 s
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=931
git.response.bytes=10322
git.response.cache=EXPIRED
git.response.duration_ms=1375
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 · hit8 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=8
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=8
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=10
Open pack map
fetchtree:02003.6 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=3708
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=tree:0
git.fetch.wants=96f1228f4004ebd74530c7f0aea264108eff425f
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=3708
git.pkt.payload.max=3681
git.sideband.band1.bytes=3681
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=3681
git.pack.trailer=731a22f61f25bdfdca1691647f2056631bcfdcf1
Open pack map
fetch2001.7 KiB · hit21 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=21
git.response.bytes=1743
git.response.cache=HIT
git.response.duration_ms=21
git.fetch.wants=cd9a0c95bfd123026ed156f2ce28e8f6834bbc5e
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=1743
git.pkt.payload.max=1716
git.sideband.band1.bytes=1716
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
a5eaee686c0b14c2
Request
801b593c…
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