Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BehaviorApprovalWidget : public QWidget

void timer_cb();

void process_input(const bool& input);
void process_input(const bool input);
Comment thread
msavchen-nasa marked this conversation as resolved.
Resolved

private Q_SLOTS:
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
/**
* Include header generated from ui file
* Note that you will need to use add_library function first
Expand All @@ -63,11 +64,36 @@ class BehaviorTreeWidget : public QWidget
rclcpp::Node::SharedPtr node_;
std::unique_ptr<Ui::behavior_tree_widget> ui_;

const std::string green = "\033[92m";
const std::string red = "\033[91m";
const std::string blue = "\033[96m";
const std::string yellow = "\033[93m";
const std::string end_color = "\033[0m";
static constexpr std::string green = "\033[92m";
static constexpr std::string red = "\033[91m";
static constexpr std::string blue = "\033[96m";
static constexpr std::string yellow = "\033[93m";
static constexpr std::string end_color = "\033[0m";

// Text replacement mapping using string_view for zero-allocation matching
struct Replacement
{
std::string_view old_text;
std::string_view new_text;
};

// Define the list of text replacements in the order they should be applied.
const std::vector<Replacement> text_replacements = { { green, "<font color=\"Green\">" },
{ "\x1b[32m", "<font color=\"Green\">" },
{ red, "<font color=\"Red\">" },
{ "\x1b[31m", "<font color=\"Red\">" },
{ blue, "<font color=\"Blue\">" },
{ "\x1b[34m", "<font color=\"Blue\">" },
{ yellow, "<font color=\"Orange\">" },
{ "\x1b[33m", "<font color=\"Orange\">" },
{ "\x1b[36m", "<font color=\"Cyan\">" },
{ end_color, "</font>" },
{ "\x1b[0m", "</font>" },
{ "\t", " " },
{ "│ ", "|&nbsp;&nbsp;&nbsp;" },
{ " ", "&nbsp;&nbsp;&nbsp;&nbsp;" },
{ "\n", "<br/>" },
{ "\r", "<br/>" } };

std::string active_behavior = "";

Expand All @@ -93,11 +119,10 @@ class BehaviorTreeWidget : public QWidget
void get_behavior_trees_cb(const rclcpp::Client<btcpp_ros2_interfaces::srv::GetTrees>::SharedFuture future);
void logger_callback(const std_msgs::msg::String::SharedPtr msg);
void bt_status_callback(const std_msgs::msg::String::SharedPtr msg);
std::string replace_string(std::string string_to_replace, std::string old_text, std::string new_text);

void update_bt_text();
void update_log_text();
std::string process_text(std::string text);
void process_text(std::string& text);

void waitForClient(rclcpp::ClientBase::SharedPtr client);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void BehaviorApprovalWidget::timer_cb()
}
}

void BehaviorApprovalWidget::process_input(const bool& input)
void BehaviorApprovalWidget::process_input(const bool input)
{
// Check if there optional variable is set
if (request_header)
Expand Down
81 changes: 37 additions & 44 deletions drt_behavior_visualization/src/ui/behavior_tree_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* under the License.
*/

#include <string_view>

#include "drt_behavior_visualization/ui/behavior_tree_widget.hpp"

namespace drt_behavior_visualization
Expand Down Expand Up @@ -175,44 +177,44 @@ void BehaviorTreeWidget::logger_callback(const std_msgs::msg::String::SharedPtr
update_log_text();
}

std::string BehaviorTreeWidget::process_text(std::string text)
void BehaviorTreeWidget::process_text(std::string& text)
{
// terminal green
text = replace_string(text, green, "<font color=\"Green\">");
// btcpp green
text = replace_string(text, "\x1b[32m", "<font color=\"Green\">");
// terminal red
text = replace_string(text, red, "<font color=\"Red\">");
// btcpp red
text = replace_string(text, "\x1b[31m", "<font color=\"Red\">");
// terminal blue
text = replace_string(text, blue, "<font color=\"Blue\">");
// btcpp blue
text = replace_string(text, "\x1b[34m", "<font color=\"Blue\">");
// terminal yellow
text = replace_string(text, yellow, "<font color=\"Orange\">");
// btcpp yelow
text = replace_string(text, "\x1b[33m", "<font color=\"Orange\">");
// btcpp cyan
text = replace_string(text, "\x1b[36m", "<font color=\"Cyan\">");
// terminal end color
text = replace_string(text, end_color, "</font>");
// btcpp end color
text = replace_string(text, "\x1b[0m", "</font>");
text = replace_string(text, "\t", " ");
text = replace_string(text, "│ ", "|&nbsp;&nbsp;&nbsp;");
text = replace_string(text, " ", "&nbsp;&nbsp;&nbsp;&nbsp;");
text = replace_string(text, "\n", "<br/>");
text = replace_string(text, "\r", "<br/>");
return text;
std::string result;
result.reserve(text.size() * 2); // Pre-allocate memory to prevent reallocations

size_t i = 0;
while (i < text.size())
{
bool matched = false;

for (const auto& [old_text, new_text] : text_replacements)
{
// Check if the current substring matches our token
if (text.compare(i, old_text.size(), old_text) == 0)
{
result.append(new_text);
i += old_text.size();
matched = true;
break; // Move to the next position in the main string
}
}

if (!matched)
{
result.push_back(text[i]);
i++;
}
}

text = std::move(result); // Efficiently swap the processed string back
}

void BehaviorTreeWidget::update_bt_text()
{
bool first_time = true;
std::string bt_print_text = "";

for (const auto& bt_data : bt_text)
for (auto bt_data : bt_text)
{
if (first_time)
{
Expand All @@ -222,7 +224,8 @@ void BehaviorTreeWidget::update_bt_text()
{
bt_print_text += "<br/>";
}
bt_print_text += process_text(bt_data);
process_text(bt_data);
bt_print_text += bt_data;
}

auto formatted_bt_print_text = "<pre>" + bt_print_text + "</pre>";
Expand Down Expand Up @@ -273,7 +276,7 @@ void BehaviorTreeWidget::update_log_text()
bool first_time = true;
std::string log_text = "";

for (const auto& log_data : log)
for (auto log_data : log)
{
if (first_time)
{
Expand All @@ -283,7 +286,8 @@ void BehaviorTreeWidget::update_log_text()
{
log_text += "<br/>";
}
log_text += process_text(log_data);
process_text(log_data);
log_text += log_data;
}

auto formatted_log_text = "<pre style='white-space: pre-wrap;'>" + log_text + "</pre>";
Expand All @@ -298,17 +302,6 @@ void BehaviorTreeWidget::update_log_text()
}
}

std::string BehaviorTreeWidget::replace_string(std::string string_to_replace, std::string old_text, std::string new_text)
{
size_t pos = 0;
while ((pos = string_to_replace.find(old_text, pos)) != std::string::npos)
{
string_to_replace.replace(pos, old_text.length(), new_text);
pos += new_text.length(); // Move past the replaced part
}
return string_to_replace;
}

void BehaviorTreeWidget::waitForClient(rclcpp::ClientBase::SharedPtr client)
{
// Get the name of the client
Expand Down
Loading