diff --git a/src/utils/input_output.cpp b/src/utils/input_output.cpp index 9e0ac45..21a0578 100644 --- a/src/utils/input_output.cpp +++ b/src/utils/input_output.cpp @@ -1,19 +1,39 @@ #include "input_output.hpp" +#include + #include "ansi_code.hpp" // OS-specific libraries. #include +unsigned int cursor_hider::s_scope_count = 0; + cursor_hider::cursor_hider(bool hide /* = true */) : m_hide(hide) { - std::cout << (m_hide ? ansi_code::hide_cursor : ansi_code::show_cursor); + s_scope_count++; + write_ansi_code(m_hide); } cursor_hider::~cursor_hider() { - std::cout << (m_hide ? ansi_code::show_cursor : ansi_code::hide_cursor); + s_scope_count--; + + if (s_scope_count == 0) + { + // Ensure cursor is visible when git2cpp exits. + write_ansi_code(false); + } + else + { + write_ansi_code(!m_hide); + } +} + +void cursor_hider::write_ansi_code(bool hide) +{ + std::cout << (hide ? ansi_code::hide_cursor : ansi_code::show_cursor); } alternative_buffer::alternative_buffer() diff --git a/src/utils/input_output.hpp b/src/utils/input_output.hpp index bb11ae1..3e56e55 100644 --- a/src/utils/input_output.hpp +++ b/src/utils/input_output.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include "common.hpp" // OS-specific libraries. @@ -22,7 +20,11 @@ class cursor_hider : noncopyable_nonmovable private: + void write_ansi_code(bool hide); + bool m_hide; + + static unsigned int s_scope_count; }; // Scope object to use alternative output buffer for