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