Compare commits

..

4 Commits

Author SHA1 Message Date
a577a3c254 Remove get for operator 2025-01-24 14:20:22 +01:00
bd5fe465a7 Better output 2025-01-24 14:20:03 +01:00
ea56f6ee0a Format 2025-01-24 14:07:33 +01:00
e0eba95ff4 Use snake_case 2025-01-24 14:06:26 +01:00
6 changed files with 44 additions and 22 deletions

View File

@ -86,8 +86,8 @@ std::string Page::str() const {
while (std::getline(stream, line)) while (std::getline(stream, line))
ret += line + "\n"; ret += line + "\n";
string_utils::limitConsecutiveWhitespace(ret, MAX_WHITESPACE); string_utils::limit_consecutive_whitespace(ret, MAX_WHITESPACE);
string_utils::removeTrailingWhitespace(ret); string_utils::remove_trailing_whitespace(ret);
return ret; return ret;
} }

View File

@ -24,12 +24,11 @@ public:
Pager &operator-=(int number); Pager &operator-=(int number);
Page &operator*(); Page &operator*();
Page *operator->(); Page *operator->();
void set(int number); void set(int number);
Page &get();
void clear(); void clear();
private: private:

View File

@ -50,6 +50,7 @@ void printHelp() {
} }
void Tui::run() { void Tui::run() {
clear_console();
std::cout << m_pager->str_pretty() << std::endl; std::cout << m_pager->str_pretty() << std::endl;
for (;;) { for (;;) {
std::cout << "Enter a command: "; std::cout << "Enter a command: ";
@ -62,32 +63,34 @@ void Tui::run() {
continue; continue;
} }
bool should_exit = false; 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>; using T = std::decay_t<T0>;
if constexpr (std::is_same_v<T, Command::None>) { if constexpr (std::is_same_v<T, Command::None>) {
std::cout << "Invalid command!" << std::endl; std::cout << "Invalid command!" << std::endl;
} else if constexpr (std::is_same_v<T, Command::Next>) { } else if constexpr (std::is_same_v<T, Command::Next>) {
try { try {
std::cout << "Fetching..." << std::endl;
m_pager += 1; m_pager += 1;
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
return; return;
} }
std::cout << m_pager->str_pretty() << std::endl; should_output = true;
} else if constexpr (std::is_same_v<T, Command::Previous>) { } else if constexpr (std::is_same_v<T, Command::Previous>) {
try { try {
std::cout << "Fetching..." << std::endl;
m_pager -= 1; m_pager -= 1;
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
return; return;
} }
std::cout << m_pager->str_pretty() << std::endl; should_output = true;
} else if constexpr (std::is_same_v<T, Command::Refresh>) { } else if constexpr (std::is_same_v<T, Command::Refresh>) {
m_pager.clear(); m_pager.clear();
std::cout << m_pager->str_pretty() << std::endl; should_output = true;
} else if constexpr (std::is_same_v<T, Command::Exit>) { } else if constexpr (std::is_same_v<T, Command::Exit>) {
should_exit = true; should_exit = true;
} else if constexpr (std::is_same_v<T, Command::Help>) { } else if constexpr (std::is_same_v<T, Command::Help>) {
@ -96,16 +99,32 @@ void Tui::run() {
const Command::Seek &seek = arg; const Command::Seek &seek = arg;
const int number = seek.number; const int number = seek.number;
try { try {
std::cout << "Fetching..." << std::endl;
m_pager.seek(number); m_pager.seek(number);
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
return; return;
} }
std::cout << m_pager->str_pretty() << std::endl; should_output = true;
} }
}; };
std::visit(visitor, command); std::visit(visitor, command);
if (should_exit) if (should_exit)
return; 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);
}

View File

@ -16,6 +16,10 @@ public:
void run(); void run();
private:;
static void clear_console();
private: private:
Pager m_pager; Pager m_pager;
}; };

View File

@ -14,28 +14,28 @@
namespace string_utils { 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::ranges::all_of(str, [](const unsigned char c) -> bool {
return std::isspace(c); return std::isspace(c);
}); });
} }
void removeTrailingWhitespace(std::string &str) { void remove_trailing_whitespace(std::string &str) {
auto shouldRemoveTrailingWhitespace = [&str]() -> bool { auto shouldRemoveTrailingWhitespace = [&str]() -> bool {
std::size_t last_newline = str.find_last_of('\n'); std::size_t last_newline = str.find_last_of('\n');
if (last_newline == std::string::npos) if (last_newline == std::string::npos)
return isAllWhitespace(str); return is_all_whitespace(str);
const std::string last_line = str.substr(last_newline + 1); const std::string last_line = str.substr(last_newline + 1);
return isAllWhitespace(last_line); return is_all_whitespace(last_line);
}; };
while (shouldRemoveTrailingWhitespace()) { while (shouldRemoveTrailingWhitespace()) {
const std::size_t last_newline = str.find_last_of('\n'); const std::size_t last_newline = str.find_last_of('\n');
if (last_newline == std::string::npos) { if (last_newline == std::string::npos) {
if (isAllWhitespace(str)) if (is_all_whitespace(str))
str.clear(); str.clear();
break; break;
} }
@ -44,11 +44,11 @@ namespace string_utils {
} }
} }
void removeTabs(std::string &str) { void remove_tabs(std::string &str) {
std::erase(str, '\t'); 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::istringstream stream(str);
std::string line; std::string line;
std::ostringstream processedStream; std::ostringstream processedStream;
@ -57,7 +57,7 @@ namespace string_utils {
bool hasAddedRealTextJet = false; bool hasAddedRealTextJet = false;
while (std::getline(stream, line)) { while (std::getline(stream, line)) {
const bool onlySpace = isAllWhitespace(line); const bool onlySpace = is_all_whitespace(line);
if (!hasAddedRealTextJet) { if (!hasAddedRealTextJet) {
if (onlySpace) if (onlySpace)

View File

@ -23,7 +23,7 @@ namespace string_utils {
* @return true If all characters in the string are whitespace. * @return true If all characters in the string are whitespace.
* @return false Otherwise. * @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. * @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. * @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. * @brief Removes all tab characters from the given string.
* *
* @param str The string from which to remove tab characters. * @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. * @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 str The string to process.
* @param maxWhitespace The maximum allowed consecutive whitespace lines. * @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);
} }