Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/libdnf5-cli/tty.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ LIBDNF_CLI_API std::ostream & cursor_down(std::ostream & stream);
LIBDNF_CLI_API std::ostream & cursor_hide(std::ostream & stream);
LIBDNF_CLI_API std::ostream & cursor_show(std::ostream & stream);

/// Hide the cursor, keeping track of whether the cursor is hidden
LIBDNF_CLI_API void hide_cursor();
/// Show the cursor, keeping track of whether the cursor is hidden
LIBDNF_CLI_API void show_cursor();
/// Return the cursor visibility state tracked by hide_cursor() and show_cursor().
LIBDNF_CLI_API bool is_cursor_hidden();


/// State of the terminal taskbar/progress indicator, following the ConEmu
/// `OSC 9;4` de facto protocol (also supported by Konsole, Windows Terminal,
Expand Down
4 changes: 2 additions & 2 deletions libdnf5-cli/progressbar/multi_progress_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ MultiProgressBar::Impl::Impl(TrackingMode tracking_mode) : tracking_mode(trackin

MultiProgressBar::MultiProgressBar(TrackingMode tracking_mode) : p_impl(new Impl(tracking_mode)) {
if (tty::is_interactive()) {
std::cerr << tty::cursor_hide;
tty::hide_cursor();
}
}

Expand All @@ -102,7 +102,7 @@ MultiProgressBar::~MultiProgressBar() {
if (tty::is_interactive()) {
// Clear any taskbar/tab progress indicator so it does not linger.
tty::set_taskbar_progress(std::cerr, tty::TaskbarProgressState::CLEAR);
std::cerr << tty::cursor_show;
tty::show_cursor();
}
}

Expand Down
23 changes: 23 additions & 0 deletions libdnf5-cli/tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ std::ostream & set_taskbar_progress(std::ostream & stream, TaskbarProgressState
}

static ColoringEnabled coloring = ColoringEnabled::AUTO;
static bool cursor_hidden = false;

void coloring_enable(ColoringEnabled value) {
coloring = value;
Expand Down Expand Up @@ -157,5 +158,27 @@ TTY_COMMAND(cursor_hide, "\x1b[?25l")
// tty::cursor_show
TTY_COMMAND(cursor_show, "\x1b[?25h")

void hide_cursor() {
if (is_interactive()) {
std::cerr << cursor_hide;
if (std::cerr) {
cursor_hidden = true;
}
}
}

void show_cursor() {
if (is_interactive()) {
std::cerr << cursor_show;
if (std::cerr) {
cursor_hidden = false;
}
}
}

bool is_cursor_hidden() {
return cursor_hidden;
}


} // namespace libdnf5::cli::tty
11 changes: 11 additions & 0 deletions libdnf5-cli/utils/userconfirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

#include "libdnf5-cli/utils/userconfirm.hpp"

#include "../../libdnf5/utils/on_scope_exit.hpp"

#include <langinfo.h>
#include <libdnf5-cli/tty.hpp>
#include <libdnf5/conf/config_main.hpp>
#include <libdnf5/utils/bgettext/bgettext-lib.h>
#include <regex.h>
Expand All @@ -38,6 +41,14 @@ bool userconfirm(bool assume_no, bool assume_yes, bool default_yes) {
return true;
}

const bool cursor_was_hidden = libdnf5::cli::tty::is_cursor_hidden();
libdnf5::cli::tty::show_cursor();
libdnf5::utils::OnScopeExit restore_cursor([cursor_was_hidden]() noexcept {
if (cursor_was_hidden) {
libdnf5::cli::tty::hide_cursor();
}
});

const char * msg_id;
const char * msg_translated;

Expand Down
Loading