From 19cdfefcbd32e13200db29e328f16fe82c130e30 Mon Sep 17 00:00:00 2001 From: Mohsen <47596867+mohsenomidi@users.noreply.github.com> Date: Sat, 3 Oct 2020 14:19:38 +0330 Subject: [PATCH 1/6] Create refresh_table.cpp demonstration of real time table refresh. This is the simple use case of refreshing the table. --- samples/refresh_table.cpp | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 samples/refresh_table.cpp diff --git a/samples/refresh_table.cpp b/samples/refresh_table.cpp new file mode 100644 index 0000000..b6d6eaf --- /dev/null +++ b/samples/refresh_table.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +using namespace tabulate; +using Row_t = std::vector>; +static volatile bool keep_running = true; + +static void *userInput_thread(void *) { + while (keep_running) { + if (std::cin.get() == 10) { + keep_running = false; + } + } + return NULL; +} + +int main() { + pthread_t tId; + (void)pthread_create(&tId, 0, userInput_thread, 0); + while (keep_running) { + Table process_table; + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0, 1); + process_table.add_row(Row_t{"PID", "%CPU", "%MEM", "User", "NI"}); + process_table.add_row( + Row_t{"4297", std::to_string((int)round(dis(gen) * 100)), + std::to_string((int)round(dis(gen) * 100)), "ubuntu", "20"}); + process_table.add_row( + Row_t{"12671", std::to_string((int)round(dis(gen) * 100)), + std::to_string((int)round(dis(gen) * 100)), "root", "0"}); + process_table.add_row({"810", std::to_string((int)round(dis(gen) * 100)), + std::to_string((int)round(dis(gen) * 100)), "root", + "-20"}); + + // center align 'Director' column + process_table.column(2).format().font_align(FontAlign::center); + + // right align 'Estimated Budget' column + process_table.column(3).format().font_align(FontAlign::right); + + // right align 'Release Date' column + process_table.column(4).format().font_align(FontAlign::right); + + // center-align and color header cells + for (size_t i = 0; i < 5; ++i) { + process_table[0][i] + .format() + .font_color(Color::yellow) + .font_align(FontAlign::center) + .font_style({FontStyle::bold}); + } + + std::cout << process_table << std::endl; + std::cout << "Press ENTER to exit..." << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::cout << "\033[F\033[F\033[F\033[F\033[F\033[F\033[F\033[F\033[F\033[F"; + } + std::cout << "\033[B\033[B\033[B\033[B\033[B\033[B\033[B\033[B\033[B\033[B"; + (void)pthread_join(tId, NULL); + + return 0; +} From 3deb3455885d6e2864c9800219408a415c311b0d Mon Sep 17 00:00:00 2001 From: Mohsen <47596867+mohsenomidi@users.noreply.github.com> Date: Sat, 3 Oct 2020 14:20:43 +0330 Subject: [PATCH 2/6] Update CMakeLists.txt Update for refresh_table example --- samples/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 98eff44..db366c0 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -47,4 +47,6 @@ add_executable(word_wrapping word_wrapping.cpp) target_link_libraries(word_wrapping PRIVATE tabulate::tabulate) add_executable(asciidoc_export asciidoc_export.cpp) -target_link_libraries(asciidoc_export PRIVATE tabulate::tabulate) \ No newline at end of file +target_link_libraries(asciidoc_export PRIVATE tabulate::tabulate) + +add_executable(refresh_table refresh_table.cpp) From 4f4bb5aa7510813c1fbbd76f68fac613d92ba2e2 Mon Sep 17 00:00:00 2001 From: Mohsen <47596867+mohsenomidi@users.noreply.github.com> Date: Sat, 3 Oct 2020 14:35:55 +0330 Subject: [PATCH 3/6] add missing target_link_libraries add missing target_link_libraries --- samples/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index db366c0..f8285c1 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -50,3 +50,4 @@ add_executable(asciidoc_export asciidoc_export.cpp) target_link_libraries(asciidoc_export PRIVATE tabulate::tabulate) add_executable(refresh_table refresh_table.cpp) +target_link_libraries(refresh_table PRIVATE tabulate::tabulate) From b22a8ed4dbb8d706f244715b7a2eb0ec1ed4b4f0 Mon Sep 17 00:00:00 2001 From: Mohsen <47596867+mohsenomidi@users.noreply.github.com> Date: Sun, 4 Oct 2020 16:51:00 +0330 Subject: [PATCH 4/6] push requested changes 1- change variable to atomic to avoid data races. 2- using std::thread for cross-platform support. 3- remove unnecessary include files . --- samples/refresh_table.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/samples/refresh_table.cpp b/samples/refresh_table.cpp index b6d6eaf..e5431d7 100644 --- a/samples/refresh_table.cpp +++ b/samples/refresh_table.cpp @@ -1,24 +1,20 @@ -#include -#include #include -#include using namespace tabulate; using Row_t = std::vector>; -static volatile bool keep_running = true; +std::atomic_bool keep_running(true); -static void *userInput_thread(void *) { +void waitingForWorkEnterKey() { while (keep_running) { if (std::cin.get() == 10) { keep_running = false; } } - return NULL; + return; } int main() { - pthread_t tId; - (void)pthread_create(&tId, 0, userInput_thread, 0); + std::thread tUserInput(waitingForWorkEnterKey); while (keep_running) { Table process_table; std::random_device rd; @@ -35,16 +31,10 @@ int main() { std::to_string((int)round(dis(gen) * 100)), "root", "-20"}); - // center align 'Director' column process_table.column(2).format().font_align(FontAlign::center); - - // right align 'Estimated Budget' column process_table.column(3).format().font_align(FontAlign::right); - - // right align 'Release Date' column process_table.column(4).format().font_align(FontAlign::right); - // center-align and color header cells for (size_t i = 0; i < 5; ++i) { process_table[0][i] .format() @@ -59,7 +49,7 @@ int main() { std::cout << "\033[F\033[F\033[F\033[F\033[F\033[F\033[F\033[F\033[F\033[F"; } std::cout << "\033[B\033[B\033[B\033[B\033[B\033[B\033[B\033[B\033[B\033[B"; - (void)pthread_join(tId, NULL); + tUserInput.join(); return 0; } From c874fb61c122077e3cb1dc4b148b2f2a067812da Mon Sep 17 00:00:00 2001 From: Mohsen <47596867+mohsenomidi@users.noreply.github.com> Date: Mon, 5 Oct 2020 09:08:45 +0330 Subject: [PATCH 5/6] revert includes Fix the compile error (Missing includes) --- samples/refresh_table.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/samples/refresh_table.cpp b/samples/refresh_table.cpp index e5431d7..f2021cf 100644 --- a/samples/refresh_table.cpp +++ b/samples/refresh_table.cpp @@ -1,4 +1,9 @@ +#include +#include +#include #include +#include + using namespace tabulate; using Row_t = std::vector>; From 176465db668da7f598865e219aba05d535e20585 Mon Sep 17 00:00:00 2001 From: Mohsen <47596867+mohsenomidi@users.noreply.github.com> Date: Mon, 5 Oct 2020 09:15:22 +0330 Subject: [PATCH 6/6] Update CMakeLists.txt --- samples/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index f8285c1..6a48116 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -50,4 +50,4 @@ add_executable(asciidoc_export asciidoc_export.cpp) target_link_libraries(asciidoc_export PRIVATE tabulate::tabulate) add_executable(refresh_table refresh_table.cpp) -target_link_libraries(refresh_table PRIVATE tabulate::tabulate) +target_link_libraries(refresh_table PRIVATE tabulate::tabulate pthread)