Improved sequence diagram message comment formatting

This commit is contained in:
Bartek Kryza
2023-10-16 21:47:46 +02:00
parent 03e8c867f4
commit 1616f79ceb
5 changed files with 90 additions and 12 deletions

View File

@@ -287,19 +287,10 @@ void generator::generate_message_comment(
if (!from)
return;
if (m.type() == message_t::kCall) {
const auto &to = model().get_participant<model::participant>(m.to());
if (!to)
return;
ostr << "note over " << generate_alias(from.value()) << '\n';
ostr << "note over " << generate_alias(from.value()) << ", "
<< generate_alias(to.value()) << '\n';
}
else {
ostr << "note over " << generate_alias(from.value()) << '\n';
}
ostr << util::format_message_comment(m.comment().value()) << '\n';
ostr << m.comment().value() << '\n';
ostr << "end note" << '\n';
}

View File

@@ -199,6 +199,28 @@ std::vector<std::string> split(
return result;
}
std::vector<std::string> split_isspace(std::string str)
{
std::vector<std::string> result;
while (static_cast<unsigned int>(!str.empty()) != 0U) {
auto index = std::find_if(
str.begin(), str.end(), [](auto c) { return std::isspace(c); });
if (index != str.end()) {
auto tok = str.substr(0, std::distance(str.begin(), index));
if (!tok.empty())
result.push_back(std::move(tok));
str = str.substr(std::distance(str.begin(), index) + 1);
}
else {
if (!str.empty())
result.push_back(str);
str = "";
}
}
return result;
}
std::string join(
const std::vector<std::string> &toks, std::string_view delimiter)
{
@@ -344,4 +366,40 @@ std::filesystem::path ensure_path_is_absolute(
return result;
}
std::string format_message_comment(const std::string &comment, unsigned width)
{
if (width == 0)
return comment;
std::string result;
if (comment.empty())
return result;
auto tokens = split_isspace(comment);
if (tokens.empty())
return result;
unsigned current_line_length{0};
for (auto it = tokens.begin(); it != tokens.end(); it++) {
if (current_line_length < width) {
result += *it;
result += ' ';
}
else {
result.back() = '\n';
current_line_length = 0;
result += *it;
result += ' ';
}
current_line_length += it->size() + 1;
}
result.pop_back();
return result;
}
} // namespace clanguml::util

View File

@@ -167,6 +167,8 @@ std::string get_os_name();
std::vector<std::string> split(
std::string str, std::string_view delimiter, bool skip_empty = true);
std::vector<std::string> split_isspace(std::string str);
/**
* @brief Remove and erase elements from a vector
*
@@ -424,4 +426,6 @@ std::string path_to_url(const std::filesystem::path &p);
std::filesystem::path ensure_path_is_absolute(const std::filesystem::path &p,
const std::filesystem::path &root = std::filesystem::current_path());
std::string format_message_comment(const std::string &c, unsigned width = 25);
} // namespace clanguml::util

View File

@@ -12,7 +12,10 @@ template <typename T> class Encoder : public T {
public:
bool send(std::string &&msg)
{
return T::send(std::move(encode(std::move(msg))));
return T::send(std::move(
// Encode the message using Base64 encoding and pass it to the next
// layer
encode(std::move(msg))));
}
protected:
@@ -27,6 +30,7 @@ public:
int retryCount = 5;
// Repeat until send() succeeds or retry count is exceeded
while (retryCount--) {
if (T::send(buffer))
return true;

View File

@@ -396,3 +396,24 @@ TEST_CASE("Test tokenize_unexposed_template_parameter", "[unit-test]")
CHECK(r[i++] == ")");
}
}
TEST_CASE("Test format_message_comment", "[unit-test]")
{
using namespace clanguml::util;
CHECK(format_message_comment("\n\n ABCD \n", 0) == "\n\n ABCD \n");
CHECK(format_message_comment("\n\n ABCD \n") == "ABCD");
CHECK(format_message_comment(
"Repeat until send() succeeds or retry count is exceeded") ==
"Repeat until send() succeeds\nor retry count is exceeded");
CHECK(format_message_comment("12345678", 5) == "12345678");
CHECK(format_message_comment("") == "");
CHECK(format_message_comment(" ") == "");
CHECK(format_message_comment("This is a url: http://example.com/test/12345",
15) == "This is a url:\nhttp://example.com/test/12345");
}