Skip to content

Commit

Permalink
Add support for new lines in Sequence (#40)
Browse files Browse the repository at this point in the history
* Add support for new lines in Sequence

When using `diagon Sequence` it is not possible (at least I couldn't
find a way) to add new lines to the messages in the Sequence submodule.

This patch adds parameter `--new_lines_at_bsn` that results in
substituting literal `\n` with new lines.

Example:

```bash
$ echo "Alice -> Bob: send(\n  msg="Hello...",\n  length=8,\n)" | \
       diagon Sequence --new_lines_at_bsn=true
```

output:
```
┌─────┐          ┌───┐
│Alice│          │Bob│
└──┬──┘          └─┬─┘
   │               │
   │send(          │
   │  msg=Hello...,│
   │  length=8,    │
   │)              │
   │──────────────>│
┌──┴──┐          ┌─┴─┐
│Alice│          │Bob│
└─────┘          └───┘
```

* Add tests & refactor.

Co-authored-by: ArthurSonzogni <[email protected]>
  • Loading branch information
tg-m and ArthurSonzogni authored Jun 7, 2022
1 parent ccb028f commit 3a1dfe8
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/translator/sequence/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
#include "translator/antlr_error_listener.h"
#include "translator/sequence/Graph.hpp"

namespace {

void SplitString(std::wstring input,
const std::wstring& delimiter,
std::vector<std::wstring>* out) {
size_t start = 0U;
size_t end = input.find(delimiter);
while (end != std::wstring::npos) {
out->push_back(input.substr(start, end - start));
start = end + delimiter.length();
end = input.find(delimiter, start);
}
out->push_back(input.substr(start));
}

} // namespace

void Actor::Draw(Screen& screen, int height) {
screen.DrawBoxedText(left, 0, name);
screen.DrawVerticalLine(3, height - 4, center);
Expand Down Expand Up @@ -211,6 +228,17 @@ std::vector<Translator::OptionDescription> Sequence::Options() {
"Use the full unicode charset or only ASCII.",
Widget::Checkbox,
},
{
"interpret_backslash_n",
{
"false",
"true",
},
"true",
"Insert new lines at every occurence of '\\n' (backslash n) in the "
"message field.",
Widget::Checkbox,
},
};
}

Expand Down Expand Up @@ -254,13 +282,27 @@ std::string Sequence::Translate(const std::string& input,

auto options = SerializeOption(options_string);
ascii_only_ = (options["ascii_only"] == "true");
interpret_backslash_n_ = (options["interpret_backslash_n"] == "true");

ComputeInternalRepresentation(input);
UniformizeInternalRepresentation();
SplitByBackslashN();
Layout();
return Draw();
}

void Sequence::SplitByBackslashN() {
if (!interpret_backslash_n_) {
return;
}
for (auto& message : messages) {
std::vector<std::wstring> old = std::move(message.messages);
for(auto& it : old) {
SplitString(it, L"\\n", &message.messages);
}
}
}

void Sequence::ComputeInternalRepresentation(const std::string& input) {
antlr4::ANTLRInputStream input_stream(input);

Expand Down
3 changes: 3 additions & 0 deletions src/translator/sequence/Sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class Sequence : public Translator {
void UniformizeActors();
void UniformizeMessageID();

void SplitByBackslashN();

// 3) Layout.
void Layout();
void LayoutComputeMessageWidth();
Expand All @@ -125,4 +127,5 @@ class Sequence : public Translator {
std::map<int, int> message_index;

bool ascii_only_;
bool interpret_backslash_n_ = false;
};
3 changes: 3 additions & 0 deletions test/Sequence/multiline/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A -> B: This is a\nmultiline string.
A -> B: This is another\nmultiline string.
B -> A: This is the \nlast one.
18 changes: 18 additions & 0 deletions test/Sequence/multiline/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│This is a │
│multiline string.│
│────────────────>│
│ │
│This is another │
│multiline string.│
│────────────────>│
│ │
│ This is the │
│ last one. │
│<────────────────│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘
5 changes: 5 additions & 0 deletions test/Sequence/multiline_interleaved/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1) A -> B : message\nnumber\n1
2) A -> B : message\nnumber\n2

A: 1<2
B: 1>2
17 changes: 17 additions & 0 deletions test/Sequence/multiline_interleaved/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│──┐ │
│message│
│number │
│2 │ │
│──────>│
│ │ │
│message│
│number │
│1 │ │
│ └───>│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘
5 changes: 5 additions & 0 deletions test/Sequence/multiline_interleaved_mixed/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1) A -> B : message number 1
2) A -> B : message\nnumber\n2

A: 1<2
B: 1>2
15 changes: 15 additions & 0 deletions test/Sequence/multiline_interleaved_mixed/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│──┐ │
│ │ message │
│ │ number │
│ │ 2 │
│───────────────>│
│ │ │
│message number 1│
│ └────────────>│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘
5 changes: 5 additions & 0 deletions test/Sequence/multiline_interleaved_mixed_2/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1) A -> B : message\nnumber\n1
2) A -> B : message number 2

A: 1<2
B: 1>2
15 changes: 15 additions & 0 deletions test/Sequence/multiline_interleaved_mixed_2/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
┌─┐ ┌─┐
│A│ │B│
└┬┘ └┬┘
│ │
│──┐ │
│message number 2│
│───────────────>│
│ │ │
│ │ message │
│ │ number │
│ │ 1 │
│ └────────────>│
┌┴┐ ┌┴┐
│A│ │B│
└─┘ └─┘

0 comments on commit 3a1dfe8

Please sign in to comment.