From a09f6326d087593a9fb5d29a387ceadd292a7c16 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Mon, 14 May 2018 21:59:13 +0900 Subject: [PATCH] Insert line feeds when selection is over multiple lines --- apps/terminal.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/apps/terminal.c b/apps/terminal.c index 15ea58b5..8eef7736 100644 --- a/apps/terminal.c +++ b/apps/terminal.c @@ -263,24 +263,33 @@ static int to_eight(uint32_t codepoint, char * out) { void count_selection(uint16_t x, uint16_t y) { term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t)); - if (cell->flags & ANSI_EXT_IMG) { return; } - if (((uint32_t *)cell)[0] != 0x00000000) { - char tmp[7]; - _selection_count += to_eight(cell->c, tmp); + if (!(cell->flags & ANSI_EXT_IMG)) { + if (((uint32_t *)cell)[0] != 0x00000000) { + char tmp[7]; + _selection_count += to_eight(cell->c, tmp); + } + } + if (x == term_width - 1) { + _selection_count++; } } void write_selection(uint16_t x, uint16_t y) { term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (y * term_width + x) * sizeof(term_cell_t)); - if (cell->flags & ANSI_EXT_IMG) { return; } - if (((uint32_t *)cell)[0] != 0x00000000) { - char tmp[7]; - int count = to_eight(cell->c, tmp); - for (int i = 0; i < count; ++i) { - selection_text[_selection_i] = tmp[i]; - _selection_i++; + if (!(cell->flags & ANSI_EXT_IMG)) { + if (((uint32_t *)cell)[0] != 0x00000000) { + char tmp[7]; + int count = to_eight(cell->c, tmp); + for (int i = 0; i < count; ++i) { + selection_text[_selection_i] = tmp[i]; + _selection_i++; + } } } + if (x == term_width - 1) { + selection_text[_selection_i] = '\n';; + _selection_i++; + } } @@ -294,11 +303,20 @@ char * copy_selection(void) { free(selection_text); } + if (_selection_count == 0) { + return NULL; + } + selection_text = malloc(_selection_count + 1); selection_text[_selection_count] = '\0'; _selection_i = 0; iterate_selection(write_selection); + if (selection_text[_selection_count-1] == '\n') { + /* Don't end on a line feed */ + selection_text[_selection_count-1] = '\0'; + } + return selection_text; }