From a8c7f1340de75ff3ea7ad6661264edd7a5b45f24 Mon Sep 17 00:00:00 2001 From: Sam Dai <89489298+samdai01@users.noreply.github.com> Date: Wed, 17 Jan 2024 11:44:13 -0800 Subject: [PATCH] Common UI (#17) * added printing div elements according to terminal width * added test class and UI differences * added menu printing --- src/CMakeLists.txt | 1 + src/boatTest/CMakeLists.txt | 1 + src/boatTest/common/CMakeLists.txt | 2 ++ src/boatTest/common/boatTest_common.cpp | 13 ++++++++ src/boatTest/common/boatTest_common.h | 23 ++++++++++++++ src/main/CMakeLists.txt | 3 +- src/main/diagnostics.cpp | 15 ++++++--- src/main/diagnostics.h | 12 ++++++++ src/ui/common/CMakeLists.txt | 4 +-- src/ui/common/commonUI.cpp | 41 ++++++++++++++++++++++--- src/ui/common/commonUI.h | 18 +++++++++++ 11 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 src/boatTest/CMakeLists.txt create mode 100644 src/boatTest/common/CMakeLists.txt create mode 100644 src/boatTest/common/boatTest_common.cpp create mode 100644 src/boatTest/common/boatTest_common.h create mode 100644 src/main/diagnostics.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 53cbaeb..7b19b6c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(main) add_subdirectory(ui) +add_subdirectory(boatTest) diff --git a/src/boatTest/CMakeLists.txt b/src/boatTest/CMakeLists.txt new file mode 100644 index 0000000..e4717b2 --- /dev/null +++ b/src/boatTest/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(common) diff --git a/src/boatTest/common/CMakeLists.txt b/src/boatTest/common/CMakeLists.txt new file mode 100644 index 0000000..768c1a7 --- /dev/null +++ b/src/boatTest/common/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(boatTest_Common boatTest_common.cpp boatTest_common.h) +target_include_directories(boatTest_Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/boatTest/common/boatTest_common.cpp b/src/boatTest/common/boatTest_common.cpp new file mode 100644 index 0000000..699e36a --- /dev/null +++ b/src/boatTest/common/boatTest_common.cpp @@ -0,0 +1,13 @@ +#include "boatTest_common.h" + +BoatTest::BoatTest() { name = "NONESPECIFIED"; } +BoatTest::BoatTest(std::string id, std::vector topics, std::vector messages) +{ + name = id; + ROS_topics = topics; + ROS_messages = messages; +} + +std::string BoatTest::getName() { return name; } +std::vector BoatTest::getROSTopics() { return ROS_topics; } +std::vector BoatTest::getROSMessages() { return ROS_messages; } diff --git a/src/boatTest/common/boatTest_common.h b/src/boatTest/common/boatTest_common.h new file mode 100644 index 0000000..3c79e12 --- /dev/null +++ b/src/boatTest/common/boatTest_common.h @@ -0,0 +1,23 @@ +#ifndef BOATTEST_COMMON_H_ +#define BOATTEST_COMMON_H_ + +/* Include Files */ +#include +#include + +/* Objects */ +class BoatTest +{ + std::string name; + std::vector ROS_topics; + std::vector ROS_messages; + +public: + BoatTest(); + BoatTest(std::string name, std::vector ROS_topics, std::vector ROS_messages); + std::string getName(); + std::vector getROSTopics(); + std::vector getROSMessages(); +}; + +#endif diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index 38c722f..be4b1b3 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -1,9 +1,10 @@ add_executable(diagnostics diagnostics.cpp) target_link_libraries(diagnostics PRIVATE - UI + UI_Common ) +target_link_libraries(diagnostics PRIVATE boatTest_Common) target_include_directories(diagnostics PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/src/main/diagnostics.cpp b/src/main/diagnostics.cpp index 3e18ac2..57489e9 100644 --- a/src/main/diagnostics.cpp +++ b/src/main/diagnostics.cpp @@ -1,15 +1,20 @@ -#include - -#include "commonUI.h" +#include "diagnostics.h" int main(int argc, char ** argv) { (void)argc; (void)argv; + CommonUI base_elements; - std::cout << "Welcome to UBC Sailbot Diagnostics! The C++ version" << std::endl; + base_elements.printDiv(); + std::string title = "Welcome to UBC Sailbot Diagnostics!"; + base_elements.printCenter(title); + base_elements.printDiv(); - CommonUI myObj; + std::unordered_map commands; + commands["ab"] = "Test AB"; + commands["cd"] = "Test CD"; + base_elements.printMenu(commands); return 0; } diff --git a/src/main/diagnostics.h b/src/main/diagnostics.h new file mode 100644 index 0000000..b8edf66 --- /dev/null +++ b/src/main/diagnostics.h @@ -0,0 +1,12 @@ +#ifndef DIAGNOSTICS_H_ +#define DIAGNOSTICS_H_ + +/* Include Files */ +#include +#include +#include + +#include "boatTest_common.h" +#include "commonUI.h" + +#endif diff --git a/src/ui/common/CMakeLists.txt b/src/ui/common/CMakeLists.txt index 5e96001..61298e0 100644 --- a/src/ui/common/CMakeLists.txt +++ b/src/ui/common/CMakeLists.txt @@ -1,2 +1,2 @@ -add_library(UI STATIC commonUI.cpp commonUI.h) -target_include_directories(UI PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +add_library(UI_Common commonUI.cpp commonUI.h) +target_include_directories(UI_Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/ui/common/commonUI.cpp b/src/ui/common/commonUI.cpp index bf41791..bbde01c 100644 --- a/src/ui/common/commonUI.cpp +++ b/src/ui/common/commonUI.cpp @@ -1,13 +1,46 @@ #include "commonUI.h" -#include -#include - CommonUI::CommonUI() { terminal_width = getTerminalWidth(); } +CommonUI::CommonUI(int user_set_width) { terminal_width = user_set_width; } int CommonUI::getTerminalWidth() { + int current_width; struct winsize size; ioctl(STDOUT_FILENO, TIOCGWINSZ, &size); - return size.ws_col; + if (size.ws_col > TERMINAL_WIDTH_MIN) { + current_width = static_cast(size.ws_col * TERMINAL_WIDTH_SCALE); + } else { + current_width = size.ws_col; + } + return current_width; +} + +void CommonUI::printDiv() const +{ + std::cout << std::endl; + + for (int i = 0; i < terminal_width; i++) { + std::cout << '='; + } + + std::cout << std::endl; +} + +void CommonUI::printCenter(std::string contents) const +{ + int left_padding = (terminal_width / 2) - (contents.size() / 2); + + for (int pad = 0; pad < left_padding; pad++) { + std::cout << ' '; + } + + std::cout << contents << std::endl; +} + +void CommonUI::printMenu(std::unordered_map commands) +{ + for (const auto & command : commands) { + std::cout << '[' << command.first << "]" << command.second << std::endl; + } } diff --git a/src/ui/common/commonUI.h b/src/ui/common/commonUI.h index f36dab3..50e16b6 100644 --- a/src/ui/common/commonUI.h +++ b/src/ui/common/commonUI.h @@ -1,6 +1,20 @@ #ifndef COMMON_UI_H_ #define COMMON_UI_H_ +/* Include Files */ +#include +#include + +#include +#include +#include +#include + +/* Defines */ + +#define TERMINAL_WIDTH_SCALE 0.6 +#define TERMINAL_WIDTH_MIN 50 + /* Objects */ class CommonUI @@ -11,6 +25,10 @@ class CommonUI public: CommonUI(); + explicit CommonUI(int user_set_width); + void printDiv() const; + void printCenter(std::string contents) const; + static void printMenu(std::unordered_map commands); }; #endif