aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-11 14:32:33 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-11 15:38:03 +0000
commit6edc3845bd90760c9c1c3173ef1f680a30c45f28 (patch)
tree095ff96a6803643051e2139caacd3aa0a76dbffc
parentselection: read until EOF when reading from clipboard (diff)
selection: calculate required length for clipboard text
-rw-r--r--selection.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/selection.c b/selection.c
index 67cf10ea..d2d43e60 100644
--- a/selection.c
+++ b/selection.c
@@ -34,8 +34,17 @@ extract_selection(const struct terminal *term)
assert(start->row <= end->row);
- /* TODO: calculate length required */
- char buf[4096];
+ size_t max_cells = 0;
+ if (start->row == end->row) {
+ assert(start->col <= end->col);
+ max_cells = end->col - start->col + 1;
+ } else {
+ max_cells = term->cols - start->col;
+ max_cells += term->cols * (end->row - start->row - 1);
+ max_cells += end->col + 1;
+ }
+
+ char *buf = malloc(max_cells * 4 + 1);
int idx = 0;
int start_col = start->col;
@@ -75,7 +84,7 @@ extract_selection(const struct terminal *term)
}
buf[idx] = '\0';
- return strdup(buf);
+ return buf;
}
void