-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathterminal_pager.cpp
More file actions
213 lines (185 loc) · 4.48 KB
/
terminal_pager.cpp
File metadata and controls
213 lines (185 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <ranges>
// OS-specific libraries.
#include <sys/ioctl.h>
#include <termcolor/termcolor.hpp>
#include "ansi_code.hpp"
#include "input_output.hpp"
#include "terminal_pager.hpp"
#include "common.hpp"
terminal_pager::terminal_pager()
: m_rows(0), m_columns(0), m_start_row_index(0)
{
maybe_grab_cout();
}
terminal_pager::~terminal_pager()
{
release_cout();
}
std::string terminal_pager::get_input() const
{
// Blocks until input received.
std::string str;
char ch;
std::cin.get(ch);
str += ch;
if (ansi_code::is_escape_char(ch)) // Start of ANSI escape sequence.
{
do
{
std::cin.get(ch);
str += ch;
} while (!std::isalpha(ch)); // ANSI escape sequence ends with a letter.
}
return str;
}
void terminal_pager::maybe_grab_cout()
{
// Unfortunately need to access _internal namespace of termcolor to check if a tty.
if (termcolor::_internal::is_atty(std::cout))
{
// Should we do anything with cerr?
m_cout_rdbuf = std::cout.rdbuf(&m_stringbuf);
}
else
{
m_cout_rdbuf = std::cout.rdbuf();
}
}
bool terminal_pager::process_input(std::string input)
{
if (input.size() == 0)
{
return true;
}
switch (input[0])
{
case 'q':
case 'Q':
return true; // Exit pager.
case 'u':
case 'U':
scroll(true, true); // Up a page.
return false;
case 'd':
case 'D':
case ' ':
scroll(false, true); // Down a page.
return false;
case '\n':
scroll(false, false); // Down a line.
return false;
case '\e': // ANSI escape sequence.
// Cannot switch on a std::string.
if (ansi_code::is_up_arrow(input))
{
scroll(true, false); // Up a line.
return false;
}
else if (ansi_code::is_down_arrow(input))
{
scroll(false, false); // Down a line.
return false;
}
}
std::cout << ansi_code::bel;
return false;
}
void terminal_pager::release_cout()
{
std::cout.rdbuf(m_cout_rdbuf);
}
void terminal_pager::render_terminal() const
{
auto end_row_index = m_start_row_index + m_rows - 1;
std::cout << ansi_code::erase_screen;
std::cout << ansi_code::cursor_to_top;
for (size_t i = m_start_row_index; i < end_row_index; i++)
{
if (i >= m_lines.size())
{
break;
}
std::cout << m_lines[i] << std::endl;
}
std::cout << ansi_code::cursor_to_row(m_rows); // Move cursor to bottom row of terminal.
std::cout << ":";
}
void terminal_pager::scroll(bool up, bool page)
{
update_terminal_size();
const auto old_start_row_index = m_start_row_index;
size_t offset = page ? m_rows - 1 : 1;
if (up)
{
// Care needed to avoid underflow of unsigned size_t.
if (m_start_row_index >= offset)
{
m_start_row_index -= offset;
}
else
{
m_start_row_index = 0;
}
}
else
{
m_start_row_index += offset;
auto end_row_index = m_start_row_index + m_rows - 1;
if (end_row_index > m_lines.size())
{
m_start_row_index = m_lines.size() - (m_rows - 1);
}
}
if (m_start_row_index == old_start_row_index)
{
std::cout << ansi_code::bel;
}
else
{
render_terminal();
}
}
void terminal_pager::show()
{
release_cout();
m_lines = split_input_at_newlines(m_stringbuf.view());
update_terminal_size();
if (m_rows == 0 || m_lines.size() <= m_rows - 1)
{
// Don't need to use pager, can display directly.
for (auto line : m_lines)
{
std::cout << line << std::endl;
}
m_lines.clear();
return;
}
alternative_buffer alt_buffer;
m_start_row_index = 0;
render_terminal();
bool stop = false;
do
{
stop = process_input(get_input());
} while (!stop);
m_lines.clear();
m_start_row_index = 0;
}
void terminal_pager::update_terminal_size()
{
struct winsize size;
int err = ioctl(fileno(stdout), TIOCGWINSZ, &size);
if (err == 0)
{
m_rows = size.ws_row;
m_columns = size.ws_col;
}
else
{
m_rows = m_columns = 0;
}
}