Skip to content

Commit 076fcd4

Browse files
committed
Improved clearing of previously rendered lines
1 parent 9627bf1 commit 076fcd4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Detect "no crontab for X"
66
- Display a message when the file is empty
7+
- Improved clearing of previously rendered lines
78

89
## v0.1.0
910

lib/render.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ std::string DIM = "\033[2m";
2121

2222
std::string CLEAR_LINE = "\033[2K";
2323
std::string CURSOR_UP = "\033[1A";
24+
std::string CURSOR_LEFT = "\033[G";
2425

2526
std::string pad_right(std::string value, int amount) {
2627
for (int i = value.length() + 1; i <= amount; i++) {
@@ -321,8 +322,11 @@ std::string render_diff(std::string current, std::string previous) {
321322
}
322323
std::stringstream output;
323324
for(unsigned i = 0; i < lines; ++i) {
324-
output << CLEAR_LINE << CURSOR_UP;
325+
output << CLEAR_LINE;
326+
output << CURSOR_UP;
325327
}
328+
output << CLEAR_LINE;
329+
output << CURSOR_LEFT;
326330
output << current;
327331
return output.str();
328332
}
@@ -370,6 +374,9 @@ std::string replace_ansi(std::string value, bool with_string) {
370374
} else if (value.substr(i, CURSOR_UP.length()) == CURSOR_UP) {
371375
final += with_string ? "{CURSOR_UP}" : "";
372376
i += CURSOR_UP.length() - 1;
377+
} else if (value.substr(i, CURSOR_LEFT.length()) == CURSOR_LEFT) {
378+
final += with_string ? "{CURSOR_LEFT}" : "";
379+
i += CURSOR_LEFT.length() - 1;
373380
} else {
374381
final += "";
375382
}

lib/render_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,25 +541,25 @@ void test_render_diff() {
541541
previous = "one\ntwo2";
542542
rendered = replace_ansi(render_diff(current, previous), true);
543543
debug_print_ui(rendered);
544-
assert(rendered == "{CLEAR_LINE}{CURSOR_UP}one\ntwo1");
544+
assert(rendered == "{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_LEFT}one\ntwo1");
545545

546546
current = "one\ntwo\nthree1";
547547
previous = "one\ntwo\nthree2";
548548
rendered = replace_ansi(render_diff(current, previous), true);
549549
debug_print_ui(rendered);
550-
assert(rendered == "{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_UP}one\ntwo\nthree1");
550+
assert(rendered == "{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_LEFT}one\ntwo\nthree1");
551551

552552
current = "";
553553
previous = "one\ntwo\nthree2";
554554
rendered = replace_ansi(render_diff(current, previous), true);
555555
debug_print_ui(rendered);
556-
assert(rendered == "{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_UP}");
556+
assert(rendered == "{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_UP}{CLEAR_LINE}{CURSOR_LEFT}");
557557

558558
current = "one\ntwo\nthree1";
559559
previous = "";
560560
rendered = replace_ansi(render_diff(current, previous), true);
561561
debug_print_ui(rendered);
562-
assert(rendered == "one\ntwo\nthree1");
562+
assert(rendered == "{CLEAR_LINE}{CURSOR_LEFT}one\ntwo\nthree1");
563563
}
564564
}
565565

0 commit comments

Comments
 (0)