selection: trim trailing spaces from block selections

The extract_*() family of functions can now optionally treat spaces as
empty cells.

Use this in selection_to_text() to trim trailing spaces from block
selections.

Closes #181
This commit is contained in:
Daniel Eklöf 2020-11-10 19:47:23 +01:00
parent c9de30e2bc
commit 194cf1ce87
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 13 additions and 5 deletions

View file

@ -58,6 +58,8 @@
* Blinking text now uses the foreground color, but dimmed down in its
off state, instead of the background color.
* Trailing spaces are trimmed when copying a block selection
(https://codeberg.org/dnkl/foot/issues/181).
### Deprecated

View file

@ -15,10 +15,11 @@ struct extraction_context {
const struct row *last_row;
const struct cell *last_cell;
enum selection_kind selection_kind;
bool trim_trailing_spaces;
};
struct extraction_context *
extract_begin(enum selection_kind kind)
extract_begin(enum selection_kind kind, bool trim_trailing_spaces)
{
struct extraction_context *ctx = malloc(sizeof(*ctx));
if (unlikely(ctx == NULL)) {
@ -28,6 +29,7 @@ extract_begin(enum selection_kind kind)
*ctx = (struct extraction_context){
.selection_kind = kind,
.trim_trailing_spaces = trim_trailing_spaces,
};
return ctx;
}
@ -143,7 +145,7 @@ extract_one(const struct terminal *term, const struct row *row,
}
}
if (cell->wc == 0) {
if (cell->wc == 0 || (ctx->trim_trailing_spaces && cell->wc == L' ')) {
ctx->empty_count++;
ctx->last_row = row;
ctx->last_cell = cell;

View file

@ -7,7 +7,8 @@
struct extraction_context;
struct extraction_context *extract_begin(enum selection_kind kind);
struct extraction_context *extract_begin(
enum selection_kind kind, bool trim_trailing_spaces);
bool extract_one(
const struct terminal *term, const struct row *row, const struct cell *cell,

View file

@ -224,7 +224,10 @@ selection_to_text(const struct terminal *term)
if (term->selection.end.row == -1)
return NULL;
struct extraction_context *ctx = extract_begin(term->selection.kind);
const enum selection_kind kind = term->selection.kind;
const bool trim_trailing_spaces = kind == SELECTION_BLOCK;
struct extraction_context *ctx = extract_begin(kind, trim_trailing_spaces);
if (ctx == NULL)
return NULL;

View file

@ -2651,7 +2651,7 @@ static bool
rows_to_text(const struct terminal *term, int start, int end,
char **text, size_t *len)
{
struct extraction_context *ctx = extract_begin(SELECTION_NONE);
struct extraction_context *ctx = extract_begin(SELECTION_NONE, false);
if (ctx == NULL)
return false;