From bd5fe465a711bc5f73ce7f20be34f897498cf429 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Fri, 24 Jan 2025 14:20:03 +0100 Subject: [PATCH] Better output --- src/Tui.cpp | 31 +++++++++++++++++++++++++------ src/Tui.hpp | 4 ++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Tui.cpp b/src/Tui.cpp index f53374e..0a88644 100644 --- a/src/Tui.cpp +++ b/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](T0 &&arg) { + bool should_output = false; + auto visitor = [this, &should_exit, &should_output](T0 &&arg) { using T = std::decay_t; if constexpr (std::is_same_v) { std::cout << "Invalid command!" << std::endl; } else if constexpr (std::is_same_v) { 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) { 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) { m_pager.clear(); - std::cout << m_pager->str_pretty() << std::endl; + should_output = true; } else if constexpr (std::is_same_v) { should_exit = true; } else if constexpr (std::is_same_v) { @@ -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); +} diff --git a/src/Tui.hpp b/src/Tui.hpp index 351960e..1d7101c 100644 --- a/src/Tui.hpp +++ b/src/Tui.hpp @@ -16,6 +16,10 @@ public: void run(); +private:; + + static void clear_console(); + private: Pager m_pager; };