mirror of
https://github.com/ArthurSonzogni/Diagon.git
synced 2026-08-29 08:34:44 +08:00
Add support for new lines in Sequence (#40)
* 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 <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -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);
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -100,6 +100,8 @@ class Sequence : public Translator {
|
||||
void UniformizeActors();
|
||||
void UniformizeMessageID();
|
||||
|
||||
void SplitByBackslashN();
|
||||
|
||||
// 3) Layout.
|
||||
void Layout();
|
||||
void LayoutComputeMessageWidth();
|
||||
@@ -125,4 +127,5 @@ class Sequence : public Translator {
|
||||
std::map<int, int> message_index;
|
||||
|
||||
bool ascii_only_;
|
||||
bool interpret_backslash_n_ = false;
|
||||
};
|
||||
|
||||
@@ -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.
|
||||
@@ -0,0 +1,18 @@
|
||||
┌─┐ ┌─┐
|
||||
│A│ │B│
|
||||
└┬┘ └┬┘
|
||||
│ │
|
||||
│This is a │
|
||||
│multiline string.│
|
||||
│────────────────>│
|
||||
│ │
|
||||
│This is another │
|
||||
│multiline string.│
|
||||
│────────────────>│
|
||||
│ │
|
||||
│ This is the │
|
||||
│ last one. │
|
||||
│<────────────────│
|
||||
┌┴┐ ┌┴┐
|
||||
│A│ │B│
|
||||
└─┘ └─┘
|
||||
@@ -0,0 +1,5 @@
|
||||
1) A -> B : message\nnumber\n1
|
||||
2) A -> B : message\nnumber\n2
|
||||
|
||||
A: 1<2
|
||||
B: 1>2
|
||||
@@ -0,0 +1,17 @@
|
||||
┌─┐ ┌─┐
|
||||
│A│ │B│
|
||||
└┬┘ └┬┘
|
||||
│ │
|
||||
│──┐ │
|
||||
│message│
|
||||
│number │
|
||||
│2 │ │
|
||||
│──────>│
|
||||
│ │ │
|
||||
│message│
|
||||
│number │
|
||||
│1 │ │
|
||||
│ └───>│
|
||||
┌┴┐ ┌┴┐
|
||||
│A│ │B│
|
||||
└─┘ └─┘
|
||||
@@ -0,0 +1,5 @@
|
||||
1) A -> B : message number 1
|
||||
2) A -> B : message\nnumber\n2
|
||||
|
||||
A: 1<2
|
||||
B: 1>2
|
||||
@@ -0,0 +1,15 @@
|
||||
┌─┐ ┌─┐
|
||||
│A│ │B│
|
||||
└┬┘ └┬┘
|
||||
│ │
|
||||
│──┐ │
|
||||
│ │ message │
|
||||
│ │ number │
|
||||
│ │ 2 │
|
||||
│───────────────>│
|
||||
│ │ │
|
||||
│message number 1│
|
||||
│ └────────────>│
|
||||
┌┴┐ ┌┴┐
|
||||
│A│ │B│
|
||||
└─┘ └─┘
|
||||
@@ -0,0 +1,5 @@
|
||||
1) A -> B : message\nnumber\n1
|
||||
2) A -> B : message number 2
|
||||
|
||||
A: 1<2
|
||||
B: 1>2
|
||||
@@ -0,0 +1,15 @@
|
||||
┌─┐ ┌─┐
|
||||
│A│ │B│
|
||||
└┬┘ └┬┘
|
||||
│ │
|
||||
│──┐ │
|
||||
│message number 2│
|
||||
│───────────────>│
|
||||
│ │ │
|
||||
│ │ message │
|
||||
│ │ number │
|
||||
│ │ 1 │
|
||||
│ └────────────>│
|
||||
┌┴┐ ┌┴┐
|
||||
│A│ │B│
|
||||
└─┘ └─┘
|
||||
Reference in New Issue
Block a user