Skip to content

Commit 40b09a6

Browse files
GUACAMOLE-1586: Don't add \n in clipboard if the line isn't finished.
1 parent 7d004ce commit 40b09a6

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

src/terminal/buffer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void guac_terminal_buffer_copy_rows(guac_terminal_buffer* buffer,
164164
guac_terminal_buffer_row* dst_row = guac_terminal_buffer_get_row(buffer, current_row + offset, src_row->length);
165165

166166
/* Copy data */
167-
memcpy(dst_row->characters, src_row->characters, sizeof(guac_terminal_char) * src_row->length);
167+
memcpy(dst_row->characters, src_row->characters, sizeof(guac_terminal_char) * src_row->length + 1);
168168
dst_row->length = src_row->length;
169169

170170
/* Next current_row */

src/terminal/select.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,33 @@ void guac_terminal_select_end(guac_terminal* terminal) {
358358
/* Otherwise, copy multiple rows */
359359
else {
360360

361+
/* Get last char of the row */
362+
guac_terminal_buffer_row* buffer_row = guac_terminal_buffer_get_row(terminal->buffer, start_row, 0);
363+
int last_row_char = buffer_row->characters[buffer_row->length].value;
364+
361365
/* Store first row */
362366
guac_terminal_clipboard_append_row(terminal, start_row, start_col, -1);
363367

364368
/* Store all middle rows */
365369
for (int row = start_row + 1; row < end_row; row++) {
366-
guac_common_clipboard_append(terminal->clipboard, "\n", 1);
370+
371+
/* Add new line only if the row above ended with a null character */
372+
if (last_row_char == 0)
373+
guac_common_clipboard_append(terminal->clipboard, "\n", 1);
374+
375+
/* Store middle row */
367376
guac_terminal_clipboard_append_row(terminal, row, 0, -1);
377+
378+
/* Update to last char of current row */
379+
buffer_row = guac_terminal_buffer_get_row(terminal->buffer, row, 0);
380+
last_row_char = buffer_row->characters[buffer_row->length].value;
368381
}
369382

383+
/* Add new line only if the row above ended with a null character */
384+
if (last_row_char == 0)
385+
guac_common_clipboard_append(terminal->clipboard, "\n", 1);
386+
370387
/* Store last row */
371-
guac_common_clipboard_append(terminal->clipboard, "\n", 1);
372388
guac_terminal_clipboard_append_row(terminal, end_row, 0, end_col);
373389

374390
}

src/terminal/terminal-handlers.c

+17-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@
5858
* @param term
5959
* The guac_terminal whose cursor should be advanced to the next row.
6060
*/
61-
static void guac_terminal_linefeed(guac_terminal* term) {
61+
static void guac_terminal_linefeed(guac_terminal* term, bool force_wrap) {
62+
63+
/* Assign in column out of screen: 1 to avoid \n in clipboard or 0 to add \n */
64+
guac_terminal_buffer_row* buffer_row =
65+
guac_terminal_buffer_get_row(term->buffer, term->cursor_row, 0);
66+
buffer_row->characters[buffer_row->length].value = force_wrap ? 1 : 0;
6267

6368
/* Scroll up if necessary */
6469
if (term->cursor_row == term->scroll_end)
@@ -81,6 +86,11 @@ static void guac_terminal_linefeed(guac_terminal* term) {
8186
*/
8287
static void guac_terminal_reverse_linefeed(guac_terminal* term) {
8388

89+
/* Reset column out of screen */
90+
guac_terminal_buffer_row* buffer_row =
91+
guac_terminal_buffer_get_row(term->buffer, term->cursor_row, 0);
92+
buffer_row->characters[buffer_row->length].value = 0;
93+
8494
/* Scroll down if necessary */
8595
if (term->cursor_row == term->scroll_start)
8696
guac_terminal_scroll_down(term, term->scroll_start,
@@ -221,7 +231,7 @@ int guac_terminal_echo(guac_terminal* term, unsigned char c) {
221231
case 0x0C: /* FF */
222232

223233
/* Advance to next row */
224-
guac_terminal_linefeed(term);
234+
guac_terminal_linefeed(term, false);
225235

226236
/* If automatic carriage return, fall through to CR handler */
227237
if (!term->automatic_carriage_return)
@@ -269,8 +279,10 @@ int guac_terminal_echo(guac_terminal* term, unsigned char c) {
269279

270280
/* Wrap if necessary */
271281
if (term->cursor_col >= term->term_width) {
282+
283+
/* New line */
272284
term->cursor_col = 0;
273-
guac_terminal_linefeed(term);
285+
guac_terminal_linefeed(term, true);
274286
}
275287

276288
/* If insert mode, shift other characters right by 1 */
@@ -340,14 +352,14 @@ int guac_terminal_escape(guac_terminal* term, unsigned char c) {
340352

341353
/* Index (IND) */
342354
case 'D':
343-
guac_terminal_linefeed(term);
355+
guac_terminal_linefeed(term, false);
344356
term->char_handler = guac_terminal_echo;
345357
break;
346358

347359
/* Next Line (NEL) */
348360
case 'E':
349361
guac_terminal_move_cursor(term, term->cursor_row, 0);
350-
guac_terminal_linefeed(term);
362+
guac_terminal_linefeed(term, false);
351363
term->char_handler = guac_terminal_echo;
352364
break;
353365

src/terminal/terminal.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void guac_terminal_reset(guac_terminal* term) {
244244

245245
/* Clear terminal */
246246
for (row=0; row<term->term_height; row++)
247-
guac_terminal_set_columns(term, row, 0, term->term_width, &(term->default_char));
247+
guac_terminal_set_columns(term, row, 0, term->term_width-1, &(term->default_char));
248248

249249
}
250250

0 commit comments

Comments
 (0)