mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-16 05:34:00 -04:00
extract: move ‘strip_trailing_empty’ parameter from extra_finish() to extract_begin()
This commit is contained in:
parent
0945e71572
commit
a6d9f01c0d
6 changed files with 32 additions and 19 deletions
28
extract.c
28
extract.c
|
|
@ -11,6 +11,7 @@ struct extraction_context {
|
||||||
size_t idx;
|
size_t idx;
|
||||||
size_t empty_count;
|
size_t empty_count;
|
||||||
size_t newline_count;
|
size_t newline_count;
|
||||||
|
bool strip_trailing_empty;
|
||||||
bool failed;
|
bool failed;
|
||||||
const struct row *last_row;
|
const struct row *last_row;
|
||||||
const struct cell *last_cell;
|
const struct cell *last_cell;
|
||||||
|
|
@ -18,7 +19,7 @@ struct extraction_context {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct extraction_context *
|
struct extraction_context *
|
||||||
extract_begin(enum selection_kind kind)
|
extract_begin(enum selection_kind kind, bool strip_trailing_empty)
|
||||||
{
|
{
|
||||||
struct extraction_context *ctx = malloc(sizeof(*ctx));
|
struct extraction_context *ctx = malloc(sizeof(*ctx));
|
||||||
if (unlikely(ctx == NULL)) {
|
if (unlikely(ctx == NULL)) {
|
||||||
|
|
@ -28,6 +29,7 @@ extract_begin(enum selection_kind kind)
|
||||||
|
|
||||||
*ctx = (struct extraction_context){
|
*ctx = (struct extraction_context){
|
||||||
.selection_kind = kind,
|
.selection_kind = kind,
|
||||||
|
.strip_trailing_empty = strip_trailing_empty,
|
||||||
};
|
};
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
@ -51,8 +53,7 @@ ensure_size(struct extraction_context *ctx, size_t additional_chars)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
extract_finish_wide(struct extraction_context *ctx, bool strip_trailing_empty,
|
extract_finish_wide(struct extraction_context *ctx, wchar_t **text, size_t *len)
|
||||||
wchar_t **text, size_t *len)
|
|
||||||
{
|
{
|
||||||
if (text == NULL)
|
if (text == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -64,7 +65,7 @@ extract_finish_wide(struct extraction_context *ctx, bool strip_trailing_empty,
|
||||||
if (ctx->failed)
|
if (ctx->failed)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
if (!strip_trailing_empty) {
|
if (!ctx->strip_trailing_empty) {
|
||||||
/* Insert pending newlines, and replace empty cells with spaces */
|
/* Insert pending newlines, and replace empty cells with spaces */
|
||||||
if (!ensure_size(ctx, ctx->newline_count + ctx->empty_count))
|
if (!ensure_size(ctx, ctx->newline_count + ctx->empty_count))
|
||||||
goto err;
|
goto err;
|
||||||
|
|
@ -106,8 +107,7 @@ err:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
extract_finish(struct extraction_context *ctx, bool strip_trailing_empty,
|
extract_finish(struct extraction_context *ctx, char **text, size_t *len)
|
||||||
char **text, size_t *len)
|
|
||||||
{
|
{
|
||||||
if (text == NULL)
|
if (text == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -115,7 +115,7 @@ extract_finish(struct extraction_context *ctx, bool strip_trailing_empty,
|
||||||
*len = 0;
|
*len = 0;
|
||||||
|
|
||||||
wchar_t *wtext;
|
wchar_t *wtext;
|
||||||
if (!extract_finish_wide(ctx, strip_trailing_empty, &wtext, NULL))
|
if (!extract_finish_wide(ctx, &wtext, NULL))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
@ -167,6 +167,13 @@ extract_one(const struct terminal *term, const struct row *row,
|
||||||
/* Don't emit newline just yet - only if there are
|
/* Don't emit newline just yet - only if there are
|
||||||
* non-empty cells following it */
|
* non-empty cells following it */
|
||||||
ctx->newline_count++;
|
ctx->newline_count++;
|
||||||
|
|
||||||
|
if (!ctx->strip_trailing_empty) {
|
||||||
|
if (!ensure_size(ctx, ctx->empty_count))
|
||||||
|
goto err;
|
||||||
|
for (size_t i = 0; i < ctx->empty_count; i++)
|
||||||
|
ctx->buf[ctx->idx++] = L' ';
|
||||||
|
}
|
||||||
ctx->empty_count = 0;
|
ctx->empty_count = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -175,6 +182,13 @@ extract_one(const struct terminal *term, const struct row *row,
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
ctx->buf[ctx->idx++] = L'\n';
|
ctx->buf[ctx->idx++] = L'\n';
|
||||||
|
|
||||||
|
if (!ctx->strip_trailing_empty) {
|
||||||
|
if (!ensure_size(ctx, ctx->empty_count))
|
||||||
|
goto err;
|
||||||
|
for (size_t i = 0; i < ctx->empty_count; i++)
|
||||||
|
ctx->buf[ctx->idx++] = L' ';
|
||||||
|
}
|
||||||
ctx->empty_count = 0;
|
ctx->empty_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,14 @@
|
||||||
|
|
||||||
struct extraction_context;
|
struct extraction_context;
|
||||||
|
|
||||||
struct extraction_context *extract_begin(enum selection_kind kind);
|
struct extraction_context *extract_begin(
|
||||||
|
enum selection_kind kind, bool strip_trailing_empty);
|
||||||
|
|
||||||
bool extract_one(
|
bool extract_one(
|
||||||
const struct terminal *term, const struct row *row, const struct cell *cell,
|
const struct terminal *term, const struct row *row, const struct cell *cell,
|
||||||
int col, void *context);
|
int col, void *context);
|
||||||
|
|
||||||
bool extract_finish(
|
bool extract_finish(
|
||||||
struct extraction_context *context, bool strip_trailing_empty,
|
struct extraction_context *context, char **text, size_t *len);
|
||||||
char **text, size_t *len);
|
|
||||||
bool extract_finish_wide(
|
bool extract_finish_wide(
|
||||||
struct extraction_context *context, bool strip_trailing_empty,
|
struct extraction_context *context, wchar_t **text, size_t *len);
|
||||||
wchar_t **text, size_t *len);
|
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ render_worker_thread(void *_ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct extraction_context *
|
struct extraction_context *
|
||||||
extract_begin(enum selection_kind kind)
|
extract_begin(enum selection_kind kind, bool strip_trailing_empty)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
4
search.c
4
search.c
|
|
@ -473,7 +473,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only)
|
||||||
struct coord pos = old_end;
|
struct coord pos = old_end;
|
||||||
row = term->grid->rows[pos.row];
|
row = term->grid->rows[pos.row];
|
||||||
|
|
||||||
struct extraction_context *ctx = extract_begin(SELECTION_NONE);
|
struct extraction_context *ctx = extract_begin(SELECTION_NONE, false);
|
||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
@ -487,7 +487,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only)
|
||||||
wchar_t *new_text;
|
wchar_t *new_text;
|
||||||
size_t new_len;
|
size_t new_len;
|
||||||
|
|
||||||
if (!extract_finish_wide(ctx, false, &new_text, &new_len))
|
if (!extract_finish_wide(ctx, &new_text, &new_len))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!search_ensure_size(term, term->search.len + new_len))
|
if (!search_ensure_size(term, term->search.len + new_len))
|
||||||
|
|
|
||||||
|
|
@ -215,7 +215,7 @@ selection_to_text(const struct terminal *term)
|
||||||
if (term->selection.end.row == -1)
|
if (term->selection.end.row == -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
struct extraction_context *ctx = extract_begin(term->selection.kind);
|
struct extraction_context *ctx = extract_begin(term->selection.kind, true);
|
||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
|
@ -224,7 +224,7 @@ selection_to_text(const struct terminal *term)
|
||||||
&extract_one_const_wrapper, ctx);
|
&extract_one_const_wrapper, ctx);
|
||||||
|
|
||||||
char *text;
|
char *text;
|
||||||
return extract_finish(ctx, true, &text, NULL) ? text : NULL;
|
return extract_finish(ctx, &text, NULL) ? text : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
||||||
|
|
@ -2900,7 +2900,7 @@ static bool
|
||||||
rows_to_text(const struct terminal *term, int start, int end,
|
rows_to_text(const struct terminal *term, int start, int end,
|
||||||
char **text, size_t *len)
|
char **text, size_t *len)
|
||||||
{
|
{
|
||||||
struct extraction_context *ctx = extract_begin(SELECTION_NONE);
|
struct extraction_context *ctx = extract_begin(SELECTION_NONE, true);
|
||||||
if (ctx == NULL)
|
if (ctx == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
@ -2917,7 +2917,7 @@ rows_to_text(const struct terminal *term, int start, int end,
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
return extract_finish(ctx, true, text, len);
|
return extract_finish(ctx, text, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue