Skip to content
Closed
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
23 changes: 9 additions & 14 deletions cpp-terminal/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,22 +179,17 @@ class Terminal: public BaseTerminal {
void restore_screen()
{
if (restore_screen_) {
write("\033[?1049l"); // restore screen
write("\033" "8"); // restore current cursor position
std::cout << "\033[?1049l" << std::flush; // restore screen
std::cout << "\033" "8" << std::flush; // restore current cursor position
restore_screen_ = false;
}
}

void save_screen()
{
restore_screen_ = true;
write("\033" "7"); // save current cursor position
write("\033[?1049h"); // save screen
}

static inline void write(const std::string& s)
{
std::cout << s << std::flush;
std::cout << "\033" "7" << std::flush; // save current cursor position
std::cout << "\033[?1049h" << std::flush; // save screen
}

// Waits for a key press, translates escape codes
Expand Down Expand Up @@ -385,7 +380,7 @@ class Terminal: public BaseTerminal {
{
char buf[32];
unsigned int i = 0;
write(cursor_position_report());
std::cout << cursor_position_report() << std::flush;
while (i < sizeof(buf) - 1) {
while (!read_raw(&buf[i])) {
};
Expand Down Expand Up @@ -421,19 +416,19 @@ class Terminal: public BaseTerminal {
explicit CursorOff(const Terminal& term)
: term{ term }
{
Term::Terminal::write(cursor_off());
std::cout << cursor_off() << std::flush;
}
~CursorOff()
{
Term::Terminal::write(cursor_on());
std::cout << cursor_on() << std::flush;
}
};
CursorOff cursor_off(*this);
int old_row{}, old_col{};
get_cursor_position(old_row, old_col);
write(move_cursor_right(999) + move_cursor_down(999));
std::cout << move_cursor_right(999) + move_cursor_down(999) << std::flush;
get_cursor_position(rows, cols);
write(move_cursor(old_row, old_col));
std::cout << move_cursor(old_row, old_col) << std::flush;
}
};

Expand Down
2 changes: 1 addition & 1 deletion examples/kilo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ void editorRefreshScreen(const Terminal) {

ab.append(cursor_on());

Term::Terminal::write(ab);
std::cout << ab << std::flush;
}

/*** input ***/
Expand Down