Skip to content

Commit

Permalink
Optimise multi_progress_bar tty control sequences
Browse files Browse the repository at this point in the history
Instead of sending multiple cursor_up and clear_line sequences,
send a single command to move the cursor up multiple lines.

This makes the tty::clear_line redundant, because the next update
will overwrite the previous lines anyway.
  • Loading branch information
Giedriusj1 committed Nov 7, 2024
1 parent 6ad9ec4 commit 830668e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 9 additions & 0 deletions include/libdnf5-cli/tty.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ LIBDNF_CLI_API std::ostream & white(std::ostream & stream);
LIBDNF_CLI_API std::ostream & clear_line(std::ostream & stream);
LIBDNF_CLI_API std::ostream & cursor_up(std::ostream & stream);

class cursor_up_multiple_lines {
public:
explicit cursor_up_multiple_lines(std::size_t);
LIBDNF_CLI_API friend std::ostream & operator<<(std::ostream & os, const cursor_up_multiple_lines & cursorUp);

private:
std::size_t line_count;
};

LIBDNF_CLI_API std::ostream & cursor_hide(std::ostream & stream);
LIBDNF_CLI_API std::ostream & cursor_show(std::ostream & stream);

Expand Down
6 changes: 3 additions & 3 deletions libdnf5-cli/progressbar/multi_progress_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ std::ostream & operator<<(std::ostream & stream, MultiProgressBar & mbar) {
text_buffer.clear();

if (is_interactive && mbar.num_of_lines_to_clear > 0) {
text_buffer << tty::clear_line;
for (std::size_t i = 1; i < mbar.num_of_lines_to_clear; i++) {
text_buffer << tty::cursor_up << tty::clear_line;
if (mbar.num_of_lines_to_clear > 1) {
text_buffer << tty::cursor_up_multiple_lines(mbar.num_of_lines_to_clear - 1);
}

text_buffer << "\r";
} else if (mbar.line_printed) {
text_buffer << std::endl;
Expand Down
10 changes: 10 additions & 0 deletions libdnf5-cli/tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ bool is_interactive() {
}


// tty::cursor_up_multiple_lines(std::size_t line_count)
cursor_up_multiple_lines::cursor_up_multiple_lines(std::size_t line_count) : line_count(line_count) {}

LIBDNF_CLI_API std::ostream & operator<<(std::ostream & os, const cursor_up_multiple_lines & cursor) {
if (is_interactive()) {
os << "\033[" << cursor.line_count << "A";
}
return os;
}

// tty::reset
TTY_COMMAND(reset, "\033[0m")

Expand Down

0 comments on commit 830668e

Please sign in to comment.