refactor escape_code_length()

Step one in addressing issue #3793
This commit is contained in:
Kurtis Rader 2017-02-03 14:41:29 -08:00
parent 279ec88351
commit 6d72d538a6

View file

@ -198,70 +198,75 @@ static bool is_csi_style_escape_seq(const wchar_t *code, size_t *resulting_lengt
return true; return true;
} }
/// Returns the number of characters in the escape code starting at 'code' (which should initially // Detect whether the escape sequence sets foreground/background color.
/// contain \e). static bool is_color_escape_seq(const wchar_t *code, size_t *resulting_length) {
size_t escape_code_length(const wchar_t *code) { if (!cur_term) return false;
assert(code != NULL);
// The only escape codes we recognize start with \e. // Detect these terminfo color escapes with parameter value up to max_colors, all of which
if (code[0] != L'\e') return 0; // don't move the cursor.
char *const esc[] = {
set_a_foreground, set_a_background, set_foreground, set_background,
};
size_t resulting_length = 0; for (size_t p = 0; p < sizeof esc / sizeof *esc; p++) {
bool found = false; if (!esc[p]) continue;
if (cur_term != NULL) { for (short k = 0; k < max_colors; k++) {
// Detect these terminfo color escapes with parameter value up to max_colors-1, all of which size_t esc_seq_len = try_sequence(tparm(esc[p], k), code);
// don't move if (esc_seq_len) {
// the cursor. *resulting_length = esc_seq_len;
char *const esc[] = { return true;
set_a_foreground, set_a_background, set_foreground, set_background,
};
for (size_t p = 0; p < sizeof esc / sizeof *esc && !found; p++) {
if (!esc[p]) continue;
for (short k = 0; k < max_colors; k++) {
size_t len = try_sequence(tparm(esc[p], k), code);
if (len) {
resulting_length = len;
found = true;
break;
}
}
}
// Detect these semi-common terminfo escapes without any parameter values, all of which
// don't move the cursor.
char *const esc2[] = {enter_bold_mode, exit_attribute_mode, enter_underline_mode,
exit_underline_mode, enter_standout_mode, exit_standout_mode,
flash_screen, enter_subscript_mode, exit_subscript_mode,
enter_superscript_mode, exit_superscript_mode, enter_blink_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_protected_mode, enter_alt_charset_mode,
exit_alt_charset_mode};
for (size_t p = 0; p < sizeof esc2 / sizeof *esc2 && !found; p++) {
if (!esc2[p]) continue;
// Test both padded and unpadded version, just to be safe. Most versions of tparm don't
// actually seem to do anything these days.
size_t len = maxi(try_sequence(tparm(esc2[p]), code), try_sequence(esc2[p], code));
if (len) {
resulting_length = len;
found = true;
} }
} }
} }
if (!found) found = is_screen_name_escape_seq(code, &resulting_length); return false;
if (!found) found = is_iterm2_escape_seq(code, &resulting_length); }
if (!found) found = is_single_byte_escape_seq(code, &resulting_length);
if (!found) found = is_csi_style_escape_seq(code, &resulting_length);
if (!found) is_two_byte_escape_seq(code, &resulting_length);
return resulting_length; // Detect whether the escape sequence sets one of the terminal attributes that affects how text is
// displayed other than the color.
static bool is_visual_escape_seq(const wchar_t *code, size_t *resulting_length) {
if (!cur_term) return false;
char *const esc2[] = {enter_bold_mode, exit_attribute_mode, enter_underline_mode,
exit_underline_mode, enter_standout_mode, exit_standout_mode,
flash_screen, enter_subscript_mode, exit_subscript_mode,
enter_superscript_mode, exit_superscript_mode, enter_blink_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_protected_mode, enter_alt_charset_mode,
exit_alt_charset_mode};
for (size_t p = 0; p < sizeof esc2 / sizeof *esc2; p++) {
if (!esc2[p]) continue;
// Test both padded and unpadded version, just to be safe. Most versions of tparm don't
// actually seem to do anything these days.
size_t esc_seq_len = maxi(try_sequence(tparm(esc2[p]), code), try_sequence(esc2[p], code));
if (esc_seq_len) {
*resulting_length = esc_seq_len;
return true;
}
}
return false;
}
/// Returns the number of characters in the escape code starting at 'code'. We only handle sequences
/// that begin with \e. If it doesn't we return zero.
size_t escape_code_length(const wchar_t *code) {
assert(code != NULL);
if (*code != L'\e') return 0;
size_t esc_seq_len;
if (is_color_escape_seq(code, &esc_seq_len)) return esc_seq_len;
if (is_visual_escape_seq(code, &esc_seq_len)) return esc_seq_len;
if (is_screen_name_escape_seq(code, &esc_seq_len)) return esc_seq_len;
if (is_iterm2_escape_seq(code, &esc_seq_len)) return esc_seq_len;
if (is_single_byte_escape_seq(code, &esc_seq_len)) return esc_seq_len;
if (is_csi_style_escape_seq(code, &esc_seq_len)) return esc_seq_len;
if (is_two_byte_escape_seq(code, &esc_seq_len)) return esc_seq_len;
return 0;
} }
// Information about a prompt layout. // Information about a prompt layout.
@ -307,6 +312,7 @@ static prompt_layout_t calc_prompt_layout(const wchar_t *prompt) {
prompt_layout.max_line_width = maxi(prompt_layout.max_line_width, current_line_width); prompt_layout.max_line_width = maxi(prompt_layout.max_line_width, current_line_width);
} }
} }
prompt_layout.last_line_width = current_line_width; prompt_layout.last_line_width = current_line_width;
return prompt_layout; return prompt_layout;
} }