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

Add support for new lines in Sequence #40

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
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│
└─┘ └─┘