Skip to content

Commit b77eb87

Browse files
committed
alternative_buffer scope object
1 parent afd276a commit b77eb87

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ set(GIT2CPP_SRC
6464
${GIT2CPP_SOURCE_DIR}/utils/common.hpp
6565
${GIT2CPP_SOURCE_DIR}/utils/git_exception.cpp
6666
${GIT2CPP_SOURCE_DIR}/utils/git_exception.hpp
67+
${GIT2CPP_SOURCE_DIR}/utils/output.cpp
6768
${GIT2CPP_SOURCE_DIR}/utils/output.hpp
6869
${GIT2CPP_SOURCE_DIR}/utils/terminal_pager.cpp
6970
${GIT2CPP_SOURCE_DIR}/utils/terminal_pager.hpp

src/utils/output.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "output.hpp"
2+
3+
// OS-specific libraries.
4+
#include <sys/ioctl.h>
5+
6+
alternative_buffer::alternative_buffer()
7+
{
8+
tcgetattr(fileno(stdin), &m_previous_termios);
9+
auto new_termios = m_previous_termios;
10+
// Disable canonical mode (buffered I/O) and echo from stdin to stdout.
11+
new_termios.c_lflag &= (~ICANON & ~ECHO);
12+
tcsetattr(fileno(stdin), TCSANOW, &new_termios);
13+
14+
std::cout << ansi_code::enable_alternative_buffer;
15+
}
16+
17+
alternative_buffer::~alternative_buffer()
18+
{
19+
std::cout << ansi_code::disable_alternative_buffer;
20+
21+
// Restore previous termios settings.
22+
tcsetattr(fileno(stdin), TCSANOW, &m_previous_termios);
23+
}

src/utils/output.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "ansi_code.hpp"
55
#include "common.hpp"
66

7+
// OS-specific libraries.
8+
#include <termios.h>
9+
710
// Scope object to hide the cursor. This avoids
811
// cursor twinkling when rewritting the same line
912
// too frequently.
@@ -19,3 +22,16 @@ struct cursor_hider : noncopyable_nonmovable
1922
std::cout << ansi_code::show_cursor;
2023
}
2124
};
25+
26+
// Scope object to use alternative output buffer for
27+
// fullscreen interactive terminal input/output.
28+
class alternative_buffer : noncopyable_nonmovable
29+
{
30+
public:
31+
alternative_buffer();
32+
33+
~alternative_buffer();
34+
35+
private:
36+
struct termios m_previous_termios;
37+
};

src/utils/terminal_pager.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
// OS-specific libraries.
88
#include <sys/ioctl.h>
9-
#include <termios.h>
109

1110
#include <termcolor/termcolor.hpp>
1211

1312
#include "ansi_code.hpp"
13+
#include "output.hpp"
1414
#include "terminal_pager.hpp"
1515

1616
terminal_pager::terminal_pager()
@@ -181,14 +181,7 @@ void terminal_pager::show()
181181
return;
182182
}
183183

184-
struct termios old_termios;
185-
tcgetattr(fileno(stdin), &old_termios);
186-
auto new_termios = old_termios;
187-
// Disable canonical mode (buffered I/O) and echo from stdin to stdout.
188-
new_termios.c_lflag &= (~ICANON & ~ECHO);
189-
tcsetattr(fileno(stdin), TCSANOW, &new_termios);
190-
191-
std::cout << ansi_code::enable_alternative_buffer;
184+
alternative_buffer alt_buffer;
192185

193186
m_start_row_index = 0;
194187
render_terminal();
@@ -199,11 +192,6 @@ void terminal_pager::show()
199192
stop = process_input(get_input());
200193
} while (!stop);
201194

202-
std::cout << ansi_code::disable_alternative_buffer;
203-
204-
// Restore original termios settings.
205-
tcsetattr(fileno(stdin), TCSANOW, &old_termios);
206-
207195
m_lines.clear();
208196
m_start_row_index = 0;
209197
}

0 commit comments

Comments
 (0)