Clear to end of each line in left prompt

This commit is contained in:
Soumya 2020-10-25 16:22:28 -07:00 committed by ridiculousfish
parent 2a07673561
commit 80aaae5b74
3 changed files with 50 additions and 20 deletions

View File

@ -5740,19 +5740,19 @@ void test_layout_cache() {
for (size_t i = 0; i < layout_cache_t::prompt_cache_max_size; i++) {
wcstring input = std::to_wstring(i);
do_test(!seqs.find_prompt_layout(input));
seqs.add_prompt_layout({input, huge, input, {i, 0, 0}});
do_test(seqs.find_prompt_layout(input)->layout.line_count == i);
seqs.add_prompt_layout({input, huge, input, {{}, i, 0}});
do_test(seqs.find_prompt_layout(input)->layout.max_line_width == i);
}
size_t expected_evictee = 3;
for (size_t i = 0; i < layout_cache_t::prompt_cache_max_size; i++) {
if (i != expected_evictee)
do_test(seqs.find_prompt_layout(std::to_wstring(i))->layout.line_count == i);
do_test(seqs.find_prompt_layout(std::to_wstring(i))->layout.max_line_width == i);
}
seqs.add_prompt_layout({L"whatever", huge, L"whatever", {100, 0, 0}});
seqs.add_prompt_layout({L"whatever", huge, L"whatever", {{}, 100, 0}});
do_test(!seqs.find_prompt_layout(std::to_wstring(expected_evictee)));
do_test(seqs.find_prompt_layout(L"whatever", huge)->layout.line_count == 100);
do_test(seqs.find_prompt_layout(L"whatever", huge)->layout.max_line_width == 100);
}
void test_prompt_truncation() {
@ -5762,7 +5762,16 @@ void test_prompt_truncation() {
/// Helper to return 'layout' formatted as a string for easy comparison.
auto format_layout = [&] {
return format_string(L"%lu,%lu,%lu", (unsigned long)layout.line_count,
wcstring line_breaks = L"";
bool first = true;
for (const size_t line_break : layout.line_breaks) {
if (!first) {
line_breaks.push_back(L',');
}
line_breaks.append(format_string(L"%lu", (unsigned long)line_break));
first = false;
}
return format_string(L"[%ls],%lu,%lu", line_breaks.c_str(),
(unsigned long)layout.max_line_width,
(unsigned long)layout.last_line_width);
};
@ -5774,12 +5783,22 @@ void test_prompt_truncation() {
// No truncation.
layout = cache.calc_prompt_layout(L"abcd", &trunc);
do_test(format_layout() == L"1,4,4");
do_test(format_layout() == L"[],4,4");
do_test(trunc == L"abcd");
// Line break calculation.
layout = cache.calc_prompt_layout(join({
L"0123456789ABCDEF", //
L"012345", //
L"0123456789abcdef", //
L"xyz" //
}),
&trunc, 80);
do_test(format_layout() == L"[16,23,40],16,3");
// Basic truncation.
layout = cache.calc_prompt_layout(L"0123456789ABCDEF", &trunc, 8);
do_test(format_layout() == L"1,8,8");
do_test(format_layout() == L"[],8,8");
do_test(trunc == ellipsis + L"9ABCDEF");
// Multiline truncation.
@ -5790,24 +5809,24 @@ void test_prompt_truncation() {
L"xyz" //
}),
&trunc, 8);
do_test(format_layout() == L"4,8,3");
do_test(format_layout() == L"[8,15,24],8,3");
do_test(trunc == join({ellipsis + L"9ABCDEF", L"012345", ellipsis + L"9abcdef", L"xyz"}));
// Escape sequences are not truncated.
layout =
cache.calc_prompt_layout(L"\x1B]50;CurrentDir=test/foo\x07NOT_PART_OF_SEQUENCE", &trunc, 4);
do_test(format_layout() == L"1,4,4");
do_test(format_layout() == L"[],4,4");
do_test(trunc == ellipsis + L"\x1B]50;CurrentDir=test/foo\x07NCE");
// Newlines in escape sequences are skipped.
layout = cache.calc_prompt_layout(L"\x1B]50;CurrentDir=\ntest/foo\x07NOT_PART_OF_SEQUENCE",
&trunc, 4);
do_test(format_layout() == L"1,4,4");
do_test(format_layout() == L"[],4,4");
do_test(trunc == ellipsis + L"\x1B]50;CurrentDir=\ntest/foo\x07NCE");
// We will truncate down to one character if we have to.
layout = cache.calc_prompt_layout(L"Yay", &trunc, 1);
do_test(format_layout() == L"1,1,1");
do_test(format_layout() == L"[],1,1");
do_test(trunc == ellipsis);
}

View File

@ -368,7 +368,7 @@ prompt_layout_t layout_cache_t::calc_prompt_layout(const wcstring &prompt_str,
size_t prompt_len = prompt_str.size();
const wchar_t *prompt = prompt_str.c_str();
prompt_layout_t layout = {1, 0, 0};
prompt_layout_t layout = {{}, 0, 0};
wcstring trunc_prompt;
size_t run_start = 0;
@ -389,7 +389,9 @@ prompt_layout_t layout_cache_t::calc_prompt_layout(const wcstring &prompt_str,
wchar_t endc = prompt[run_end];
if (endc) {
layout.line_count += (endc == L'\n' || endc == L'\f');
if (endc == L'\n' || endc == L'\f') {
layout.line_breaks.push_back(trunc_prompt.size());
}
trunc_prompt.push_back(endc);
run_start = run_end + 1;
} else {
@ -409,7 +411,7 @@ static size_t calc_prompt_lines(const wcstring &prompt) {
// calc_prompt_width_and_lines.
size_t result = 1;
if (prompt.find_first_of(L"\n\f") != wcstring::npos) {
result = layout_cache_t::shared.calc_prompt_layout(prompt).line_count;
result = layout_cache_t::shared.calc_prompt_layout(prompt).line_breaks.size() + 1;
}
return result;
}
@ -696,7 +698,8 @@ static void s_update(screen_t *scr, const wcstring &left_prompt, const wcstring
const scoped_buffer_t buffering(*scr);
// Determine size of left and right prompt. Note these have already been truncated.
const size_t left_prompt_width = cached_layouts.calc_prompt_layout(left_prompt).last_line_width;
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;
const size_t right_prompt_width =
cached_layouts.calc_prompt_layout(right_prompt).last_line_width;
@ -734,7 +737,15 @@ static void s_update(screen_t *scr, const wcstring &left_prompt, const wcstring
// Output the left prompt if it has changed.
if (left_prompt != scr->actual_left_prompt) {
s_move(scr, 0, 0);
s_write_str(scr, left_prompt.c_str());
size_t start = 0;
for (const size_t line_break : left_prompt_layout.line_breaks) {
s_write_str(scr, left_prompt.substr(start, line_break - start).c_str());
if (clr_eol) {
s_write_mbs(scr, clr_eol);
}
start = line_break;
}
s_write_str(scr, left_prompt.c_str() + start);
scr->actual_left_prompt = left_prompt;
scr->actual.cursor.x = static_cast<int>(left_prompt_width);
}

View File

@ -213,9 +213,9 @@ void screen_force_clear_to_end();
// Information about the layout of a prompt.
struct prompt_layout_t {
size_t line_count; // how many lines the prompt consumes
size_t max_line_width; // width of the longest line
size_t last_line_width; // width of the last line
std::vector<size_t> line_breaks; // line breaks when rendering the prompt
size_t max_line_width; // width of the longest line
size_t last_line_width; // width of the last line
};
// Maintain a mapping of escape sequences to their widths for fast lookup.