2016-05-03 19:31:07 +00:00
|
|
|
// High level library for handling the terminal screen.
|
|
|
|
//
|
|
|
|
// The screen library allows the interactive reader to write its output to screen efficiently by
|
|
|
|
// keeping an internal representation of the current screen contents and trying to find the most
|
|
|
|
// efficient way for transforming that to the desired screen content.
|
|
|
|
//
|
2006-10-01 16:02:58 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2017-02-16 04:09:26 +00:00
|
|
|
#include <stddef.h>
|
2006-10-01 16:02:58 +00:00
|
|
|
#include <stdio.h>
|
2016-05-03 19:31:07 +00:00
|
|
|
#include <stdlib.h>
|
2019-06-11 09:34:14 +00:00
|
|
|
#include <termios.h>
|
2006-10-01 16:02:58 +00:00
|
|
|
#include <unistd.h>
|
2019-10-13 22:50:48 +00:00
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <cstdint>
|
2019-05-05 10:09:25 +00:00
|
|
|
#include <cstring>
|
2019-03-12 21:06:01 +00:00
|
|
|
#include <cwchar>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2018-02-04 08:59:37 +00:00
|
|
|
#if HAVE_CURSES_H
|
2022-08-21 21:51:33 +00:00
|
|
|
#include <curses.h> // IWYU pragma: keep
|
2018-02-04 08:59:37 +00:00
|
|
|
#elif HAVE_NCURSES_H
|
2006-10-01 16:02:58 +00:00
|
|
|
#include <ncurses.h>
|
2014-12-07 08:41:15 +00:00
|
|
|
#elif HAVE_NCURSES_CURSES_H
|
|
|
|
#include <ncurses/curses.h>
|
2006-10-01 16:02:58 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_TERM_H
|
|
|
|
#include <term.h>
|
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
2012-03-04 10:45:51 +00:00
|
|
|
#include <vector>
|
2006-10-01 16:02:58 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-12-23 21:08:45 +00:00
|
|
|
#include "env.h"
|
2016-05-03 19:31:07 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2019-08-10 21:32:55 +00:00
|
|
|
#include "flog.h"
|
2022-08-21 06:14:48 +00:00
|
|
|
#include "global_safety.h"
|
2016-05-03 19:31:07 +00:00
|
|
|
#include "highlight.h"
|
|
|
|
#include "output.h"
|
2014-01-16 02:21:38 +00:00
|
|
|
#include "pager.h"
|
2016-05-03 19:31:07 +00:00
|
|
|
#include "screen.h"
|
2022-08-21 06:14:48 +00:00
|
|
|
#include "termsize.h"
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// The number of characters to indent new blocks.
|
2022-09-20 18:58:37 +00:00
|
|
|
#define INDENT_STEP 4u
|
2007-09-24 08:49:33 +00:00
|
|
|
|
2021-09-28 19:50:27 +00:00
|
|
|
/// RAII class to begin and end buffering around an outputter.
|
|
|
|
namespace {
|
|
|
|
class scoped_buffer_t : noncopyable_t, nonmovable_t {
|
|
|
|
outputter_t &outp_;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
public:
|
2021-09-28 19:50:27 +00:00
|
|
|
explicit scoped_buffer_t(outputter_t &outp) : outp_(outp) { outp_.begin_buffering(); }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-28 19:50:27 +00:00
|
|
|
~scoped_buffer_t() { outp_.end_buffering(); }
|
2012-03-26 08:21:10 +00:00
|
|
|
};
|
2021-09-28 19:50:27 +00:00
|
|
|
} // namespace
|
2012-03-26 08:21:10 +00:00
|
|
|
|
2017-02-04 03:20:21 +00:00
|
|
|
// Singleton of the cached escape sequences seen in prompts and similar strings.
|
2017-09-01 21:33:59 +00:00
|
|
|
// Note this is deliberately exported so that init_curses can clear it.
|
2020-05-30 21:59:35 +00:00
|
|
|
layout_cache_t layout_cache_t::shared;
|
2017-02-04 03:20:21 +00:00
|
|
|
|
2023-05-28 23:13:54 +00:00
|
|
|
void screen_clear_layout_cache_ffi() { layout_cache_t::shared.clear(); }
|
2023-05-16 19:46:16 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Tests if the specified narrow character sequence is present at the specified position of the
|
|
|
|
/// specified wide character string. All of \c seq must match, but str may be longer than seq.
|
|
|
|
static size_t try_sequence(const char *seq, const wchar_t *str) {
|
|
|
|
for (size_t i = 0;; i++) {
|
|
|
|
if (!seq[i]) return i;
|
|
|
|
if (seq[i] != str[i]) return 0;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2017-02-04 03:20:21 +00:00
|
|
|
DIE("unexpectedly fell off end of try_sequence()");
|
|
|
|
return 0; // this should never be executed
|
2006-10-01 16:02:58 +00:00
|
|
|
}
|
|
|
|
|
Add workaround for Midnight Commander's issue with prompt extraction
When we draw the prompt, we move the cursor to the actual
position *we* think it is by issuing a carriage return (via
`move(0,0)`), and then going forward until we hit the spot.
This helps when the terminal and fish disagree on the width of the
prompt, because we are now definitely in the correct place, so we can
only overwrite a bit of the prompt (if it renders longer than we
expected) or leave space after the prompt. Both of these are benign in
comparison to staircase effects we would otherwise get.
Unfortunately, midnight commander ("mc") tries to extract the last
line of the prompt, and does so in a way that is overly naive - it
resets everything to 0 when it sees a `\r`, and doesn't account for
cursor movement. In effect it's playing a terminal, but not committing
to the bit.
Since this has been an open request in mc for quite a while, we hack
around it, by checking the $MC_SID environment variable.
If we see it, we skip the clearing. We end up most likely doing
relative movement from where we think we are, and in most cases it
should be *fine*.
2023-02-04 17:57:41 +00:00
|
|
|
static bool midnight_commander_hack = false;
|
|
|
|
|
2023-02-24 23:54:46 +00:00
|
|
|
void screen_set_midnight_commander_hack() { midnight_commander_hack = true; }
|
Add workaround for Midnight Commander's issue with prompt extraction
When we draw the prompt, we move the cursor to the actual
position *we* think it is by issuing a carriage return (via
`move(0,0)`), and then going forward until we hit the spot.
This helps when the terminal and fish disagree on the width of the
prompt, because we are now definitely in the correct place, so we can
only overwrite a bit of the prompt (if it renders longer than we
expected) or leave space after the prompt. Both of these are benign in
comparison to staircase effects we would otherwise get.
Unfortunately, midnight commander ("mc") tries to extract the last
line of the prompt, and does so in a way that is overly naive - it
resets everything to 0 when it sees a `\r`, and doesn't account for
cursor movement. In effect it's playing a terminal, but not committing
to the bit.
Since this has been an open request in mc for quite a while, we hack
around it, by checking the $MC_SID environment variable.
If we see it, we skip the clearing. We end up most likely doing
relative movement from where we think we are, and in most cases it
should be *fine*.
2023-02-04 17:57:41 +00:00
|
|
|
|
2019-11-25 11:03:25 +00:00
|
|
|
/// Returns the number of columns left until the next tab stop, given the current cursor position.
|
2016-05-05 01:14:04 +00:00
|
|
|
static size_t next_tab_stop(size_t current_line_width) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Assume tab stops every 8 characters if undefined.
|
2019-11-19 01:08:16 +00:00
|
|
|
size_t tab_width = init_tabs > 0 ? static_cast<size_t>(init_tabs) : 8;
|
2016-05-05 01:14:04 +00:00
|
|
|
return ((current_line_width / tab_width) + 1) * tab_width;
|
2006-11-11 10:54:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 20:53:41 +00:00
|
|
|
int line_t::wcswidth_min_0(size_t max) const {
|
|
|
|
int result = 0;
|
|
|
|
for (size_t idx = 0, end = std::min(max, text.size()); idx < end; idx++) {
|
2021-09-23 10:57:44 +00:00
|
|
|
auto w = fish_wcwidth_visible(text[idx].character);
|
|
|
|
// A backspace at the start of the line does nothing.
|
|
|
|
if (w > 0 || result > 0) {
|
|
|
|
result += w;
|
|
|
|
}
|
2020-06-07 20:53:41 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Whether we permit soft wrapping. If so, in some cases we don't explicitly move to the second
|
|
|
|
/// physical line on a wrapped logical line; instead we just output it.
|
2018-02-19 02:33:04 +00:00
|
|
|
static bool allow_soft_wrap() {
|
2012-10-01 10:29:18 +00:00
|
|
|
// Should we be looking at eat_newline_glitch as well?
|
2016-05-05 01:14:04 +00:00
|
|
|
return auto_right_margin;
|
|
|
|
}
|
|
|
|
|
2018-09-25 04:17:05 +00:00
|
|
|
/// Does this look like the escape sequence for setting a screen name?
|
2016-05-05 01:14:04 +00:00
|
|
|
static bool is_screen_name_escape_seq(const wchar_t *code, size_t *resulting_length) {
|
2021-08-30 14:55:14 +00:00
|
|
|
// Tmux escapes start with `\ePtmux;` and end also in `\e\\`,
|
|
|
|
// so we can just handle them here.
|
|
|
|
static const wchar_t *tmux_seq = L"Ptmux;";
|
|
|
|
static const size_t tmux_seq_len = std::wcslen(tmux_seq);
|
2021-08-30 16:08:42 +00:00
|
|
|
bool is_tmux = false;
|
2016-10-31 03:21:26 +00:00
|
|
|
if (code[1] != L'k') {
|
2021-08-30 16:08:42 +00:00
|
|
|
if (wcsncmp(&code[1], tmux_seq, tmux_seq_len) == 0) {
|
|
|
|
is_tmux = true;
|
|
|
|
} else {
|
2021-08-30 14:55:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-05 01:14:04 +00:00
|
|
|
}
|
2018-06-18 05:01:32 +00:00
|
|
|
const wchar_t *const screen_name_end_sentinel = L"\x1B\\";
|
2021-08-30 16:08:42 +00:00
|
|
|
size_t offset = 2;
|
|
|
|
while (true) {
|
|
|
|
const wchar_t *screen_name_end = std::wcsstr(&code[offset], screen_name_end_sentinel);
|
|
|
|
if (screen_name_end == nullptr) {
|
|
|
|
// Consider just <esc>k to be the code.
|
|
|
|
// (note: for the tmux sequence this is broken, but since we have no idea...)
|
|
|
|
*resulting_length = 2;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// The tmux sequence requires that all escapes in the payload sequence
|
|
|
|
// be doubled. So if we have \e\e\\ that's still not the end.
|
|
|
|
if (is_tmux) {
|
|
|
|
size_t esc_count = 0;
|
|
|
|
const wchar_t *i = screen_name_end;
|
|
|
|
while (i > code && *(i - 1) == L'\x1B' && --i) esc_count++;
|
|
|
|
if (esc_count % 2 == 1) {
|
2021-11-08 19:36:01 +00:00
|
|
|
offset = screen_name_end - code + 1;
|
2021-08-30 16:08:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const wchar_t *escape_sequence_end =
|
|
|
|
screen_name_end + std::wcslen(screen_name_end_sentinel);
|
|
|
|
*resulting_length = escape_sequence_end - code;
|
|
|
|
break;
|
|
|
|
}
|
2016-10-31 03:21:26 +00:00
|
|
|
}
|
|
|
|
return true;
|
2016-05-05 01:14:04 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 16:29:35 +00:00
|
|
|
/// Operating System Command (OSC) escape codes, used by iTerm2 and others:
|
|
|
|
/// ESC followed by ], terminated by either BEL or escape + backslash.
|
|
|
|
/// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
|
|
|
/// and https://iterm2.com/documentation-escape-codes.html .
|
|
|
|
static bool is_osc_escape_seq(const wchar_t *code, size_t *resulting_length) {
|
2016-05-05 01:14:04 +00:00
|
|
|
bool found = false;
|
|
|
|
if (code[1] == ']') {
|
|
|
|
// Start at 2 to skip over <esc>].
|
|
|
|
size_t cursor = 2;
|
|
|
|
for (; code[cursor] != L'\0'; cursor++) {
|
|
|
|
// Consume a sequence of characters up to <esc>\ or <bel>.
|
2018-06-18 05:01:32 +00:00
|
|
|
if (code[cursor] == '\x07' || (code[cursor] == '\\' && code[cursor - 1] == '\x1B')) {
|
2016-05-05 01:14:04 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
*resulting_length = cursor + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2019-06-07 02:42:34 +00:00
|
|
|
/// Generic VT100 three byte sequence: CSI followed by something in the range @ through _.
|
2019-06-07 04:45:40 +00:00
|
|
|
static bool is_three_byte_escape_seq(const wchar_t *code, size_t *resulting_length) {
|
2016-05-05 01:14:04 +00:00
|
|
|
bool found = false;
|
|
|
|
if (code[1] == L'[' && (code[2] >= L'@' && code[2] <= L'_')) {
|
|
|
|
*resulting_length = 3;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic VT100 two byte sequence: <esc> followed by something in the range @ through _.
|
|
|
|
static bool is_two_byte_escape_seq(const wchar_t *code, size_t *resulting_length) {
|
|
|
|
bool found = false;
|
|
|
|
if (code[1] >= L'@' && code[1] <= L'_') {
|
|
|
|
*resulting_length = 2;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic VT100 CSI-style sequence. <esc>, followed by zero or more ASCII characters NOT in
|
|
|
|
/// the range [@,_], followed by one character in that range.
|
2022-03-08 16:06:59 +00:00
|
|
|
/// This will also catch color sequences.
|
2016-05-05 01:14:04 +00:00
|
|
|
static bool is_csi_style_escape_seq(const wchar_t *code, size_t *resulting_length) {
|
2016-10-31 03:21:26 +00:00
|
|
|
if (code[1] != L'[') {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-05 01:14:04 +00:00
|
|
|
|
2016-10-31 03:21:26 +00:00
|
|
|
// Start at 2 to skip over <esc>[
|
|
|
|
size_t cursor = 2;
|
|
|
|
for (; code[cursor] != L'\0'; cursor++) {
|
|
|
|
// Consume a sequence of ASCII characters not in the range [@, ~].
|
|
|
|
wchar_t widechar = code[cursor];
|
2016-05-05 01:14:04 +00:00
|
|
|
|
2016-10-31 03:21:26 +00:00
|
|
|
// If we're not in ASCII, just stop.
|
|
|
|
if (widechar > 127) break;
|
|
|
|
|
|
|
|
// If we're the end character, then consume it and then stop.
|
|
|
|
if (widechar >= L'@' && widechar <= L'~') {
|
|
|
|
cursor++;
|
|
|
|
break;
|
2016-05-05 01:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 03:21:26 +00:00
|
|
|
// cursor now indexes just beyond the end of the sequence (or at the terminating zero).
|
|
|
|
*resulting_length = cursor;
|
|
|
|
return true;
|
2012-10-01 10:29:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 03:20:21 +00:00
|
|
|
/// Detect whether the escape sequence sets one of the terminal attributes that affects how text is
|
|
|
|
/// displayed other than the color.
|
2017-02-03 22:41:29 +00:00
|
|
|
static bool is_visual_escape_seq(const wchar_t *code, size_t *resulting_length) {
|
|
|
|
if (!cur_term) return false;
|
2018-12-12 10:42:29 +00:00
|
|
|
const char *const esc2[] = {
|
2019-05-05 10:09:25 +00:00
|
|
|
enter_bold_mode, exit_attribute_mode, enter_underline_mode, exit_underline_mode,
|
|
|
|
enter_standout_mode, exit_standout_mode, enter_blink_mode, enter_protected_mode,
|
|
|
|
enter_italics_mode, exit_italics_mode, enter_reverse_mode, enter_shadow_mode,
|
|
|
|
exit_shadow_mode, enter_standout_mode, exit_standout_mode, enter_secure_mode,
|
|
|
|
enter_dim_mode, enter_blink_mode, enter_alt_charset_mode, exit_alt_charset_mode};
|
2017-02-03 22:41:29 +00:00
|
|
|
|
2019-11-19 21:46:47 +00:00
|
|
|
for (auto p : esc2) {
|
|
|
|
if (!p) continue;
|
2022-03-14 14:36:17 +00:00
|
|
|
// Test both padded and unpadded version, just to be safe. Most versions of fish_tparm don't
|
2017-02-03 22:41:29 +00:00
|
|
|
// actually seem to do anything these days.
|
2019-11-19 21:46:47 +00:00
|
|
|
size_t esc_seq_len =
|
2022-03-14 14:36:17 +00:00
|
|
|
std::max(try_sequence(fish_tparm(const_cast<char *>(p)), code), try_sequence(p, code));
|
2017-02-03 22:41:29 +00:00
|
|
|
if (esc_seq_len) {
|
|
|
|
*resulting_length = esc_seq_len;
|
|
|
|
return true;
|
2013-09-29 09:48:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 05:44:58 +00:00
|
|
|
|
2017-02-03 22:41:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-26 22:27:39 +00:00
|
|
|
|
2017-02-03 22:41:29 +00:00
|
|
|
/// Returns the number of characters in the escape code starting at 'code'. We only handle sequences
|
2019-05-05 10:09:25 +00:00
|
|
|
/// that begin with \x1B. If it doesn't we return zero. We also return zero if we don't recognize
|
|
|
|
/// the escape sequence based on querying terminfo and other heuristics.
|
2021-07-01 17:35:07 +00:00
|
|
|
maybe_t<size_t> escape_code_length(const wchar_t *code) {
|
2019-11-19 02:34:50 +00:00
|
|
|
assert(code != nullptr);
|
2021-08-18 00:04:10 +00:00
|
|
|
if (*code != L'\x1B') return none();
|
2017-02-03 22:41:29 +00:00
|
|
|
|
2021-07-01 17:35:07 +00:00
|
|
|
size_t esc_seq_len = 0;
|
2021-08-26 19:01:55 +00:00
|
|
|
bool found = is_visual_escape_seq(code, &esc_seq_len);
|
2017-02-04 03:20:21 +00:00
|
|
|
if (!found) found = is_screen_name_escape_seq(code, &esc_seq_len);
|
2019-04-19 16:29:35 +00:00
|
|
|
if (!found) found = is_osc_escape_seq(code, &esc_seq_len);
|
2019-06-07 04:45:40 +00:00
|
|
|
if (!found) found = is_three_byte_escape_seq(code, &esc_seq_len);
|
2017-02-04 03:20:21 +00:00
|
|
|
if (!found) found = is_csi_style_escape_seq(code, &esc_seq_len);
|
|
|
|
if (!found) found = is_two_byte_escape_seq(code, &esc_seq_len);
|
2021-08-18 00:04:10 +00:00
|
|
|
|
|
|
|
return found ? maybe_t<size_t>{esc_seq_len} : none();
|
2021-07-01 17:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t layout_cache_t::escape_code_length(const wchar_t *code) {
|
|
|
|
assert(code != nullptr);
|
|
|
|
if (*code != L'\x1B') return 0;
|
|
|
|
|
|
|
|
size_t esc_seq_len = this->find_escape_code(code);
|
|
|
|
if (esc_seq_len) return esc_seq_len;
|
|
|
|
|
|
|
|
auto found = ::escape_code_length(code);
|
2022-10-08 16:56:38 +00:00
|
|
|
if (found.has_value()) {
|
2021-07-01 17:35:07 +00:00
|
|
|
this->add_escape_code(wcstring(code, *found));
|
|
|
|
esc_seq_len = *found;
|
|
|
|
}
|
2017-02-04 03:20:21 +00:00
|
|
|
return esc_seq_len;
|
2013-09-29 09:48:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
const layout_cache_t::prompt_cache_entry_t *layout_cache_t::find_prompt_layout(
|
|
|
|
const wcstring &input, size_t max_line_width) {
|
2020-06-01 13:52:15 +00:00
|
|
|
auto start = prompt_cache_.begin();
|
|
|
|
auto end = prompt_cache_.end();
|
2018-02-04 23:43:03 +00:00
|
|
|
for (auto iter = start; iter != end; ++iter) {
|
2020-05-31 00:10:44 +00:00
|
|
|
if (iter->text == input && iter->max_line_width == max_line_width) {
|
2018-02-04 23:43:03 +00:00
|
|
|
// Found it. Move it to the front if not already there.
|
|
|
|
if (iter != start) prompt_cache_.splice(start, prompt_cache_, iter);
|
2020-05-31 00:10:44 +00:00
|
|
|
return &*prompt_cache_.begin();
|
2018-02-04 23:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 00:10:44 +00:00
|
|
|
return nullptr;
|
2018-02-04 23:43:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
void layout_cache_t::add_prompt_layout(prompt_cache_entry_t entry) {
|
|
|
|
prompt_cache_.emplace_front(std::move(entry));
|
2018-02-04 23:43:03 +00:00
|
|
|
if (prompt_cache_.size() > prompt_cache_max_size) {
|
|
|
|
prompt_cache_.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
/// \return whether \p c ends a measuring run.
|
|
|
|
static bool is_run_terminator(wchar_t c) {
|
|
|
|
return c == L'\0' || c == L'\n' || c == L'\r' || c == L'\f';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Measure a run of characters in \p input starting at \p start.
|
|
|
|
/// Stop when we reach a run terminator, and return its index in \p out_end (if not null).
|
|
|
|
/// Note \0 is a run terminator so there will always be one.
|
|
|
|
/// We permit escape sequences to have run terminators other than \0. That is, escape sequences may
|
|
|
|
/// have embedded newlines, etc.; it's unclear if this is possible but we allow it.
|
|
|
|
static size_t measure_run_from(const wchar_t *input, size_t start, size_t *out_end,
|
|
|
|
layout_cache_t &cache) {
|
|
|
|
size_t width = 0;
|
|
|
|
size_t idx = start;
|
2022-04-09 00:00:54 +00:00
|
|
|
for (; !is_run_terminator(input[idx]); idx++) {
|
2020-05-31 00:10:44 +00:00
|
|
|
if (input[idx] == L'\x1B') {
|
|
|
|
// This is the start of an escape code; we assume it has width 0.
|
|
|
|
// -1 because we are going to increment in the loop.
|
|
|
|
size_t len = cache.escape_code_length(&input[idx]);
|
|
|
|
if (len > 0) idx += len - 1;
|
|
|
|
} else if (input[idx] == L'\t') {
|
|
|
|
width = next_tab_stop(width);
|
|
|
|
} else {
|
|
|
|
// Ordinary char. Add its width with care to ignore control chars which have width -1.
|
2021-09-23 10:57:44 +00:00
|
|
|
auto w = fish_wcwidth_visible(input[idx]);
|
|
|
|
// A backspace at the start of the line does nothing.
|
|
|
|
if (w != -1 || width > 0) {
|
2021-11-08 19:36:01 +00:00
|
|
|
width += w;
|
2021-09-23 10:57:44 +00:00
|
|
|
}
|
2020-05-31 00:10:44 +00:00
|
|
|
}
|
2017-02-04 03:20:21 +00:00
|
|
|
}
|
2020-05-31 00:10:44 +00:00
|
|
|
if (out_end) *out_end = idx;
|
|
|
|
return width;
|
|
|
|
}
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
/// Attempt to truncate the prompt run \p run, which has width \p width, to \p no more than
|
|
|
|
/// desired_width. \return the resulting width and run by reference.
|
|
|
|
static void truncate_run(wcstring *run, size_t desired_width, size_t *width,
|
|
|
|
layout_cache_t &cache) {
|
|
|
|
size_t curr_width = *width;
|
|
|
|
if (curr_width <= desired_width) {
|
|
|
|
return;
|
|
|
|
}
|
2012-11-25 00:58:30 +00:00
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
// Bravely prepend ellipsis char and skip it.
|
|
|
|
// Ellipsis is always width 1.
|
|
|
|
wchar_t ellipsis = get_ellipsis_char();
|
|
|
|
run->insert(0, 1, ellipsis); // index, count, char
|
|
|
|
curr_width += 1;
|
|
|
|
|
|
|
|
// Start removing characters after ellipsis.
|
|
|
|
// Note we modify 'run' inside this loop.
|
|
|
|
size_t idx = 1;
|
|
|
|
while (curr_width > desired_width && idx < run->size()) {
|
|
|
|
wchar_t c = run->at(idx);
|
|
|
|
assert(!is_run_terminator(c) && "Should not have run terminator inside run");
|
|
|
|
if (c == L'\x1B') {
|
|
|
|
size_t len = cache.escape_code_length(run->c_str() + idx);
|
|
|
|
idx += std::max(len, (size_t)1);
|
|
|
|
} else if (c == '\t') {
|
|
|
|
// Tabs would seem to be quite annoying to measure while truncating.
|
|
|
|
// We simply remove these and start over.
|
|
|
|
run->erase(idx, 1);
|
|
|
|
curr_width = measure_run_from(run->c_str(), 0, nullptr, cache);
|
|
|
|
idx = 0;
|
|
|
|
} else {
|
2021-09-23 10:57:44 +00:00
|
|
|
size_t char_width = fish_wcwidth_visible(c);
|
2020-05-31 00:10:44 +00:00
|
|
|
curr_width -= std::min(curr_width, char_width);
|
|
|
|
run->erase(idx, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*width = curr_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt_layout_t layout_cache_t::calc_prompt_layout(const wcstring &prompt_str,
|
|
|
|
wcstring *out_trunc_prompt,
|
|
|
|
size_t max_line_width) {
|
2020-06-07 19:56:14 +00:00
|
|
|
// FIXME: we could avoid allocating trunc_prompt if max_line_width is SIZE_T_MAX.
|
2020-05-31 00:10:44 +00:00
|
|
|
if (const auto *entry = this->find_prompt_layout(prompt_str, max_line_width)) {
|
|
|
|
if (out_trunc_prompt) out_trunc_prompt->assign(entry->trunc_text);
|
|
|
|
return entry->layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t prompt_len = prompt_str.size();
|
2019-07-31 18:06:39 +00:00
|
|
|
const wchar_t *prompt = prompt_str.c_str();
|
2020-05-31 00:10:44 +00:00
|
|
|
|
2020-11-12 18:52:50 +00:00
|
|
|
prompt_layout_t layout = {{}, 0, 0};
|
2020-05-31 00:10:44 +00:00
|
|
|
wcstring trunc_prompt;
|
|
|
|
|
|
|
|
size_t run_start = 0;
|
|
|
|
while (run_start < prompt_len) {
|
|
|
|
size_t run_end;
|
|
|
|
size_t line_width = measure_run_from(prompt, run_start, &run_end, *this);
|
|
|
|
if (line_width <= max_line_width) {
|
|
|
|
// No truncation needed on this line.
|
|
|
|
trunc_prompt.append(&prompt[run_start], run_end - run_start);
|
2016-05-03 19:31:07 +00:00
|
|
|
} else {
|
2020-05-31 00:10:44 +00:00
|
|
|
// Truncation needed on this line.
|
|
|
|
wcstring run_storage(&prompt[run_start], run_end - run_start);
|
|
|
|
truncate_run(&run_storage, max_line_width, &line_width, *this);
|
|
|
|
trunc_prompt.append(run_storage);
|
|
|
|
}
|
|
|
|
layout.max_line_width = std::max(layout.max_line_width, line_width);
|
|
|
|
layout.last_line_width = line_width;
|
|
|
|
|
|
|
|
wchar_t endc = prompt[run_end];
|
|
|
|
if (endc) {
|
2020-10-25 23:22:28 +00:00
|
|
|
if (endc == L'\n' || endc == L'\f') {
|
2020-11-12 18:52:50 +00:00
|
|
|
layout.line_breaks.push_back(trunc_prompt.size());
|
2021-09-15 15:41:35 +00:00
|
|
|
// If the prompt ends in a new line, that's one empy last line.
|
|
|
|
if (run_end == prompt_str.size() - 1) layout.last_line_width = 0;
|
2020-10-25 23:22:28 +00:00
|
|
|
}
|
2020-05-31 00:10:44 +00:00
|
|
|
trunc_prompt.push_back(endc);
|
|
|
|
run_start = run_end + 1;
|
|
|
|
} else {
|
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2020-05-31 00:10:44 +00:00
|
|
|
this->add_prompt_layout(prompt_cache_entry_t{prompt, max_line_width, trunc_prompt, layout});
|
|
|
|
if (out_trunc_prompt) {
|
|
|
|
*out_trunc_prompt = std::move(trunc_prompt);
|
|
|
|
}
|
|
|
|
return layout;
|
2012-10-16 02:25:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 22:14:37 +00:00
|
|
|
static size_t calc_prompt_lines(const wcstring &prompt) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Hack for the common case where there's no newline at all. I don't know if a newline can
|
|
|
|
// appear in an escape sequence, so if we detect a newline we have to defer to
|
2020-11-12 18:52:50 +00:00
|
|
|
// calc_prompt_width_and_lines.
|
2012-10-16 02:25:56 +00:00
|
|
|
size_t result = 1;
|
2020-05-30 21:59:35 +00:00
|
|
|
if (prompt.find_first_of(L"\n\f") != wcstring::npos) {
|
2020-11-12 18:52:50 +00:00
|
|
|
result = layout_cache_t::shared.calc_prompt_layout(prompt).line_breaks.size() + 1;
|
2012-10-16 02:25:56 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2006-10-04 23:33:12 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Stat stdout and stderr and save result. This should be done before calling a function that may
|
|
|
|
/// cause output.
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::save_status() {
|
|
|
|
fstat(STDOUT_FILENO, &this->prev_buff_1);
|
|
|
|
fstat(STDERR_FILENO, &this->prev_buff_2);
|
2006-10-04 23:33:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Stat stdout and stderr and compare result to previous result in reader_save_status. Repaint if
|
|
|
|
/// modification time has changed.
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::check_status() {
|
2012-11-19 00:30:30 +00:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2016-06-17 20:08:25 +00:00
|
|
|
if (!has_working_tty_timestamps) {
|
|
|
|
// We can't reliably determine if the terminal has been written to behind our back so we
|
|
|
|
// just assume that hasn't happened and hope for the best. This is important for multi-line
|
|
|
|
// prompts to work correctly.
|
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2020-04-28 18:25:32 +00:00
|
|
|
struct stat post_buff_1 {};
|
|
|
|
struct stat post_buff_2 {};
|
|
|
|
fstat(STDOUT_FILENO, &post_buff_1);
|
|
|
|
fstat(STDERR_FILENO, &post_buff_2);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
bool changed = (this->prev_buff_1.st_mtime != post_buff_1.st_mtime) ||
|
|
|
|
(this->prev_buff_2.st_mtime != post_buff_2.st_mtime);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
#if defined HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
|
2021-09-21 19:51:04 +00:00
|
|
|
changed = changed ||
|
|
|
|
this->prev_buff_1.st_mtimespec.tv_nsec != post_buff_1.st_mtimespec.tv_nsec ||
|
|
|
|
this->prev_buff_2.st_mtimespec.tv_nsec != post_buff_2.st_mtimespec.tv_nsec;
|
2016-05-03 19:31:07 +00:00
|
|
|
#elif defined HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
|
2021-09-21 19:51:04 +00:00
|
|
|
changed = changed || this->prev_buff_1.st_mtim.tv_nsec != post_buff_1.st_mtim.tv_nsec ||
|
|
|
|
this->prev_buff_2.st_mtim.tv_nsec != post_buff_2.st_mtim.tv_nsec;
|
2016-05-03 19:31:07 +00:00
|
|
|
#endif
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
if (changed) {
|
|
|
|
// Ok, someone has been messing with our screen. We will want to repaint. However, we do not
|
|
|
|
// know where the cursor is. It is our best bet that we are still on the same line, so we
|
|
|
|
// move to the beginning of the line, reset the modelled screen contents, and then set the
|
|
|
|
// modeled cursor y-pos to its earlier value.
|
2021-09-21 19:51:04 +00:00
|
|
|
int prev_line = this->actual.cursor.y;
|
|
|
|
this->reset_line(true /* repaint prompt */);
|
|
|
|
this->actual.cursor.y = prev_line;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2006-10-04 23:33:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::desired_append_char(wchar_t b, highlight_spec_t c, int indent, size_t prompt_width,
|
|
|
|
size_t bwidth) {
|
|
|
|
int line_no = this->desired.cursor.y;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-05 01:14:04 +00:00
|
|
|
if (b == L'\n') {
|
|
|
|
// Current line is definitely hard wrapped.
|
2019-08-10 21:32:55 +00:00
|
|
|
// Create the next line.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.create_line(this->desired.cursor.y + 1);
|
|
|
|
this->desired.line(this->desired.cursor.y).is_soft_wrapped = false;
|
|
|
|
int line_no = ++this->desired.cursor.y;
|
|
|
|
this->desired.cursor.x = 0;
|
2021-10-28 08:46:29 +00:00
|
|
|
size_t indentation = prompt_width + static_cast<size_t>(indent) * INDENT_STEP;
|
2021-09-21 19:51:04 +00:00
|
|
|
line_t &line = this->desired.line(line_no);
|
2019-06-07 04:27:13 +00:00
|
|
|
line.indentation = indentation;
|
|
|
|
for (size_t i = 0; i < indentation; i++) {
|
2021-09-21 19:51:04 +00:00
|
|
|
desired_append_char(L' ', highlight_spec_t{}, indent, prompt_width, 1);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-05 01:14:04 +00:00
|
|
|
} else if (b == L'\r') {
|
2021-09-21 19:51:04 +00:00
|
|
|
line_t ¤t = this->desired.line(line_no);
|
2016-05-05 01:14:04 +00:00
|
|
|
current.clear();
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.cursor.x = 0;
|
2016-05-05 01:14:04 +00:00
|
|
|
} else {
|
2021-09-21 19:51:04 +00:00
|
|
|
int screen_width = this->desired.screen_width;
|
2019-05-11 08:16:22 +00:00
|
|
|
int cw = bwidth;
|
2016-05-05 01:14:04 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.create_line(line_no);
|
2016-05-05 01:14:04 +00:00
|
|
|
|
|
|
|
// Check if we are at the end of the line. If so, continue on the next line.
|
2021-09-21 19:51:04 +00:00
|
|
|
if ((this->desired.cursor.x + cw) > screen_width) {
|
2016-05-05 01:14:04 +00:00
|
|
|
// Current line is soft wrapped (assuming we support it).
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.line(this->desired.cursor.y).is_soft_wrapped = true;
|
2016-05-05 01:14:04 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
line_no = static_cast<int>(this->desired.line_count());
|
|
|
|
this->desired.add_line();
|
|
|
|
this->desired.cursor.y++;
|
|
|
|
this->desired.cursor.x = 0;
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
line_t &line = this->desired.line(line_no);
|
2016-05-05 01:14:04 +00:00
|
|
|
line.append(b, c);
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.cursor.x += cw;
|
2012-11-19 08:31:03 +00:00
|
|
|
|
2016-05-05 01:14:04 +00:00
|
|
|
// Maybe wrap the cursor to the next line, even if the line itself did not wrap. This
|
|
|
|
// avoids wonkiness in the last column.
|
2021-09-21 19:51:04 +00:00
|
|
|
if (this->desired.cursor.x >= screen_width) {
|
2016-05-05 01:14:04 +00:00
|
|
|
line.is_soft_wrapped = true;
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.cursor.x = 0;
|
|
|
|
this->desired.cursor.y++;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2006-10-01 16:02:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::move(int new_x, int new_y) {
|
|
|
|
if (this->actual.cursor.x == new_x && this->actual.cursor.y == new_y) return;
|
2016-05-03 19:31:07 +00:00
|
|
|
|
2021-09-28 19:50:27 +00:00
|
|
|
const scoped_buffer_t buffering(outp());
|
2018-10-06 20:32:08 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// If we are at the end of our window, then either the cursor stuck to the edge or it didn't. We
|
|
|
|
// don't know! We can fix it up though.
|
2021-09-21 19:51:04 +00:00
|
|
|
if (this->actual.cursor.x == this->actual.screen_width) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Either issue a cr to go back to the beginning of this line, or a nl to go to the
|
|
|
|
// beginning of the next one, depending on what we think is more efficient.
|
2021-09-21 19:51:04 +00:00
|
|
|
if (new_y <= this->actual.cursor.y) {
|
|
|
|
this->outp().push_back('\r');
|
2016-05-03 19:31:07 +00:00
|
|
|
} else {
|
2021-09-21 19:51:04 +00:00
|
|
|
this->outp().push_back('\n');
|
|
|
|
this->actual.cursor.y++;
|
2012-12-06 10:54:25 +00:00
|
|
|
}
|
2016-05-03 19:31:07 +00:00
|
|
|
// Either way we're not in the first column.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.x = 0;
|
2012-12-06 10:54:25 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
int i;
|
|
|
|
int x_steps, y_steps;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2018-12-12 10:42:29 +00:00
|
|
|
const char *str;
|
2021-09-21 19:51:04 +00:00
|
|
|
auto &outp = this->outp();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
y_steps = new_y - this->actual.cursor.y;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
if (y_steps < 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
str = cursor_up;
|
2019-04-19 16:40:30 +00:00
|
|
|
} else if (y_steps > 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
str = cursor_down;
|
2019-05-28 02:47:13 +00:00
|
|
|
if ((shell_modes.c_oflag & ONLCR) != 0 &&
|
|
|
|
std::strcmp(str, "\n") == 0) { // See GitHub issue #4505.
|
2019-04-19 02:42:18 +00:00
|
|
|
// Most consoles use a simple newline as the cursor down escape.
|
|
|
|
// If ONLCR is enabled (which it normally is) this will of course
|
|
|
|
// also move the cursor to the beginning of the line.
|
2019-04-19 16:40:30 +00:00
|
|
|
// We could do:
|
|
|
|
// if (std::strcmp(cursor_up, "\x1B[A") == 0) str = "\x1B[B";
|
|
|
|
// else ... but that doesn't work for unknown reasons.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.x = 0;
|
2019-04-19 02:42:18 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
for (i = 0; i < abs(y_steps); i++) {
|
2023-05-28 23:13:54 +00:00
|
|
|
outp.writembs(str);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
x_steps = new_x - this->actual.cursor.x;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
if (x_steps && new_x == 0) {
|
2018-10-06 20:32:08 +00:00
|
|
|
outp.push_back('\r');
|
2012-11-19 00:30:30 +00:00
|
|
|
x_steps = 0;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-11-19 02:34:50 +00:00
|
|
|
const char *multi_str = nullptr;
|
2016-05-03 19:31:07 +00:00
|
|
|
if (x_steps < 0) {
|
2012-11-19 00:30:30 +00:00
|
|
|
str = cursor_left;
|
2014-05-09 21:37:23 +00:00
|
|
|
multi_str = parm_left_cursor;
|
2016-05-03 19:31:07 +00:00
|
|
|
} else {
|
2012-11-19 00:30:30 +00:00
|
|
|
str = cursor_right;
|
2014-05-09 21:37:23 +00:00
|
|
|
multi_str = parm_right_cursor;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 19:31:07 +00:00
|
|
|
|
2014-05-09 21:37:23 +00:00
|
|
|
// Use the bulk ('multi') output for cursor movement if it is supported and it would be shorter
|
2016-05-03 19:31:07 +00:00
|
|
|
// Note that this is required to avoid some visual glitches in iTerm (issue #1448).
|
2019-11-19 02:34:50 +00:00
|
|
|
bool use_multi = multi_str != nullptr && multi_str[0] != '\0' &&
|
2019-05-05 10:09:25 +00:00
|
|
|
abs(x_steps) * std::strlen(str) > std::strlen(multi_str);
|
2017-02-16 04:09:26 +00:00
|
|
|
if (use_multi && cur_term) {
|
2022-03-14 14:36:17 +00:00
|
|
|
char *multi_param = fish_tparm(const_cast<char *>(multi_str), abs(x_steps));
|
2018-10-06 20:32:08 +00:00
|
|
|
writembs(outp, multi_param);
|
2016-05-03 19:31:07 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < abs(x_steps); i++) {
|
2018-10-06 20:32:08 +00:00
|
|
|
writembs(outp, str);
|
2014-05-09 21:37:23 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.x = new_x;
|
|
|
|
this->actual.cursor.y = new_y;
|
2006-10-01 16:02:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Convert a wide character to a multibyte string and append it to the buffer.
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::write_char(wchar_t c, size_t width) {
|
2021-09-28 19:50:27 +00:00
|
|
|
scoped_buffer_t buffering(outp());
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.x += width;
|
|
|
|
this->outp().writech(c);
|
|
|
|
if (this->actual.cursor.x == this->actual.screen_width && allow_soft_wrap()) {
|
|
|
|
this->soft_wrap_location = screen_data_t::cursor_t{0, this->actual.cursor.y + 1};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Note that our cursor position may be a lie: Apple Terminal makes the right cursor stick
|
|
|
|
// to the margin, while Ubuntu makes it "go off the end" (but still doesn't wrap). We rely
|
|
|
|
// on s_move to fix this up.
|
|
|
|
} else {
|
2021-09-21 19:51:04 +00:00
|
|
|
this->soft_wrap_location = none();
|
2012-10-01 10:29:18 +00:00
|
|
|
}
|
2012-07-15 17:45:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 20:32:08 +00:00
|
|
|
/// Send the specified string through tputs and append the output to the screen's outputter.
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::write_mbs(const char *s) { writembs(this->outp(), s); }
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Convert a wide string to a multibyte string and append it to the buffer.
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::write_str(const wchar_t *s) { this->outp().writestr(s); }
|
2023-05-28 23:13:54 +00:00
|
|
|
void screen_t::write_str(const wcstring &s) { this->outp().writestr(s.c_str()); }
|
2006-10-01 16:02:58 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Returns the length of the "shared prefix" of the two lines, which is the run of matching text
|
|
|
|
/// and colors. If the prefix ends on a combining character, do not include the previous character
|
|
|
|
/// in the prefix.
|
|
|
|
static size_t line_shared_prefix(const line_t &a, const line_t &b) {
|
2012-07-15 17:45:18 +00:00
|
|
|
size_t idx, max = std::min(a.size(), b.size());
|
2016-05-03 19:31:07 +00:00
|
|
|
for (idx = 0; idx < max; idx++) {
|
2012-07-15 17:45:18 +00:00
|
|
|
wchar_t ac = a.char_at(idx), bc = b.char_at(idx);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// We're done if the text or colors are different.
|
2019-05-10 15:21:06 +00:00
|
|
|
if (ac != bc || a.color_at(idx) != b.color_at(idx)) {
|
|
|
|
if (idx > 0) {
|
|
|
|
const line_t *c = nullptr;
|
2019-05-28 02:47:13 +00:00
|
|
|
// Possible combining mark, go back until we hit _two_ printable characters or idx
|
|
|
|
// of 0.
|
2019-05-10 15:21:06 +00:00
|
|
|
if (fish_wcwidth(a.char_at(idx)) < 1) {
|
|
|
|
c = &a;
|
|
|
|
} else if (fish_wcwidth(b.char_at(idx)) < 1) {
|
|
|
|
c = &b;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c) {
|
2019-05-28 02:47:13 +00:00
|
|
|
while (idx > 1 && (fish_wcwidth(c->char_at(idx - 1)) < 1 ||
|
|
|
|
fish_wcwidth(c->char_at(idx)) < 1))
|
|
|
|
idx--;
|
2019-05-10 15:21:06 +00:00
|
|
|
if (idx == 1 && fish_wcwidth(c->char_at(idx)) < 1) idx = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-07-15 17:45:18 +00:00
|
|
|
}
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// We are about to output one or more characters onto the screen at the given x, y. If we are at the
|
|
|
|
// end of previous line, and the previous line is marked as soft wrapping, then tweak the screen so
|
|
|
|
// we believe we are already in the target position. This lets the terminal take care of wrapping,
|
|
|
|
// which means that if you copy and paste the text, it won't have an embedded newline.
|
2021-09-21 19:51:04 +00:00
|
|
|
bool screen_t::handle_soft_wrap(int x, int y) {
|
|
|
|
if (this->soft_wrap_location && x == this->soft_wrap_location->x &&
|
|
|
|
y == this->soft_wrap_location->y) { //!OCLINT
|
2016-05-03 19:31:07 +00:00
|
|
|
// We can soft wrap; but do we want to?
|
2021-09-21 19:51:04 +00:00
|
|
|
if (this->desired.line(y - 1).is_soft_wrapped && allow_soft_wrap()) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Yes. Just update the actual cursor; that will cause us to elide emitting the commands
|
|
|
|
// to move here, so we will just output on "one big line" (which the terminal soft
|
|
|
|
// wraps.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor = this->soft_wrap_location.value();
|
2012-10-01 10:29:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Update the screen to match the desired output.
|
2021-09-23 03:46:36 +00:00
|
|
|
void screen_t::update(const wcstring &left_prompt, const wcstring &right_prompt,
|
|
|
|
const environment_t &vars) {
|
2020-08-04 00:34:27 +00:00
|
|
|
// Helper function to set a resolved color, using the caching resolver.
|
|
|
|
highlight_color_resolver_t color_resolver{};
|
|
|
|
auto set_color = [&](highlight_spec_t c) {
|
2021-09-21 19:51:04 +00:00
|
|
|
this->outp().set_color(color_resolver.resolve_spec(c, false, vars),
|
|
|
|
color_resolver.resolve_spec(c, true, vars));
|
2020-08-04 00:34:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
layout_cache_t &cached_layouts = layout_cache_t::shared;
|
2021-09-28 19:50:27 +00:00
|
|
|
const scoped_buffer_t buffering(outp());
|
2020-05-31 00:10:44 +00:00
|
|
|
|
|
|
|
// Determine size of left and right prompt. Note these have already been truncated.
|
2020-10-25 23:22:28 +00:00
|
|
|
const prompt_layout_t left_prompt_layout = cached_layouts.calc_prompt_layout(left_prompt);
|
|
|
|
const size_t left_prompt_width = left_prompt_layout.last_line_width;
|
2017-02-04 03:20:21 +00:00
|
|
|
const size_t right_prompt_width =
|
2020-05-31 00:10:44 +00:00
|
|
|
cached_layouts.calc_prompt_layout(right_prompt).last_line_width;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Figure out how many following lines we need to clear (probably 0).
|
2021-09-21 19:51:04 +00:00
|
|
|
size_t actual_lines_before_reset = this->actual_lines_before_reset;
|
|
|
|
this->actual_lines_before_reset = 0;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
bool need_clear_lines = this->need_clear_lines;
|
|
|
|
bool need_clear_screen = this->need_clear_screen;
|
2012-11-25 09:26:58 +00:00
|
|
|
bool has_cleared_screen = false;
|
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
const int screen_width = this->desired.screen_width;
|
2020-06-07 21:53:42 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
if (this->actual.screen_width != screen_width) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Ensure we don't issue a clear screen for the very first output, to avoid issue #402.
|
2021-09-21 19:51:04 +00:00
|
|
|
if (this->actual.screen_width > 0) {
|
2012-11-25 00:42:25 +00:00
|
|
|
need_clear_screen = true;
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(0, 0);
|
|
|
|
this->reset_line();
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
need_clear_lines = need_clear_lines || this->need_clear_lines;
|
|
|
|
need_clear_screen = need_clear_screen || this->need_clear_screen;
|
2012-11-25 00:42:25 +00:00
|
|
|
}
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.screen_width = screen_width;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
this->need_clear_lines = false;
|
|
|
|
this->need_clear_screen = false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Determine how many lines have stuff on them; we need to clear lines with stuff that we don't
|
|
|
|
// want.
|
2021-09-21 19:51:04 +00:00
|
|
|
const size_t lines_with_stuff = std::max(actual_lines_before_reset, this->actual.line_count());
|
|
|
|
if (this->desired.line_count() < lines_with_stuff) need_clear_screen = true;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2020-04-29 21:22:54 +00:00
|
|
|
// Output the left prompt if it has changed.
|
2021-09-21 19:51:04 +00:00
|
|
|
if (left_prompt != this->actual_left_prompt) {
|
|
|
|
this->move(0, 0);
|
2020-11-12 18:52:50 +00:00
|
|
|
size_t start = 0;
|
|
|
|
for (const size_t line_break : left_prompt_layout.line_breaks) {
|
2021-09-21 19:51:04 +00:00
|
|
|
this->write_str(left_prompt.substr(start, line_break - start));
|
2020-11-12 18:52:50 +00:00
|
|
|
if (clr_eol) {
|
2021-09-21 19:51:04 +00:00
|
|
|
this->write_mbs(clr_eol);
|
2020-10-25 23:22:28 +00:00
|
|
|
}
|
2020-11-12 18:52:50 +00:00
|
|
|
start = line_break;
|
2020-10-25 23:22:28 +00:00
|
|
|
}
|
2021-09-21 19:51:04 +00:00
|
|
|
this->write_str(left_prompt.substr(start));
|
|
|
|
this->actual_left_prompt = left_prompt;
|
|
|
|
this->actual.cursor.x = static_cast<int>(left_prompt_width);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2020-04-29 21:22:54 +00:00
|
|
|
// Output all lines.
|
2021-09-21 19:51:04 +00:00
|
|
|
for (size_t i = 0; i < this->desired.line_count(); i++) {
|
|
|
|
const line_t &o_line = this->desired.line(i);
|
|
|
|
line_t &s_line = this->actual.create_line(i);
|
2016-05-04 04:31:32 +00:00
|
|
|
size_t start_pos = i == 0 ? left_prompt_width : 0;
|
2012-07-15 17:45:18 +00:00
|
|
|
int current_width = 0;
|
2019-06-07 04:27:13 +00:00
|
|
|
bool has_cleared_line = false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// If this is the last line, maybe we should clear the screen.
|
2020-04-29 21:22:54 +00:00
|
|
|
// Don't issue clr_eos if we think the cursor will end up in the last column - see #6951.
|
2016-05-03 19:31:07 +00:00
|
|
|
const bool should_clear_screen_this_line =
|
2021-09-21 19:51:04 +00:00
|
|
|
need_clear_screen && i + 1 == this->desired.line_count() && clr_eos != nullptr &&
|
|
|
|
!(this->desired.cursor.x == 0 &&
|
|
|
|
this->desired.cursor.y == static_cast<int>(this->desired.line_count()));
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2020-04-29 21:22:54 +00:00
|
|
|
// skip_remaining is how many columns are unchanged on this line.
|
2016-05-03 19:31:07 +00:00
|
|
|
// Note that skip_remaining is a width, not a character count.
|
2012-08-01 23:32:52 +00:00
|
|
|
size_t skip_remaining = start_pos;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-05-11 08:25:27 +00:00
|
|
|
const size_t shared_prefix = line_shared_prefix(o_line, s_line);
|
2019-06-07 04:27:13 +00:00
|
|
|
size_t skip_prefix = shared_prefix;
|
|
|
|
if (shared_prefix < o_line.indentation) {
|
2019-10-13 22:50:48 +00:00
|
|
|
if (o_line.indentation > s_line.indentation && !has_cleared_screen && clr_eol &&
|
|
|
|
clr_eos) {
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(highlight_spec_t{});
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(0, static_cast<int>(i));
|
|
|
|
this->write_mbs(should_clear_screen_this_line ? clr_eos : clr_eol);
|
2019-06-07 04:27:13 +00:00
|
|
|
has_cleared_screen = should_clear_screen_this_line;
|
|
|
|
has_cleared_line = true;
|
|
|
|
}
|
|
|
|
skip_prefix = o_line.indentation;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2019-10-02 18:45:01 +00:00
|
|
|
// Compute how much we should skip. At a minimum we skip over the prompt. But also skip
|
|
|
|
// over the shared prefix of what we want to output now, and what we output before, to
|
|
|
|
// avoid repeatedly outputting it.
|
|
|
|
if (skip_prefix > 0) {
|
2020-06-07 20:53:41 +00:00
|
|
|
size_t skip_width =
|
|
|
|
shared_prefix < skip_prefix ? skip_prefix : o_line.wcswidth_min_0(shared_prefix);
|
2019-10-02 18:45:01 +00:00
|
|
|
if (skip_width > skip_remaining) skip_remaining = skip_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!should_clear_screen_this_line) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// If we're soft wrapped, and if we're going to change the first character of the next
|
|
|
|
// line, don't skip over the last two characters so that we maintain soft-wrapping.
|
2021-09-21 19:51:04 +00:00
|
|
|
if (o_line.is_soft_wrapped && i + 1 < this->desired.line_count()) {
|
2016-05-05 01:14:04 +00:00
|
|
|
bool next_line_will_change = true;
|
2021-09-21 19:51:04 +00:00
|
|
|
if (i + 1 < this->actual.line_count()) { //!OCLINT
|
|
|
|
if (line_shared_prefix(this->desired.line(i + 1), this->actual.line(i + 1)) >
|
|
|
|
0) {
|
2016-05-05 01:14:04 +00:00
|
|
|
next_line_will_change = false;
|
2012-12-06 10:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-05 01:14:04 +00:00
|
|
|
if (next_line_will_change) {
|
2021-09-21 19:51:04 +00:00
|
|
|
skip_remaining = std::min(skip_remaining,
|
|
|
|
static_cast<size_t>(this->actual.screen_width - 2));
|
2012-12-06 10:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-15 17:45:18 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Skip over skip_remaining width worth of characters.
|
2012-07-15 17:45:18 +00:00
|
|
|
size_t j = 0;
|
2016-05-03 19:31:07 +00:00
|
|
|
for (; j < o_line.size(); j++) {
|
2021-09-23 10:57:44 +00:00
|
|
|
size_t width = fish_wcwidth_visible(o_line.char_at(j));
|
2016-05-03 19:31:07 +00:00
|
|
|
if (skip_remaining < width) break;
|
2012-08-05 01:32:15 +00:00
|
|
|
skip_remaining -= width;
|
2012-07-15 17:45:18 +00:00
|
|
|
current_width += width;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Skip over zero-width characters (e.g. combining marks at the end of the prompt).
|
|
|
|
for (; j < o_line.size(); j++) {
|
2021-09-23 10:57:44 +00:00
|
|
|
int width = fish_wcwidth_visible(o_line.char_at(j));
|
2016-05-03 19:31:07 +00:00
|
|
|
if (width > 0) break;
|
2012-07-15 17:45:18 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Now actually output stuff.
|
2019-10-13 22:50:48 +00:00
|
|
|
for (;; j++) {
|
2019-06-07 04:18:09 +00:00
|
|
|
bool done = j >= o_line.size();
|
|
|
|
// Clear the screen if we have not done so yet.
|
2016-05-03 19:31:07 +00:00
|
|
|
// If we are about to output into the last column, clear the screen first. If we clear
|
|
|
|
// the screen after we output into the last column, it can erase the last character due
|
|
|
|
// to the sticky right cursor. If we clear the screen too early, we can defeat soft
|
|
|
|
// wrapping.
|
2019-10-13 22:50:48 +00:00
|
|
|
if (should_clear_screen_this_line && !has_cleared_screen &&
|
2019-11-19 01:08:16 +00:00
|
|
|
(done || j + 1 == static_cast<size_t>(screen_width))) {
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(highlight_spec_t{});
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(current_width, static_cast<int>(i));
|
|
|
|
this->write_mbs(clr_eos);
|
2012-11-25 09:26:58 +00:00
|
|
|
has_cleared_screen = true;
|
|
|
|
}
|
2019-10-13 22:50:48 +00:00
|
|
|
if (done) break;
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
this->handle_soft_wrap(current_width, static_cast<int>(i));
|
|
|
|
this->move(current_width, static_cast<int>(i));
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(o_line.color_at(j));
|
2021-09-23 10:57:44 +00:00
|
|
|
auto width = fish_wcwidth_visible(o_line.char_at(j));
|
2021-09-21 19:51:04 +00:00
|
|
|
this->write_char(o_line.char_at(j), width);
|
2019-05-11 08:16:22 +00:00
|
|
|
current_width += width;
|
2012-07-15 17:45:18 +00:00
|
|
|
}
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2012-10-03 00:30:07 +00:00
|
|
|
bool clear_remainder = false;
|
2016-05-03 19:31:07 +00:00
|
|
|
// Clear the remainder of the line if we need to clear and if we didn't write to the end of
|
|
|
|
// the line. If we did write to the end of the line, the "sticky right edge" (as part of
|
|
|
|
// auto_right_margin) means that we'll be clearing the last character we wrote!
|
2019-06-07 04:27:13 +00:00
|
|
|
if (has_cleared_screen || has_cleared_line) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Already cleared everything.
|
2012-11-08 03:59:20 +00:00
|
|
|
clear_remainder = false;
|
2016-05-03 19:31:07 +00:00
|
|
|
} else if (need_clear_lines && current_width < screen_width) {
|
2012-11-08 03:59:20 +00:00
|
|
|
clear_remainder = true;
|
2021-09-21 19:51:04 +00:00
|
|
|
} else if (right_prompt_width < this->last_right_prompt_width) {
|
2012-10-03 00:30:07 +00:00
|
|
|
clear_remainder = true;
|
2016-05-03 19:31:07 +00:00
|
|
|
} else {
|
2019-05-11 08:25:27 +00:00
|
|
|
// This wcswidth shows up strong in the profile.
|
|
|
|
// Only do it if the previous line could conceivably be wider.
|
|
|
|
// That means if it is a prefix of the current one we can skip it.
|
|
|
|
if (s_line.text.size() != shared_prefix) {
|
2020-06-07 20:53:41 +00:00
|
|
|
int prev_width = s_line.wcswidth_min_0();
|
2019-05-11 08:25:27 +00:00
|
|
|
clear_remainder = prev_width > current_width;
|
|
|
|
}
|
2012-10-03 00:30:07 +00:00
|
|
|
}
|
2018-11-28 12:08:48 +00:00
|
|
|
if (clear_remainder && clr_eol) {
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(highlight_spec_t{});
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(current_width, static_cast<int>(i));
|
|
|
|
this->write_mbs(clr_eol);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Output any rprompt if this is the first line.
|
2016-10-31 03:21:26 +00:00
|
|
|
if (i == 0 && right_prompt_width > 0) { //!OCLINT(Use early exit/continue)
|
2021-05-17 15:34:41 +00:00
|
|
|
// Move the cursor to the beginning of the line first to be independent of the width.
|
|
|
|
// This helps prevent staircase effects if fish and the terminal disagree.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(0, 0);
|
|
|
|
this->move(static_cast<int>(screen_width - right_prompt_width), static_cast<int>(i));
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(highlight_spec_t{});
|
2021-09-21 19:51:04 +00:00
|
|
|
this->write_str(right_prompt);
|
|
|
|
this->actual.cursor.x += right_prompt_width;
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// We output in the last column. Some terms (Linux) push the cursor further right, past
|
|
|
|
// the window. Others make it "stick." Since we don't really know which is which, issue
|
|
|
|
// a cr so it goes back to the left.
|
|
|
|
//
|
|
|
|
// However, if the user is resizing the window smaller, then it's possible the cursor
|
|
|
|
// wrapped. If so, then a cr will go to the beginning of the following line! So instead
|
|
|
|
// issue a bunch of "move left" commands to get back onto the line, and then jump to the
|
|
|
|
// front of it.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(this->actual.cursor.x - static_cast<int>(right_prompt_width),
|
|
|
|
this->actual.cursor.y);
|
|
|
|
this->write_str(L"\r");
|
|
|
|
this->actual.cursor.x = 0;
|
2012-11-05 07:21:37 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-05-17 21:17:13 +00:00
|
|
|
// Also move the cursor to the beginning of the line here,
|
|
|
|
// in case we're wrong about the width anywhere.
|
Add workaround for Midnight Commander's issue with prompt extraction
When we draw the prompt, we move the cursor to the actual
position *we* think it is by issuing a carriage return (via
`move(0,0)`), and then going forward until we hit the spot.
This helps when the terminal and fish disagree on the width of the
prompt, because we are now definitely in the correct place, so we can
only overwrite a bit of the prompt (if it renders longer than we
expected) or leave space after the prompt. Both of these are benign in
comparison to staircase effects we would otherwise get.
Unfortunately, midnight commander ("mc") tries to extract the last
line of the prompt, and does so in a way that is overly naive - it
resets everything to 0 when it sees a `\r`, and doesn't account for
cursor movement. In effect it's playing a terminal, but not committing
to the bit.
Since this has been an open request in mc for quite a while, we hack
around it, by checking the $MC_SID environment variable.
If we see it, we skip the clearing. We end up most likely doing
relative movement from where we think we are, and in most cases it
should be *fine*.
2023-02-04 17:57:41 +00:00
|
|
|
// Don't do it when running in midnight_commander because of
|
|
|
|
// https://midnight-commander.org/ticket/4258.
|
|
|
|
if (!midnight_commander_hack) {
|
|
|
|
this->move(0, 0);
|
|
|
|
}
|
2021-05-17 21:17:13 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Clear remaining lines (if any) if we haven't cleared the screen.
|
2019-11-01 16:28:41 +00:00
|
|
|
if (!has_cleared_screen && need_clear_screen && clr_eol) {
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(highlight_spec_t{});
|
2021-09-21 19:51:04 +00:00
|
|
|
for (size_t i = this->desired.line_count(); i < lines_with_stuff; i++) {
|
|
|
|
this->move(0, static_cast<int>(i));
|
|
|
|
this->write_mbs(clr_eol);
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
this->move(this->desired.cursor.x, this->desired.cursor.y);
|
2020-08-04 00:34:27 +00:00
|
|
|
set_color(highlight_spec_t{});
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// We have now synced our actual screen against our desired screen. Note that this is a big
|
|
|
|
// assignment!
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual = this->desired;
|
|
|
|
this->last_right_prompt_width = right_prompt_width;
|
2006-10-01 16:02:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
/// Returns true if we are using a dumb terminal.
|
2018-02-19 02:33:04 +00:00
|
|
|
static bool is_dumb() {
|
2017-02-16 04:09:26 +00:00
|
|
|
if (!cur_term) return true;
|
|
|
|
return !cursor_up || !cursor_down || !cursor_left || !cursor_right;
|
|
|
|
}
|
2007-08-01 19:07:54 +00:00
|
|
|
|
2021-09-28 19:50:27 +00:00
|
|
|
namespace {
|
2016-05-03 19:31:07 +00:00
|
|
|
struct screen_layout_t {
|
|
|
|
// The left prompt that we're going to use.
|
2012-11-08 03:59:20 +00:00
|
|
|
wcstring left_prompt;
|
2016-05-03 19:31:07 +00:00
|
|
|
// How much space to leave for it.
|
2012-11-08 03:59:20 +00:00
|
|
|
size_t left_prompt_space;
|
2016-05-03 19:31:07 +00:00
|
|
|
// The right prompt.
|
2012-11-08 03:59:20 +00:00
|
|
|
wcstring right_prompt;
|
2016-05-03 19:31:07 +00:00
|
|
|
// The autosuggestion.
|
2012-11-08 03:59:20 +00:00
|
|
|
wcstring autosuggestion;
|
|
|
|
};
|
2021-09-28 19:50:27 +00:00
|
|
|
} // namespace
|
2012-11-08 03:59:20 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Given a vector whose indexes are offsets and whose values are the widths of the string if
|
|
|
|
// truncated at that offset, return the offset that fits in the given width. Returns
|
|
|
|
// width_by_offset.size() - 1 if they all fit. The first value in width_by_offset is assumed to be
|
|
|
|
// 0.
|
|
|
|
static size_t truncation_offset_for_width(const std::vector<size_t> &width_by_offset,
|
|
|
|
size_t max_width) {
|
|
|
|
assert(!width_by_offset.empty() && width_by_offset.at(0) == 0);
|
2012-11-08 03:59:20 +00:00
|
|
|
size_t i;
|
2016-05-03 19:31:07 +00:00
|
|
|
for (i = 1; i < width_by_offset.size(); i++) {
|
|
|
|
if (width_by_offset.at(i) > max_width) break;
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
2016-05-03 19:31:07 +00:00
|
|
|
// i is the first index that did not fit; i-1 is therefore the last that did.
|
2012-11-08 03:59:20 +00:00
|
|
|
return i - 1;
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
static screen_layout_t compute_layout(screen_t *s, size_t screen_width,
|
2020-05-31 00:10:44 +00:00
|
|
|
const wcstring &left_untrunc_prompt,
|
|
|
|
const wcstring &right_untrunc_prompt,
|
|
|
|
const wcstring &commandline,
|
2019-05-21 21:34:05 +00:00
|
|
|
const wcstring &autosuggestion_str) {
|
2016-10-09 21:38:26 +00:00
|
|
|
UNUSED(s);
|
2012-11-08 03:59:20 +00:00
|
|
|
screen_layout_t result = {};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
// Truncate both prompts to screen width (#904).
|
|
|
|
wcstring left_prompt;
|
|
|
|
prompt_layout_t left_prompt_layout =
|
|
|
|
layout_cache_t::shared.calc_prompt_layout(left_untrunc_prompt, &left_prompt, screen_width);
|
2019-05-21 21:34:05 +00:00
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
wcstring right_prompt;
|
|
|
|
prompt_layout_t right_prompt_layout = layout_cache_t::shared.calc_prompt_layout(
|
|
|
|
right_untrunc_prompt, &right_prompt, screen_width);
|
2012-12-03 10:25:08 +00:00
|
|
|
|
2012-11-25 09:26:58 +00:00
|
|
|
size_t left_prompt_width = left_prompt_layout.last_line_width;
|
|
|
|
size_t right_prompt_width = right_prompt_layout.last_line_width;
|
|
|
|
|
2020-05-31 00:10:44 +00:00
|
|
|
if (left_prompt_width + right_prompt_width > screen_width) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// Nix right_prompt.
|
2012-11-08 03:59:20 +00:00
|
|
|
right_prompt = L"";
|
|
|
|
right_prompt_width = 0;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Now we should definitely fit.
|
2020-05-31 00:10:44 +00:00
|
|
|
assert(left_prompt_width + right_prompt_width <= screen_width);
|
2012-11-08 03:59:20 +00:00
|
|
|
|
2019-05-10 15:38:56 +00:00
|
|
|
// Get the width of the first line, and if there is more than one line.
|
|
|
|
bool multiline = false;
|
|
|
|
size_t first_line_width = 0;
|
2019-11-19 21:46:47 +00:00
|
|
|
for (auto c : commandline) {
|
2016-05-03 19:31:07 +00:00
|
|
|
if (c == L'\n') {
|
2019-05-10 15:38:56 +00:00
|
|
|
multiline = true;
|
|
|
|
break;
|
2016-05-03 19:31:07 +00:00
|
|
|
} else {
|
2021-09-23 10:57:44 +00:00
|
|
|
first_line_width += fish_wcwidth_visible(c);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2019-05-10 15:38:56 +00:00
|
|
|
const size_t first_command_line_width = first_line_width;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// If we have more than one line, ensure we have no autosuggestion.
|
2020-05-31 00:10:44 +00:00
|
|
|
const wchar_t *autosuggestion = autosuggestion_str.c_str();
|
2016-05-05 01:14:04 +00:00
|
|
|
size_t autosuggest_total_width = 0;
|
2019-05-10 15:38:56 +00:00
|
|
|
std::vector<size_t> autosuggest_truncated_widths;
|
|
|
|
if (multiline) {
|
|
|
|
autosuggestion = L"";
|
|
|
|
} else {
|
2019-05-21 21:34:05 +00:00
|
|
|
autosuggest_truncated_widths.reserve(1 + autosuggestion_str.size());
|
2019-05-10 15:38:56 +00:00
|
|
|
for (size_t i = 0; autosuggestion[i] != L'\0'; i++) {
|
|
|
|
autosuggest_truncated_widths.push_back(autosuggest_total_width);
|
2021-09-23 10:57:44 +00:00
|
|
|
autosuggest_total_width += fish_wcwidth_visible(autosuggestion[i]);
|
2019-05-10 15:38:56 +00:00
|
|
|
}
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Here are the layouts we try in turn:
|
|
|
|
//
|
|
|
|
// 1. Left prompt visible, right prompt visible, command line visible, autosuggestion visible.
|
|
|
|
//
|
|
|
|
// 2. Left prompt visible, right prompt visible, command line visible, autosuggestion truncated
|
|
|
|
// (possibly to zero).
|
|
|
|
//
|
2020-05-24 19:54:00 +00:00
|
|
|
// 3. Left prompt visible, right prompt hidden, command line visible, autosuggestion visible
|
2016-05-03 19:31:07 +00:00
|
|
|
//
|
2020-05-24 19:54:00 +00:00
|
|
|
// 4. Left prompt visible, right prompt hidden, command line visible, autosuggestion truncated
|
|
|
|
//
|
|
|
|
// 5. Newline separator (left prompt visible, right prompt hidden, command line visible,
|
2016-05-03 19:31:07 +00:00
|
|
|
// autosuggestion visible).
|
|
|
|
//
|
|
|
|
// A remark about layout #4: if we've pushed the command line to a new line, why can't we draw
|
|
|
|
// the right prompt? The issue is resizing: if you resize the window smaller, then the right
|
|
|
|
// prompt will wrap to the next line. This means that we can't go back to the line that we were
|
|
|
|
// on, and things turn to chaos very quickly.
|
2017-05-02 04:44:30 +00:00
|
|
|
size_t calculated_width;
|
2012-11-08 03:59:20 +00:00
|
|
|
bool done = false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Case 1
|
2017-05-02 04:44:30 +00:00
|
|
|
if (!done) {
|
|
|
|
calculated_width = left_prompt_width + right_prompt_width + first_command_line_width +
|
|
|
|
autosuggest_total_width;
|
2020-05-31 00:10:44 +00:00
|
|
|
if (calculated_width <= screen_width) {
|
2019-05-21 21:34:05 +00:00
|
|
|
result.left_prompt = left_prompt;
|
2017-05-02 04:44:30 +00:00
|
|
|
result.left_prompt_space = left_prompt_width;
|
2019-05-21 21:34:05 +00:00
|
|
|
result.right_prompt = right_prompt;
|
|
|
|
result.autosuggestion = autosuggestion;
|
2017-05-02 04:44:30 +00:00
|
|
|
done = true;
|
|
|
|
}
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Case 2. Note that we require strict inequality so that there's always at least one space
|
|
|
|
// between the left edge and the rprompt.
|
2017-05-02 04:44:30 +00:00
|
|
|
if (!done) {
|
|
|
|
calculated_width = left_prompt_width + right_prompt_width + first_command_line_width;
|
2020-05-31 00:10:44 +00:00
|
|
|
if (calculated_width <= screen_width) {
|
2019-05-21 21:34:05 +00:00
|
|
|
result.left_prompt = left_prompt;
|
2017-05-02 04:44:30 +00:00
|
|
|
result.left_prompt_space = left_prompt_width;
|
2019-05-21 21:34:05 +00:00
|
|
|
result.right_prompt = right_prompt;
|
2017-05-02 04:44:30 +00:00
|
|
|
|
|
|
|
// Need at least two characters to show an autosuggestion.
|
|
|
|
size_t available_autosuggest_space =
|
|
|
|
screen_width - (left_prompt_width + right_prompt_width + first_command_line_width);
|
|
|
|
if (autosuggest_total_width > 0 && available_autosuggest_space > 2) {
|
|
|
|
size_t truncation_offset = truncation_offset_for_width(
|
|
|
|
autosuggest_truncated_widths, available_autosuggest_space - 2);
|
|
|
|
result.autosuggestion = wcstring(autosuggestion, truncation_offset);
|
2019-04-28 22:00:36 +00:00
|
|
|
result.autosuggestion.push_back(get_ellipsis_char());
|
2017-05-02 04:44:30 +00:00
|
|
|
}
|
|
|
|
done = true;
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Case 3
|
2017-05-02 04:44:30 +00:00
|
|
|
if (!done) {
|
2020-05-24 19:54:00 +00:00
|
|
|
calculated_width = left_prompt_width + first_command_line_width + autosuggest_total_width;
|
2020-05-31 00:10:44 +00:00
|
|
|
if (calculated_width <= screen_width) {
|
2019-05-21 21:34:05 +00:00
|
|
|
result.left_prompt = left_prompt;
|
2017-05-02 04:44:30 +00:00
|
|
|
result.left_prompt_space = left_prompt_width;
|
2020-05-24 19:54:00 +00:00
|
|
|
result.autosuggestion = autosuggestion;
|
2017-05-02 04:44:30 +00:00
|
|
|
done = true;
|
|
|
|
}
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Case 4
|
2020-05-24 19:54:00 +00:00
|
|
|
if (!done) {
|
|
|
|
calculated_width = left_prompt_width + first_command_line_width;
|
2020-05-31 00:10:44 +00:00
|
|
|
if (calculated_width <= screen_width) {
|
2020-05-24 19:54:00 +00:00
|
|
|
result.left_prompt = left_prompt;
|
|
|
|
result.left_prompt_space = left_prompt_width;
|
|
|
|
|
|
|
|
// Need at least two characters to show an autosuggestion.
|
|
|
|
size_t available_autosuggest_space =
|
|
|
|
screen_width - (left_prompt_width + first_command_line_width);
|
|
|
|
if (autosuggest_total_width > 0 && available_autosuggest_space > 2) {
|
|
|
|
size_t truncation_offset = truncation_offset_for_width(
|
|
|
|
autosuggest_truncated_widths, available_autosuggest_space - 2);
|
|
|
|
result.autosuggestion = wcstring(autosuggestion, truncation_offset);
|
|
|
|
result.autosuggestion.push_back(get_ellipsis_char());
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 5
|
2016-05-03 19:31:07 +00:00
|
|
|
if (!done) {
|
2019-05-21 21:34:05 +00:00
|
|
|
result.left_prompt = left_prompt;
|
2012-11-08 03:59:20 +00:00
|
|
|
result.left_prompt_space = left_prompt_width;
|
2020-09-12 18:09:26 +00:00
|
|
|
result.autosuggestion = autosuggestion;
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-08 03:59:20 +00:00
|
|
|
return result;
|
|
|
|
}
|
2007-08-01 19:07:54 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::write(const wcstring &left_prompt, const wcstring &right_prompt,
|
|
|
|
const wcstring &commandline, size_t explicit_len,
|
|
|
|
const std::vector<highlight_spec_t> &colors, const std::vector<int> &indent,
|
2021-09-23 03:46:36 +00:00
|
|
|
size_t cursor_pos, const environment_t &vars, pager_t &pager,
|
|
|
|
page_rendering_t &page_rendering, bool cursor_is_within_pager) {
|
2020-07-05 06:38:03 +00:00
|
|
|
termsize_t curr_termsize = termsize_last();
|
|
|
|
int screen_width = curr_termsize.width;
|
2020-03-03 02:22:18 +00:00
|
|
|
static relaxed_atomic_t<uint32_t> s_repaints{0};
|
2020-04-08 23:56:59 +00:00
|
|
|
FLOGF(screen, "Repaint %u", static_cast<unsigned>(++s_repaints));
|
2012-11-19 00:30:30 +00:00
|
|
|
screen_data_t::cursor_t cursor_arr;
|
2012-11-08 03:59:20 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Turn the command line into the explicit portion and the autosuggestion.
|
2012-11-08 03:59:20 +00:00
|
|
|
const wcstring explicit_command_line = commandline.substr(0, explicit_len);
|
|
|
|
const wcstring autosuggestion = commandline.substr(explicit_len);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// If we are using a dumb terminal, don't try any fancy stuff, just print out the text.
|
|
|
|
// right_prompt not supported.
|
|
|
|
if (is_dumb()) {
|
2012-11-19 00:30:30 +00:00
|
|
|
const std::string prompt_narrow = wcs2string(left_prompt);
|
|
|
|
const std::string command_line_narrow = wcs2string(explicit_command_line);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-12-01 23:44:09 +00:00
|
|
|
write_loop(STDOUT_FILENO, "\r", 1);
|
|
|
|
write_loop(STDOUT_FILENO, prompt_narrow.c_str(), prompt_narrow.size());
|
|
|
|
write_loop(STDOUT_FILENO, command_line_narrow.c_str(), command_line_narrow.size());
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
this->check_status();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Completely ignore impossibly small screens.
|
|
|
|
if (screen_width < 4) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Compute a layout.
|
2021-09-21 19:51:04 +00:00
|
|
|
const screen_layout_t layout = compute_layout(this, screen_width, left_prompt, right_prompt,
|
2019-05-13 01:23:00 +00:00
|
|
|
explicit_command_line, autosuggestion);
|
2012-11-08 03:59:20 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Determine whether, if we have an autosuggestion, it was truncated.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->autosuggestion_is_truncated =
|
2016-05-03 19:31:07 +00:00
|
|
|
!autosuggestion.empty() && autosuggestion != layout.autosuggestion;
|
2013-08-21 03:08:56 +00:00
|
|
|
|
2020-06-07 21:53:42 +00:00
|
|
|
// Clear the desired screen and set its width.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.screen_width = screen_width;
|
|
|
|
this->desired.resize(0);
|
|
|
|
this->desired.cursor.x = this->desired.cursor.y = 0;
|
2012-11-08 03:59:20 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Append spaces for the left prompt.
|
|
|
|
for (size_t i = 0; i < layout.left_prompt_space; i++) {
|
2021-09-21 19:51:04 +00:00
|
|
|
desired_append_char(L' ', highlight_spec_t{}, 0, layout.left_prompt_space, 1);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// If overflowing, give the prompt its own line to improve the situation.
|
2012-11-08 03:59:20 +00:00
|
|
|
size_t first_line_prompt_space = layout.left_prompt_space;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Reconstruct the command line.
|
2012-11-08 03:59:20 +00:00
|
|
|
wcstring effective_commandline = explicit_command_line + layout.autosuggestion;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Output the command line.
|
2012-11-08 03:59:20 +00:00
|
|
|
size_t i;
|
2016-05-03 19:31:07 +00:00
|
|
|
for (i = 0; i < effective_commandline.size(); i++) {
|
|
|
|
// Grab the current cursor's x,y position if this character matches the cursor's offset.
|
2016-05-05 01:14:04 +00:00
|
|
|
if (!cursor_is_within_pager && i == cursor_pos) {
|
2021-09-21 19:51:04 +00:00
|
|
|
cursor_arr = this->desired.cursor;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2021-09-21 19:51:04 +00:00
|
|
|
desired_append_char(effective_commandline.at(i), colors[i], indent[i],
|
|
|
|
first_line_prompt_space,
|
|
|
|
fish_wcwidth_visible(effective_commandline.at(i)));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 19:31:07 +00:00
|
|
|
|
|
|
|
// Cursor may have been at the end too.
|
2016-05-05 01:14:04 +00:00
|
|
|
if (!cursor_is_within_pager && i == cursor_pos) {
|
2021-09-21 19:51:04 +00:00
|
|
|
cursor_arr = this->desired.cursor;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 19:31:07 +00:00
|
|
|
|
2021-12-02 03:40:55 +00:00
|
|
|
int full_line_count = this->desired.cursor.y + 1;
|
|
|
|
|
2016-05-03 19:31:07 +00:00
|
|
|
// Now that we've output everything, set the cursor to the position that we saved in the loop
|
|
|
|
// above.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.cursor = cursor_arr;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-05 01:14:04 +00:00
|
|
|
if (cursor_is_within_pager) {
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.cursor.x = static_cast<int>(cursor_pos);
|
|
|
|
this->desired.cursor.y = static_cast<int>(this->desired.line_count());
|
2014-01-27 08:56:13 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2020-07-05 06:38:03 +00:00
|
|
|
// Re-render our completions page if necessary. Limit the term size of the pager to the true
|
|
|
|
// term size, minus the number of lines consumed by our string.
|
2023-03-19 22:50:33 +00:00
|
|
|
pager.set_term_size(
|
|
|
|
termsize_t{std::max((rust::isize)1, curr_termsize.width),
|
|
|
|
std::max((rust::isize)1, curr_termsize.height - full_line_count)});
|
2020-07-05 06:38:03 +00:00
|
|
|
pager.update_rendering(&page_rendering);
|
2016-05-03 19:31:07 +00:00
|
|
|
// Append pager_data (none if empty).
|
2021-09-21 19:51:04 +00:00
|
|
|
this->desired.append_lines(page_rendering.screen_data);
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2021-09-23 03:46:36 +00:00
|
|
|
this->update(layout.left_prompt, layout.right_prompt, vars);
|
2021-09-21 19:51:04 +00:00
|
|
|
this->save_status();
|
2012-11-08 03:59:20 +00:00
|
|
|
}
|
2020-06-07 21:53:42 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::reset_line(bool repaint_prompt) {
|
2020-06-07 22:16:27 +00:00
|
|
|
// Remember how many lines we had output to, so we can clear the remaining lines in the next
|
|
|
|
// call to s_update. This prevents leaving junk underneath the cursor when resizing a window
|
|
|
|
// wider such that it reduces our desired line count.
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual_lines_before_reset =
|
|
|
|
std::max(this->actual_lines_before_reset, this->actual.line_count());
|
2012-11-25 00:58:30 +00:00
|
|
|
|
2020-06-07 22:16:27 +00:00
|
|
|
if (repaint_prompt) {
|
2016-05-03 19:31:07 +00:00
|
|
|
// If the prompt is multi-line, we need to move up to the prompt's initial line. We do this
|
|
|
|
// by lying to ourselves and claiming that we're really below what we consider "line 0"
|
|
|
|
// (which is the last line of the prompt). This will cause us to move up to try to get back
|
|
|
|
// to line 0, but really we're getting back to the initial line of the prompt.
|
2021-09-21 19:51:04 +00:00
|
|
|
const size_t prompt_line_count = calc_prompt_lines(this->actual_left_prompt);
|
2012-11-25 05:06:42 +00:00
|
|
|
assert(prompt_line_count >= 1);
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.y += (prompt_line_count - 1);
|
|
|
|
this->actual_left_prompt.clear();
|
2012-11-25 05:06:42 +00:00
|
|
|
}
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.resize(0);
|
|
|
|
this->need_clear_lines = true;
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2020-06-07 22:16:27 +00:00
|
|
|
// This should prevent resetting the cursor position during the next repaint.
|
|
|
|
write_loop(STDOUT_FILENO, "\r", 1);
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.x = 0;
|
2016-10-03 19:41:53 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
fstat(STDOUT_FILENO, &this->prev_buff_1);
|
|
|
|
fstat(STDERR_FILENO, &this->prev_buff_2);
|
2020-06-07 22:16:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
void screen_t::reset_abandoning_line(int screen_width) {
|
|
|
|
this->actual.cursor.y = 0;
|
|
|
|
this->actual.resize(0);
|
|
|
|
this->actual_left_prompt.clear();
|
|
|
|
this->need_clear_lines = true;
|
2020-06-07 22:16:27 +00:00
|
|
|
|
|
|
|
// Do the PROMPT_SP hack.
|
|
|
|
wcstring abandon_line_string;
|
|
|
|
abandon_line_string.reserve(screen_width + 32);
|
|
|
|
|
|
|
|
// Don't need to check for fish_wcwidth errors; this is done when setting up
|
|
|
|
// omitted_newline_char in common.cpp.
|
|
|
|
int non_space_width = get_omitted_newline_width();
|
|
|
|
// We do `>` rather than `>=` because the code below might require one extra space.
|
|
|
|
if (screen_width > non_space_width) {
|
|
|
|
bool justgrey = true;
|
|
|
|
if (cur_term && enter_dim_mode) {
|
2022-03-14 14:36:17 +00:00
|
|
|
std::string dim = fish_tparm(const_cast<char *>(enter_dim_mode));
|
2020-06-07 22:16:27 +00:00
|
|
|
if (!dim.empty()) {
|
|
|
|
// Use dim if they have it, so the color will be based on their actual normal
|
2022-10-29 03:07:36 +00:00
|
|
|
// color and the background of the terminal.
|
2020-06-07 22:16:27 +00:00
|
|
|
abandon_line_string.append(str2wcstring(dim));
|
|
|
|
justgrey = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cur_term && justgrey && set_a_foreground) {
|
|
|
|
if (max_colors >= 238) {
|
|
|
|
// draw the string in a particular grey
|
|
|
|
abandon_line_string.append(
|
2022-03-14 14:36:17 +00:00
|
|
|
str2wcstring(fish_tparm(const_cast<char *>(set_a_foreground), 237)));
|
2020-06-07 22:16:27 +00:00
|
|
|
} else if (max_colors >= 9) {
|
|
|
|
// bright black (the ninth color, looks grey)
|
|
|
|
abandon_line_string.append(
|
2022-03-14 14:36:17 +00:00
|
|
|
str2wcstring(fish_tparm(const_cast<char *>(set_a_foreground), 8)));
|
2020-06-07 22:16:27 +00:00
|
|
|
} else if (max_colors >= 2 && enter_bold_mode) {
|
|
|
|
// we might still get that color by setting black and going bold for bright
|
|
|
|
abandon_line_string.append(
|
2022-03-14 14:36:17 +00:00
|
|
|
str2wcstring(fish_tparm(const_cast<char *>(enter_bold_mode))));
|
2020-06-07 22:16:27 +00:00
|
|
|
abandon_line_string.append(
|
2022-03-14 14:36:17 +00:00
|
|
|
str2wcstring(fish_tparm(const_cast<char *>(set_a_foreground), 0)));
|
2020-06-07 22:16:27 +00:00
|
|
|
}
|
2012-12-01 23:44:09 +00:00
|
|
|
}
|
2017-02-16 04:09:26 +00:00
|
|
|
|
2019-04-28 22:00:36 +00:00
|
|
|
abandon_line_string.append(get_omitted_newline_str());
|
2020-06-07 22:16:27 +00:00
|
|
|
|
|
|
|
if (cur_term && exit_attribute_mode) {
|
2022-03-14 14:36:17 +00:00
|
|
|
abandon_line_string.append(str2wcstring(fish_tparm(
|
2020-06-07 22:16:27 +00:00
|
|
|
const_cast<char *>(exit_attribute_mode)))); // normal text ANSI escape sequence
|
2018-05-08 10:39:58 +00:00
|
|
|
}
|
2013-01-31 23:57:08 +00:00
|
|
|
|
2023-05-16 19:35:14 +00:00
|
|
|
int newline_glitch_width = TERM_HAS_XN ? 0 : 1;
|
2020-06-07 22:16:27 +00:00
|
|
|
abandon_line_string.append(screen_width - non_space_width - newline_glitch_width, L' ');
|
2012-12-01 23:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-07 22:16:27 +00:00
|
|
|
abandon_line_string.push_back(L'\r');
|
|
|
|
abandon_line_string.append(get_omitted_newline_str());
|
|
|
|
// Now we are certainly on a new line. But we may have dropped the omitted newline char on
|
|
|
|
// it. So append enough spaces to overwrite the omitted newline char, and then clear all the
|
|
|
|
// spaces from the new line.
|
|
|
|
abandon_line_string.append(non_space_width, L' ');
|
|
|
|
abandon_line_string.push_back(L'\r');
|
|
|
|
// Clear entire line. Zsh doesn't do this. Fish added this with commit 4417a6ee: If you have
|
|
|
|
// a prompt preceded by a new line, you'll get a line full of spaces instead of an empty
|
|
|
|
// line above your prompt. This doesn't make a difference in normal usage, but copying and
|
|
|
|
// pasting your terminal log becomes a pain. This commit clears that line, making it an
|
|
|
|
// actual empty line.
|
|
|
|
if (!is_dumb() && clr_eol) {
|
|
|
|
abandon_line_string.append(str2wcstring(clr_eol));
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-25 00:58:30 +00:00
|
|
|
|
2020-06-07 22:16:27 +00:00
|
|
|
const std::string narrow_abandon_line_string = wcs2string(abandon_line_string);
|
|
|
|
write_loop(STDOUT_FILENO, narrow_abandon_line_string.c_str(),
|
|
|
|
narrow_abandon_line_string.size());
|
2021-09-21 19:51:04 +00:00
|
|
|
this->actual.cursor.x = 0;
|
2020-06-07 22:16:27 +00:00
|
|
|
|
2021-09-21 19:51:04 +00:00
|
|
|
fstat(STDOUT_FILENO, &this->prev_buff_1);
|
|
|
|
fstat(STDERR_FILENO, &this->prev_buff_2);
|
2006-10-01 16:02:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 20:32:08 +00:00
|
|
|
void screen_force_clear_to_end() {
|
2016-05-03 19:31:07 +00:00
|
|
|
if (clr_eos) {
|
2023-05-28 23:13:54 +00:00
|
|
|
writembs(stdoutput(), clr_eos);
|
2014-01-16 02:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 23:13:54 +00:00
|
|
|
screen_t::screen_t() : outp_(stdoutput()) {}
|
2020-04-28 18:49:26 +00:00
|
|
|
|
|
|
|
bool screen_t::cursor_is_wrapped_to_own_line() const {
|
|
|
|
// Note == comparison against the line count is correct: we do not create a line just for the
|
|
|
|
// cursor. If there is a line containing the cursor, then it means that line has contents and we
|
|
|
|
// should return false.
|
2020-04-28 23:30:52 +00:00
|
|
|
// Don't consider dumb terminals to have wrapping for the purposes of this function.
|
|
|
|
return actual.cursor.x == 0 && static_cast<size_t>(actual.cursor.y) == actual.line_count() &&
|
|
|
|
!is_dumb();
|
2020-04-28 18:49:26 +00:00
|
|
|
}
|