Compare commits
4 Commits
51943a5123
...
master
Author | SHA1 | Date | |
---|---|---|---|
a577a3c254 | |||
bd5fe465a7 | |||
ea56f6ee0a | |||
e0eba95ff4 |
@ -86,8 +86,8 @@ std::string Page::str() const {
|
||||
while (std::getline(stream, line))
|
||||
ret += line + "\n";
|
||||
|
||||
string_utils::limitConsecutiveWhitespace(ret, MAX_WHITESPACE);
|
||||
string_utils::removeTrailingWhitespace(ret);
|
||||
string_utils::limit_consecutive_whitespace(ret, MAX_WHITESPACE);
|
||||
string_utils::remove_trailing_whitespace(ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -24,12 +24,11 @@ public:
|
||||
Pager &operator-=(int number);
|
||||
|
||||
Page &operator*();
|
||||
|
||||
Page *operator->();
|
||||
|
||||
void set(int number);
|
||||
|
||||
Page &get();
|
||||
|
||||
void clear();
|
||||
|
||||
private:
|
||||
|
31
src/Tui.cpp
31
src/Tui.cpp
@ -50,6 +50,7 @@ void printHelp() {
|
||||
}
|
||||
|
||||
void Tui::run() {
|
||||
clear_console();
|
||||
std::cout << m_pager->str_pretty() << std::endl;
|
||||
for (;;) {
|
||||
std::cout << "Enter a command: ";
|
||||
@ -62,32 +63,34 @@ void Tui::run() {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
bool should_exit = false;
|
||||
auto visitor = [this, &should_exit]<typename T0>(T0 &&arg) {
|
||||
bool should_output = false;
|
||||
auto visitor = [this, &should_exit, &should_output]<typename T0>(T0 &&arg) {
|
||||
using T = std::decay_t<T0>;
|
||||
|
||||
if constexpr (std::is_same_v<T, Command::None>) {
|
||||
std::cout << "Invalid command!" << std::endl;
|
||||
} else if constexpr (std::is_same_v<T, Command::Next>) {
|
||||
try {
|
||||
std::cout << "Fetching..." << std::endl;
|
||||
m_pager += 1;
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << m_pager->str_pretty() << std::endl;
|
||||
should_output = true;
|
||||
} else if constexpr (std::is_same_v<T, Command::Previous>) {
|
||||
try {
|
||||
std::cout << "Fetching..." << std::endl;
|
||||
m_pager -= 1;
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << m_pager->str_pretty() << std::endl;
|
||||
should_output = true;
|
||||
} else if constexpr (std::is_same_v<T, Command::Refresh>) {
|
||||
m_pager.clear();
|
||||
std::cout << m_pager->str_pretty() << std::endl;
|
||||
should_output = true;
|
||||
} else if constexpr (std::is_same_v<T, Command::Exit>) {
|
||||
should_exit = true;
|
||||
} else if constexpr (std::is_same_v<T, Command::Help>) {
|
||||
@ -96,16 +99,32 @@ void Tui::run() {
|
||||
const Command::Seek &seek = arg;
|
||||
const int number = seek.number;
|
||||
try {
|
||||
std::cout << "Fetching..." << std::endl;
|
||||
m_pager.seek(number);
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << m_pager->str_pretty() << std::endl;
|
||||
should_output = true;
|
||||
}
|
||||
};
|
||||
std::visit(visitor, command);
|
||||
if (should_exit)
|
||||
return;
|
||||
|
||||
clear_console();
|
||||
if (should_output)
|
||||
std::cout << m_pager->str_pretty() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Tui::clear_console() {
|
||||
const auto CMD =
|
||||
#ifdef _WIN32 || _WIN64
|
||||
"cls";
|
||||
#else
|
||||
"clear";
|
||||
#endif
|
||||
|
||||
system(CMD);
|
||||
}
|
||||
|
@ -16,6 +16,10 @@ public:
|
||||
|
||||
void run();
|
||||
|
||||
private:;
|
||||
|
||||
static void clear_console();
|
||||
|
||||
private:
|
||||
Pager m_pager;
|
||||
};
|
||||
|
@ -14,28 +14,28 @@
|
||||
|
||||
|
||||
namespace string_utils {
|
||||
bool isAllWhitespace(const std::string &str) {
|
||||
bool is_all_whitespace(const std::string &str) {
|
||||
return std::ranges::all_of(str, [](const unsigned char c) -> bool {
|
||||
return std::isspace(c);
|
||||
});
|
||||
}
|
||||
|
||||
void removeTrailingWhitespace(std::string &str) {
|
||||
void remove_trailing_whitespace(std::string &str) {
|
||||
auto shouldRemoveTrailingWhitespace = [&str]() -> bool {
|
||||
std::size_t last_newline = str.find_last_of('\n');
|
||||
|
||||
if (last_newline == std::string::npos)
|
||||
return isAllWhitespace(str);
|
||||
return is_all_whitespace(str);
|
||||
|
||||
const std::string last_line = str.substr(last_newline + 1);
|
||||
return isAllWhitespace(last_line);
|
||||
return is_all_whitespace(last_line);
|
||||
};
|
||||
|
||||
while (shouldRemoveTrailingWhitespace()) {
|
||||
const std::size_t last_newline = str.find_last_of('\n');
|
||||
|
||||
if (last_newline == std::string::npos) {
|
||||
if (isAllWhitespace(str))
|
||||
if (is_all_whitespace(str))
|
||||
str.clear();
|
||||
break;
|
||||
}
|
||||
@ -44,11 +44,11 @@ namespace string_utils {
|
||||
}
|
||||
}
|
||||
|
||||
void removeTabs(std::string &str) {
|
||||
void remove_tabs(std::string &str) {
|
||||
std::erase(str, '\t');
|
||||
}
|
||||
|
||||
void limitConsecutiveWhitespace(std::string &str, const uint_fast8_t maxWhitespace) {
|
||||
void limit_consecutive_whitespace(std::string &str, const uint_fast8_t maxWhitespace) {
|
||||
std::istringstream stream(str);
|
||||
std::string line;
|
||||
std::ostringstream processedStream;
|
||||
@ -57,7 +57,7 @@ namespace string_utils {
|
||||
bool hasAddedRealTextJet = false;
|
||||
|
||||
while (std::getline(stream, line)) {
|
||||
const bool onlySpace = isAllWhitespace(line);
|
||||
const bool onlySpace = is_all_whitespace(line);
|
||||
|
||||
if (!hasAddedRealTextJet) {
|
||||
if (onlySpace)
|
||||
|
@ -23,7 +23,7 @@ namespace string_utils {
|
||||
* @return true If all characters in the string are whitespace.
|
||||
* @return false Otherwise.
|
||||
*/
|
||||
bool isAllWhitespace(const std::string& str);
|
||||
bool is_all_whitespace(const std::string& str);
|
||||
|
||||
/**
|
||||
* @brief Removes trailing whitespace lines from the given string.
|
||||
@ -33,14 +33,14 @@ namespace string_utils {
|
||||
*
|
||||
* @param str The string from which to remove trailing whitespace lines.
|
||||
*/
|
||||
void removeTrailingWhitespace(std::string& str);
|
||||
void remove_trailing_whitespace(std::string& str);
|
||||
|
||||
/**
|
||||
* @brief Removes all tab characters from the given string.
|
||||
*
|
||||
* @param str The string from which to remove tab characters.
|
||||
*/
|
||||
void removeTabs(std::string& str);
|
||||
void remove_tabs(std::string& str);
|
||||
|
||||
/**
|
||||
* @brief Limits the number of consecutive whitespace lines in the given string.
|
||||
@ -51,6 +51,6 @@ namespace string_utils {
|
||||
* @param str The string to process.
|
||||
* @param maxWhitespace The maximum allowed consecutive whitespace lines.
|
||||
*/
|
||||
void limitConsecutiveWhitespace(std::string& str, uint_fast8_t maxWhitespace);
|
||||
void limit_consecutive_whitespace(std::string& str, uint_fast8_t maxWhitespace);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user