Skip to content

Commit 95ad5e6

Browse files
committed
Fix cursor showing and hiding
1 parent ad032f0 commit 95ad5e6

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/utils/input_output.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
#include "input_output.hpp"
22

3+
#include <iostream>
4+
35
#include "ansi_code.hpp"
46

57
// OS-specific libraries.
68
#include <sys/ioctl.h>
79

10+
unsigned int cursor_hider::s_scope_count = 0;
11+
812
cursor_hider::cursor_hider(bool hide /* = true */)
913
: m_hide(hide)
1014
{
11-
std::cout << (m_hide ? ansi_code::hide_cursor : ansi_code::show_cursor);
15+
s_scope_count++;
16+
write_ansi_code(m_hide);
1217
}
1318

1419
cursor_hider::~cursor_hider()
1520
{
16-
std::cout << (m_hide ? ansi_code::show_cursor : ansi_code::hide_cursor);
21+
s_scope_count--;
22+
23+
if (s_scope_count == 0)
24+
{
25+
// Ensure cursor is visible when git2cpp exits.
26+
write_ansi_code(false);
27+
}
28+
else
29+
{
30+
write_ansi_code(!m_hide);
31+
}
32+
}
33+
34+
void cursor_hider::write_ansi_code(bool hide)
35+
{
36+
std::cout << (hide ? ansi_code::hide_cursor : ansi_code::show_cursor);
1737
}
1838

1939
alternative_buffer::alternative_buffer()

src/utils/input_output.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <iostream>
3+
#include <stack>
44

55
#include "common.hpp"
66

@@ -22,7 +22,11 @@ class cursor_hider : noncopyable_nonmovable
2222

2323
private:
2424

25+
void write_ansi_code(bool hide);
26+
2527
bool m_hide;
28+
29+
static unsigned int s_scope_count;
2630
};
2731

2832
// Scope object to use alternative output buffer for

0 commit comments

Comments
 (0)