Skip to content

Commit

Permalink
Fixed issues #26 and #29
Browse files Browse the repository at this point in the history
  • Loading branch information
avighnac committed Jun 19, 2023
1 parent 7e1e232 commit c4d3e79
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ cmake_minimum_required(VERSION 3.0)
project(arithmetica-tui)

option(BUILD_RELEASE "Build for a GitHub release" ON)
option(BUILD_MAIN_EXECUTABLE "Build the main executable" ON)
option(BUILD_MAIN_EXECUTABLE "Build the main executable" OFF)
option(USE_SUBMODULE_AM_BMO "Fetch arithmetica and basic_math_operations using FetchContent" ON)
option(ARITHMETICA_TUI_TESTS "Build tests" OFF)
option(ARITHMETICA_TUI_TESTS "Build tests" ON)

if(WIN32 OR USE_SUBMODULE_AM_BMO)
# First fetch the repositories
Expand Down
17 changes: 15 additions & 2 deletions src/arithmetica_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ void print_eval_expression(std::string expression, int outputType, int padding,

int bracket_count = 0;

bool negative_sign_at_index_0 = expression.length() > 0 && expression[0] == '-';

for (size_t i = 0; i < expression.length(); ++i) {
if (expression[i] == ' ')
continue;
Expand All @@ -388,7 +390,9 @@ void print_eval_expression(std::string expression, int outputType, int padding,
long operational_sign = eval_with_steps::find_operational_sign(
expression.c_str(), expression[i]);
if (operational_sign != i) {
continue;
if (i != 0) {
continue;
}
}
}
if (expression[i] == '+') {
Expand All @@ -415,7 +419,7 @@ void print_eval_expression(std::string expression, int outputType, int padding,
free(leftArgument);
free(rightArgument);

if (!signs.empty() && signs.back() == "-") {
if (((!signs.empty() && !negative_sign_at_index_0) || (negative_sign_at_index_0 && signs.size() > 1)) && signs.back() == "-") {
left = left.substr(1, left.length());
}

Expand Down Expand Up @@ -744,6 +748,7 @@ int arithmetica_tui(int argc, char **argv) {
}

bool no_introduction = false;
bool reprint_input = false;

if (argc >= 2) {
if (std::string(argv[1]) == "--version") {
Expand Down Expand Up @@ -791,6 +796,10 @@ int arithmetica_tui(int argc, char **argv) {
no_introduction = true;
continue;
}
if (std::string(argv[i]) == "--reprint-input") {
reprint_input = true;
continue;
}
if (std::string(argv[i]) == "-o") {
if (argc < i + 1) {
std::cout << "Usage: arithmetica -o [file]\n";
Expand Down Expand Up @@ -948,6 +957,10 @@ int arithmetica_tui(int argc, char **argv) {
// by default Which is amazing, for once windows is better than linux Thank
// you Microsoft
std::getline(std::cin, input);

if (reprint_input) {
std::cout << "\rarithmetica> " << input << "\n";
}
#endif

#ifdef __linux__
Expand Down
60 changes: 42 additions & 18 deletions src/call_arithmetica_tui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,77 @@ void insert_characters_into_line(std::string &line, std::string chars,
}

std::string shorten_console_output(std::string str) {
std::string answer;
std::string current_line;
std::vector<std::string> answer = {""};

int cursor_location = 0;
size_t line_index = 0;

for (auto &i : str) {
if (current_line.length() >= 2 &&
current_line.substr(current_line.length() - 2, 2) == "\\r") {
if (answer[line_index].length() >= 2 &&
answer[line_index].substr(answer[line_index].length() - 2, 2) == "\\r") {
cursor_location = 0;
current_line = current_line.substr(0, current_line.length() - 2);
answer[line_index] = answer[line_index].substr(0, answer[line_index].length() - 2);
}
if (current_line.length() >= 7 &&
current_line.substr(current_line.length() - 7, 7) == "\\033[2K") {
current_line.clear();
if (answer[line_index].length() >= 7 &&
answer[line_index].substr(answer[line_index].length() - 7, 7) == "\\033[2K") {
answer[line_index].clear();
}
if (answer[line_index].length() >= 6 &&
answer[line_index].substr(answer[line_index].length() - 6, 6) == "\\033[A") {
answer[line_index] = answer[line_index].substr(0, answer[line_index].length() - 6);
if (line_index > 0) {
--line_index;
}
}

if (i == '\033') {
insert_characters_into_line(current_line, "\\033", cursor_location);
insert_characters_into_line(answer[line_index], "\\033", cursor_location);
continue;
}
if (i == '\r') {
insert_characters_into_line(current_line, "\\r", cursor_location);
insert_characters_into_line(answer[line_index], "\\r", cursor_location);
continue;
}
if (i == '\n') {
answer += current_line + "\\n";
current_line.clear();
if (line_index == answer.size() - 1)
answer.push_back("");
cursor_location = 0;
++line_index;
continue;
}

insert_characters_into_line(current_line, std::string(1, i),
insert_characters_into_line(answer[line_index], std::string(1, i),
cursor_location);
}

return answer;
std::string answer_str;
for (size_t i = 0; i < answer.size(); ++i) {
answer_str += answer[i];
if (i != answer.size() - 1) {
answer_str += "\\n";
}
}
return answer_str;
}

std::string call_arithmetica_tui(std::string command) {
#ifdef _WIN32
return "Not implemented.";
#else
#include <io.h>
#include <fcntl.h>
#endif

std::string call_arithmetica_tui(std::string command) {
command += "\nexit\n";

std::stringstream s;
auto buf = std::cout.rdbuf();
std::cout.rdbuf(s.rdbuf());

int pipefd[2];
#ifdef _WIN32
if (_pipe(pipefd, 4096, O_BINARY) == -1) {
#else
if (pipe(pipefd) == -1) {
#endif
perror("pipe");
return nullptr;
}
Expand Down Expand Up @@ -103,6 +124,10 @@ std::string call_arithmetica_tui(std::string command) {

std::vector<std::string> argv_vec = {"arithmetica", "--no-introduction"};

#ifdef _WIN32
argv_vec.push_back("--reprint-input");
#endif

std::vector<char *> argv_cstr;
for (const auto &arg : argv_vec) {
argv_cstr.push_back(const_cast<char *>(arg.c_str()));
Expand All @@ -126,5 +151,4 @@ std::string call_arithmetica_tui(std::string command) {
test = shorten_console_output(test);

return test;
#endif
}
10 changes: 5 additions & 5 deletions tests/eval_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST(EvalTests, Test2) {
result = call_arithmetica_tui("eval 120-256+100");
ASSERT_EQ(result, "arithmetica> eval 120-256+100\\n \\n-36\\n \\narithmetica> exit\\n");
result = call_arithmetica_tui("showsteps\neval 120-256+100");
ASSERT_EQ(result, "arithmetica> showsteps\\nshowsteps is now true\\narithmetica> eval 120-256+100\\n\\n==> 120 - 256 + 100\\n==> 120 - 156\\n==> -36\\n\\narithmetica> exit\\n");
ASSERT_EQ(result, "arithmetica> showsteps\\nshowsteps is now true\\narithmetica> eval 120-256+100\\n\\n==> 120 - 256 + 100\\n==> 120 - 156\\n==> - 36\\n\\narithmetica> exit\\n");
}

TEST(EvalTests, Test3) {
Expand Down Expand Up @@ -72,7 +72,7 @@ TEST(EvalTests, Test9) {
result = call_arithmetica_tui("eval (10-15)^2");
ASSERT_EQ(result, "arithmetica> eval (10-15)^2\\n \\n25\\n \\narithmetica> exit\\n");
result = call_arithmetica_tui("showsteps\neval (10-15)^2");
ASSERT_EQ(result, "arithmetica> showsteps\\nshowsteps is now true\\narithmetica> eval (10-15)^2\\n\\n==> (10 - 15)^2\\n ==> 10 - 15\\n ==> -5\\n==> (-5)^2\\n==> 25\\n\\narithmetica> exit\\n");
ASSERT_EQ(result, "arithmetica> showsteps\\nshowsteps is now true\\narithmetica> eval (10-15)^2\\n\\n==> (10 - 15)^2\\n ==> 10 - 15\\n ==> - 5\\n==> ( - 5)^2\\n==> 25\\n\\narithmetica> exit\\n");
}

TEST(EvalTests, Test10) {
Expand Down Expand Up @@ -128,13 +128,13 @@ TEST(EvalTests, Test16) {
result = call_arithmetica_tui("eval -(1)^(1/1)");
ASSERT_EQ(result, "arithmetica> eval -(1)^(1/1)\\n \\n-1\\n \\narithmetica> exit\\n");
result = call_arithmetica_tui("showsteps\neval -(1)^(1/1)");
ASSERT_EQ(result, "this test fails");
ASSERT_EQ(result, "arithmetica> showsteps\\nshowsteps is now true\\narithmetica> eval -(1)^(1/1)\\n\\n==> - (1)^(1/1)\\n 1 \\n ==> -\\n 1 \\n ==> 1\\n==> - (1)^(1)\\n==> - 1\\n\\narithmetica> exit\\n");
}

TEST(EvalTests, Test) {
TEST(EvalTests, Test17) {
std::string result;
result = call_arithmetica_tui("eval -1^(1/1)");
ASSERT_EQ(result, "arithmetica> eval -1^(1/1)\\n \\n-1\\n \\narithmetica> exit\\n");
result = call_arithmetica_tui("showsteps\neval -1^(1/1)");
ASSERT_EQ(result, "this test fails");
ASSERT_EQ(result, "arithmetica> showsteps\\nshowsteps is now true\\narithmetica> eval -1^(1/1)\\n\\n 1 \\n==> - 1^(-)\\n 1 \\n 1 \\n ==> -\\n 1 \\n ==> 1\\n==> - 1^(1)\\n==> - 1\\n\\narithmetica> exit\\n");
}

0 comments on commit c4d3e79

Please sign in to comment.