-
Notifications
You must be signed in to change notification settings - Fork 3
Implement terminal_pager for log subcommand #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+388
−2
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97cf63b
Implement terminal_pager for log subcommand
ianthomas23 e083e48
Include cstdint
ianthomas23 44c04be
Use stringbuf instead of ostringstream
ianthomas23 f7e4643
Remove m_grabbed
ianthomas23 0edd017
Pass by value to process_input
ianthomas23 afd276a
Separate namespace for ANSI code sequences to avoid magic strings
ianthomas23 b77eb87
alternative_buffer scope object
ianthomas23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "ansi_code.hpp" | ||
|
||
namespace ansi_code | ||
{ | ||
std::string cursor_to_row(size_t row) | ||
{ | ||
return "\e[" + std::to_string(row) + "H"; | ||
} | ||
|
||
bool is_down_arrow(std::string str) | ||
{ | ||
return str == "\e[B" || str == "\e[1B]"; | ||
} | ||
|
||
bool is_escape_char(char ch) | ||
{ | ||
return ch == '\e'; | ||
} | ||
|
||
bool is_up_arrow(std::string str) | ||
{ | ||
return str == "\e[A" || str == "\e[1A]"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
/** | ||
* ANSI escape codes. | ||
* Use `termcolor` for colours. | ||
*/ | ||
namespace ansi_code | ||
{ | ||
// Constants. | ||
const std::string bel = "\a"; // ASCII 7, used for audio/visual feedback. | ||
const std::string cursor_to_top = "\e[H"; | ||
const std::string erase_screen = "\e[2J"; | ||
|
||
const std::string enable_alternative_buffer = "\e[?1049h"; | ||
const std::string disable_alternative_buffer = "\e[?1049l"; | ||
|
||
const std::string hide_cursor = "\e[?25l"; | ||
const std::string show_cursor = "\e[?25h"; | ||
|
||
// Functions. | ||
std::string cursor_to_row(size_t row); | ||
|
||
bool is_escape_char(char ch); | ||
|
||
bool is_down_arrow(std::string str); | ||
bool is_up_arrow(std::string str); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "output.hpp" | ||
|
||
// OS-specific libraries. | ||
#include <sys/ioctl.h> | ||
|
||
alternative_buffer::alternative_buffer() | ||
{ | ||
tcgetattr(fileno(stdin), &m_previous_termios); | ||
auto new_termios = m_previous_termios; | ||
// Disable canonical mode (buffered I/O) and echo from stdin to stdout. | ||
new_termios.c_lflag &= (~ICANON & ~ECHO); | ||
tcsetattr(fileno(stdin), TCSANOW, &new_termios); | ||
|
||
std::cout << ansi_code::enable_alternative_buffer; | ||
} | ||
|
||
alternative_buffer::~alternative_buffer() | ||
{ | ||
std::cout << ansi_code::disable_alternative_buffer; | ||
|
||
// Restore previous termios settings. | ||
tcsetattr(fileno(stdin), TCSANOW, &m_previous_termios); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include "ansi_code.hpp" | ||
#include "common.hpp" | ||
|
||
// OS-specific libraries. | ||
#include <termios.h> | ||
|
||
// Scope object to hide the cursor. This avoids | ||
// cursor twinkling when rewritting the same line | ||
// too frequently. | ||
struct cursor_hider : noncopyable_nonmovable | ||
{ | ||
cursor_hider() | ||
{ | ||
std::cout << "\e[?25l"; | ||
std::cout << ansi_code::hide_cursor; | ||
} | ||
|
||
~cursor_hider() | ||
{ | ||
std::cout << "\e[?25h"; | ||
std::cout << ansi_code::show_cursor; | ||
} | ||
}; | ||
|
||
// Scope object to use alternative output buffer for | ||
// fullscreen interactive terminal input/output. | ||
class alternative_buffer : noncopyable_nonmovable | ||
{ | ||
public: | ||
alternative_buffer(); | ||
|
||
~alternative_buffer(); | ||
|
||
private: | ||
struct termios m_previous_termios; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
#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 "output.hpp" | ||
#include "terminal_pager.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(); | ||
|
||
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::split_input_at_newlines(std::string_view str) | ||
{ | ||
auto split = str | std::ranges::views::split('\n') | ||
| std::ranges::views::transform([](auto&& range) { | ||
return std::string(range.begin(), std::ranges::distance(range)); | ||
}); | ||
m_lines = std::vector<std::string>{split.begin(), split.end()}; | ||
} | ||
|
||
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably capture
std::cerr
too as by default (at least on some platforms) it outputs to the same place asstd::cout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do need a strategy for what to do with
cerr
, but I don't know what is best yet.Currently
cerr
will be written to the normal terminal buffer, so it won't be seen whilst the alternative buffer is being used but it reappears when the alternative buffer is disabled. This is perhaps not a good solution, but I don't think it is too bad for a first implementation. Some other options:cout
, it will appear in the output flow in the alternative buffer. We'd probably need to colour it to make is easily visible as it could be anywhere.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a preference for option 1, but I think we can discuss it in a dedicated issue and solve it in a dedicated PR so that it does not block this one, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree let's deal with
cerr
separately so that we can merge and use this now.