From f8e875a7cddaac91818b98993d17a96d09a5b4a5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 8 Dec 2022 10:35:30 +0100 Subject: term: move row->prompt_marker into new struct, row->shell_integration --- terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index f63aaaa7..33666e6a 100644 --- a/terminal.c +++ b/terminal.c @@ -1825,7 +1825,7 @@ erase_line(struct terminal *term, struct row *row) { erase_cell_range(term, row, 0, term->cols - 1); row->linebreak = false; - row->prompt_marker = false; + row->shell_integration.prompt_marker = false; } void -- cgit v1.3 From e9607de5ae2eaf1a57924bb282a660150eca6eb5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 8 Dec 2022 10:46:46 +0100 Subject: osc: store column of FTCS_COMMAND_{EXECUTED,FINISHED} in row struct --- grid.c | 4 ++++ osc.c | 10 ++++++++-- terminal.c | 2 ++ terminal.h | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) (limited to 'terminal.c') diff --git a/grid.c b/grid.c index 19322cbf..f14580ad 100644 --- a/grid.c +++ b/grid.c @@ -367,6 +367,8 @@ grid_row_alloc(int cols, bool initialize) row->linebreak = false; row->extra = NULL; row->shell_integration.prompt_marker = false; + row->shell_integration.cmd_start = -1; + row->shell_integration.cmd_end = -1; if (initialize) { row->cells = xcalloc(cols, sizeof(row->cells[0])); @@ -588,6 +590,8 @@ _line_wrap(struct grid *old_grid, struct row **new_grid, struct row *row, grid_row_reset_extra(new_row); new_row->linebreak = false; new_row->shell_integration.prompt_marker = false; + new_row->shell_integration.cmd_start = -1; + new_row->shell_integration.cmd_end = -1; tll_foreach(old_grid->sixel_images, it) { if (it->item.pos.row == *row_idx) { diff --git a/osc.c b/osc.c index a54946ff..5da8666f 100644 --- a/osc.c +++ b/osc.c @@ -901,11 +901,17 @@ osc_dispatch(struct terminal *term) break; case 'C': - LOG_DBG("FTCS_COMMAND_EXECUTED"); + LOG_DBG("FTCS_COMMAND_EXECUTED: %dx%d", + term->grid->cursor.point.row, + term->grid->cursor.point.col); + term->grid->cur_row->shell_integration.cmd_start = term->grid->cursor.point.col; break; case 'D': - LOG_DBG("FTCS_COMMAND_FINISHED"); + LOG_DBG("FTCS_COMMAND_FINISHED: %dx%d", + term->grid->cursor.point.row, + term->grid->cursor.point.col); + term->grid->cur_row->shell_integration.cmd_end = term->grid->cursor.point.col; break; } break; diff --git a/terminal.c b/terminal.c index 33666e6a..4152724a 100644 --- a/terminal.c +++ b/terminal.c @@ -1826,6 +1826,8 @@ erase_line(struct terminal *term, struct row *row) erase_cell_range(term, row, 0, term->cols - 1); row->linebreak = false; row->shell_integration.prompt_marker = false; + row->shell_integration.cmd_start = -1; + row->shell_integration.cmd_end = -1; } void diff --git a/terminal.h b/terminal.h index 76b52566..1e3a724e 100644 --- a/terminal.h +++ b/terminal.h @@ -123,6 +123,8 @@ struct row { struct { bool prompt_marker; + int cmd_start; /* Column, -1 if unset */ + int cmd_end; /* Column, -1 if unset */ } shell_integration; }; -- cgit v1.3 From 1c70a84fde80405e0b6658722af8bc3fb54f225d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 8 Dec 2022 11:45:23 +0100 Subject: config: add pipe-command-output key-binding --- config.c | 1 + input.c | 7 ++++- key-binding.h | 1 + terminal.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- terminal.h | 2 ++ 5 files changed, 99 insertions(+), 5 deletions(-) (limited to 'terminal.c') diff --git a/config.c b/config.c index e3d531c2..3728826f 100644 --- a/config.c +++ b/config.c @@ -111,6 +111,7 @@ static const char *const binding_action_map[] = { [BIND_ACTION_PIPE_SCROLLBACK] = "pipe-scrollback", [BIND_ACTION_PIPE_VIEW] = "pipe-visible", [BIND_ACTION_PIPE_SELECTED] = "pipe-selected", + [BIND_ACTION_PIPE_COMMAND_OUTPUT] = "pipe-command-output", [BIND_ACTION_SHOW_URLS_COPY] = "show-urls-copy", [BIND_ACTION_SHOW_URLS_LAUNCH] = "show-urls-launch", [BIND_ACTION_SHOW_URLS_PERSISTENT] = "show-urls-persistent", diff --git a/input.c b/input.c index 5cdf513f..166ad70b 100644 --- a/input.c +++ b/input.c @@ -227,7 +227,8 @@ execute_binding(struct seat *seat, struct terminal *term, break; /* FALLTHROUGH */ case BIND_ACTION_PIPE_VIEW: - case BIND_ACTION_PIPE_SELECTED: { + case BIND_ACTION_PIPE_SELECTED: + case BIND_ACTION_PIPE_COMMAND_OUTPUT: { if (binding->aux->type != BINDING_AUX_PIPE) return true; @@ -269,6 +270,10 @@ execute_binding(struct seat *seat, struct terminal *term, len = text != NULL ? strlen(text) : 0; break; + case BIND_ACTION_PIPE_COMMAND_OUTPUT: + success = term_command_output_to_text(term, &text, &len); + break; + default: BUG("Unhandled action type"); success = false; diff --git a/key-binding.h b/key-binding.h index 050c80a6..ba841efa 100644 --- a/key-binding.h +++ b/key-binding.h @@ -32,6 +32,7 @@ enum bind_action_normal { BIND_ACTION_PIPE_SCROLLBACK, BIND_ACTION_PIPE_VIEW, BIND_ACTION_PIPE_SELECTED, + BIND_ACTION_PIPE_COMMAND_OUTPUT, BIND_ACTION_SHOW_URLS_COPY, BIND_ACTION_SHOW_URLS_LAUNCH, BIND_ACTION_SHOW_URLS_PERSISTENT, diff --git a/terminal.c b/terminal.c index 4152724a..a328783f 100644 --- a/terminal.c +++ b/terminal.c @@ -3613,7 +3613,7 @@ term_surface_kind(const struct terminal *term, const struct wl_surface *surface) static bool rows_to_text(const struct terminal *term, int start, int end, - char **text, size_t *len) + int col_start, int col_end, char **text, size_t *len) { struct extraction_context *ctx = extract_begin(SELECTION_NONE, true); if (ctx == NULL) @@ -3626,15 +3626,20 @@ rows_to_text(const struct terminal *term, int start, int end, const struct row *row = term->grid->rows[r]; xassert(row != NULL); - for (int c = 0; c < term->cols; c++) + const int c_end = r == end ? col_end : term->cols; + + for (int c = col_start; c < c_end; c++) { if (!extract_one(term, row, &row->cells[c], c, ctx)) goto out; + } if (r == end) break; r++; r &= grid_rows - 1; + + col_start = 0; } out: @@ -3666,7 +3671,7 @@ term_scrollback_to_text(const struct terminal *term, char **text, size_t *len) end += term->grid->num_rows; } - return rows_to_text(term, start, end, text, len); + return rows_to_text(term, start, end, 0, term->cols, text, len); } bool @@ -3674,7 +3679,87 @@ term_view_to_text(const struct terminal *term, char **text, size_t *len) { int start = grid_row_absolute_in_view(term->grid, 0); int end = grid_row_absolute_in_view(term->grid, term->rows - 1); - return rows_to_text(term, start, end, text, len); + return rows_to_text(term, start, end, 0, term->cols, text, len); +} + +bool +term_command_output_to_text(const struct terminal *term, char **text, size_t *len) +{ + int start_row = -1; + int end_row = -1; + int start_col = -1; + int end_col = -1; + + const struct grid *grid = term->grid; + const int sb_end = grid_row_absolute(grid, term->rows - 1); + int r = (sb_end - 1 + grid->num_rows) & (grid->num_rows - 1); + + while (start_row < 0 && r != sb_end) { + const struct row *row = grid->rows[r]; + if (row == NULL) + break; + + if (row->shell_integration.cmd_end >= 0) { + end_row = r; + end_col = row->shell_integration.cmd_end; + } + + if (end_row >= 0 && row->shell_integration.cmd_start >= 0) { + start_row = r; + start_col = row->shell_integration.cmd_start; + } + + r = (r - 1 + grid->num_rows) & (grid->num_rows - 1); + } + + if (start_row < 0) + return false; + + bool ret = rows_to_text(term, start_row, end_row, start_col, end_col, text, len); + if (!ret) + return false; + + /* + * If the FTCS_COMMAND_FINISHED marker was emitted at the *first* + * column, then the *entire* previous line is part of the command + * output. *Including* the newline, if any. + * + * Since rows_to_text() doesn’t extract the column + * FTCS_COMMAND_FINISHED was emitted at (that would be wrong - + * FTCS_COMMAND_FINISHED is emitted *after* the command output, + * not at its last character), the extraction logic will not see + * the last newline (this is true for all non-line-wise selection + * types), and the extracted text will *not* end with a newline. + * + * Here we try to compensate for that. Note that if ‘end_col’ is + * not 0, then the command output only covers a partial row, and + * thus we do *not* want to append a newline. + */ + + if (end_col > 0) { + /* Command output covers partial row - don’t append newline */ + return true; + } + + int next_to_last_row = (end_row - 1 + grid->num_rows) & (grid->num_rows - 1); + const struct row *row = grid->rows[next_to_last_row]; + + /* Add newline if last row has a hard linebreak */ + if (row->linebreak) { + char *new_text = xrealloc(*text, *len + 1 + 1); + + if (new_text == NULL) { + /* Ignore failure - use text as is (without inserting newline) */ + return true; + } + + *text = new_text; + (*len)++; + (*text)[*len - 1] = '\n'; + (*text)[*len] = '\0'; + } + + return true; } bool diff --git a/terminal.h b/terminal.h index 1e3a724e..35127e3c 100644 --- a/terminal.h +++ b/terminal.h @@ -847,6 +847,8 @@ bool term_scrollback_to_text( const struct terminal *term, char **text, size_t *len); bool term_view_to_text( const struct terminal *term, char **text, size_t *len); +bool term_command_output_to_text( + const struct terminal *term, char **text, size_t *len); bool term_ime_is_enabled(const struct terminal *term); void term_ime_enable(struct terminal *term); -- cgit v1.3 From d5308a0493bb69167aacf54a8eb98f2e92a046dd Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 8 Dec 2022 13:06:24 +0100 Subject: term: command_output_to_text(): don’t skip FTCS_COMMAND_FINISHED on last row MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- terminal.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index a328783f..5d6c956d 100644 --- a/terminal.c +++ b/terminal.c @@ -3692,9 +3692,10 @@ term_command_output_to_text(const struct terminal *term, char **text, size_t *le const struct grid *grid = term->grid; const int sb_end = grid_row_absolute(grid, term->rows - 1); - int r = (sb_end - 1 + grid->num_rows) & (grid->num_rows - 1); + const int sb_start = (sb_end + 1) & (grid->num_rows - 1); + int r = sb_end; - while (start_row < 0 && r != sb_end) { + while (start_row < 0) { const struct row *row = grid->rows[r]; if (row == NULL) break; @@ -3709,6 +3710,9 @@ term_command_output_to_text(const struct terminal *term, char **text, size_t *le start_col = row->shell_integration.cmd_start; } + if (r == sb_start) + break; + r = (r - 1 + grid->num_rows) & (grid->num_rows - 1); } -- cgit v1.3