Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Common UI (#17)
Browse files Browse the repository at this point in the history
* added printing div elements according to terminal width

* added test class and UI differences

* added menu printing
  • Loading branch information
samdai01 authored Jan 17, 2024
1 parent 3c084b4 commit a8c7f13
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(main)
add_subdirectory(ui)
add_subdirectory(boatTest)
1 change: 1 addition & 0 deletions src/boatTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(common)
2 changes: 2 additions & 0 deletions src/boatTest/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_library(boatTest_Common boatTest_common.cpp boatTest_common.h)
target_include_directories(boatTest_Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
13 changes: 13 additions & 0 deletions src/boatTest/common/boatTest_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "boatTest_common.h"

BoatTest::BoatTest() { name = "NONESPECIFIED"; }
BoatTest::BoatTest(std::string id, std::vector<std::string> topics, std::vector<std::string> messages)
{
name = id;
ROS_topics = topics;
ROS_messages = messages;
}

std::string BoatTest::getName() { return name; }
std::vector<std::string> BoatTest::getROSTopics() { return ROS_topics; }
std::vector<std::string> BoatTest::getROSMessages() { return ROS_messages; }
23 changes: 23 additions & 0 deletions src/boatTest/common/boatTest_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef BOATTEST_COMMON_H_
#define BOATTEST_COMMON_H_

/* Include Files */
#include <string>
#include <vector>

/* Objects */
class BoatTest
{
std::string name;
std::vector<std::string> ROS_topics;
std::vector<std::string> ROS_messages;

public:
BoatTest();
BoatTest(std::string name, std::vector<std::string> ROS_topics, std::vector<std::string> ROS_messages);
std::string getName();
std::vector<std::string> getROSTopics();
std::vector<std::string> getROSMessages();
};

#endif
3 changes: 2 additions & 1 deletion src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
15 changes: 10 additions & 5 deletions src/main/diagnostics.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#include <iostream>

#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<std::string, std::string> commands;
commands["ab"] = "Test AB";
commands["cd"] = "Test CD";
base_elements.printMenu(commands);

return 0;
}
12 changes: 12 additions & 0 deletions src/main/diagnostics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef DIAGNOSTICS_H_
#define DIAGNOSTICS_H_

/* Include Files */
#include <iostream>
#include <unordered_map>
#include <vector>

#include "boatTest_common.h"
#include "commonUI.h"

#endif
4 changes: 2 additions & 2 deletions src/ui/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
41 changes: 37 additions & 4 deletions src/ui/common/commonUI.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
#include "commonUI.h"

#include <sys/ioctl.h>
#include <unistd.h>

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<int>(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<std::string, std::string> commands)
{
for (const auto & command : commands) {
std::cout << '[' << command.first << "]" << command.second << std::endl;
}
}
18 changes: 18 additions & 0 deletions src/ui/common/commonUI.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#ifndef COMMON_UI_H_
#define COMMON_UI_H_

/* Include Files */
#include <sys/ioctl.h>
#include <unistd.h>

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

/* Defines */

#define TERMINAL_WIDTH_SCALE 0.6
#define TERMINAL_WIDTH_MIN 50

/* Objects */

class CommonUI
Expand All @@ -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<std::string, std::string> commands);
};

#endif

0 comments on commit a8c7f13

Please sign in to comment.