Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort info output by topic name #1804

Open
wants to merge 11 commits into
base: rolling
Choose a base branch
from
39 changes: 33 additions & 6 deletions rosbag2_py/src/rosbag2_py/format_bag_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <numeric>

#ifdef _WIN32
#include <time.h>
Expand Down Expand Up @@ -115,7 +117,8 @@ void format_topics_with_type(
const std::unordered_map<std::string, uint64_t> & messages_size,
bool verbose,
std::stringstream & info_stream,
int indentation_spaces)
int indentation_spaces,
std::string sort_method = "name")
MichaelOrlov marked this conversation as resolved.
Show resolved Hide resolved
{
if (topics.empty()) {
info_stream << std::endl;
Expand All @@ -139,13 +142,37 @@ void format_topics_with_type(
info_stream << std::endl;
};

std::vector<size_t> sorted_idx(topics.size());
std::iota(sorted_idx.begin(), sorted_idx.end(), 0);
std::sort(
sorted_idx.begin(),
sorted_idx.end(),
[&topics, sort_method](size_t i1, size_t i2) {
if (sort_method == "type")
{
return topics[i1].topic_metadata.type < topics[i2].topic_metadata.type;
}
if (sort_method == "count")
{
return topics[i1].message_count < topics[i2].message_count;
}
if (sort_method == "serialization_format")
MichaelOrlov marked this conversation as resolved.
Show resolved Hide resolved
{
std::string format_1 = topics[i1].topic_metadata.serialization_format;
std::string format_2 = topics[i2].topic_metadata.serialization_format;
return format_1 < format_2;
}
return topics[i1].topic_metadata.name < topics[i2].topic_metadata.name;
}
);

size_t number_of_topics = topics.size();
size_t i = 0;
// Find first topic which isn't service event topic
while (i < number_of_topics &&
rosbag2_cpp::is_service_event_topic(
topics[i].topic_metadata.name,
topics[i].topic_metadata.type))
topics[sorted_idx[i]].topic_metadata.name,
topics[sorted_idx[i]].topic_metadata.type))
{
i++;
}
Expand All @@ -155,15 +182,15 @@ void format_topics_with_type(
return;
}

print_topic_info(topics[i]);
print_topic_info(topics[sorted_idx[i]]);
for (size_t j = ++i; j < number_of_topics; ++j) {
if (rosbag2_cpp::is_service_event_topic(
topics[j].topic_metadata.name, topics[j].topic_metadata.type))
topics[sorted_idx[j]].topic_metadata.name, topics[sorted_idx[j]].topic_metadata.type))
{
continue;
}
indent(info_stream, indentation_spaces);
print_topic_info(topics[j]);
print_topic_info(topics[sorted_idx[j]]);
}
}

Expand Down
Loading