diff --git a/src/backends/lua/luaexpression.cpp b/src/backends/lua/luaexpression.cpp --- a/src/backends/lua/luaexpression.cpp +++ b/src/backends/lua/luaexpression.cpp @@ -69,7 +69,6 @@ { output.replace(command(), QLatin1String("")); output.replace(QLatin1String("return"), QLatin1String("")); - output.replace(QLatin1String(">"), QLatin1String("")); output = output.trimmed(); qDebug() << "final output of the command " << command() << ": " << output << endl; diff --git a/src/backends/lua/luasession.h b/src/backends/lua/luasession.h --- a/src/backends/lua/luasession.h +++ b/src/backends/lua/luasession.h @@ -60,6 +60,9 @@ QProcess* m_process; LuaExpression* m_currentExpression; QString m_output; + QString m_tmpline; + QStringList m_commands; + bool m_isFirstReadedLine; }; #endif /* _LUASESSION_H */ diff --git a/src/backends/lua/luasession.cpp b/src/backends/lua/luasession.cpp --- a/src/backends/lua/luasession.cpp +++ b/src/backends/lua/luasession.cpp @@ -31,7 +31,8 @@ LuaSession::LuaSession( Cantor::Backend* backend) : Session(backend), m_process(0), - m_currentExpression(0) + m_currentExpression(0), + m_isFirstReadedLine(true) { } @@ -59,7 +60,7 @@ m_process->start(); m_process->waitForStarted(); m_process->waitForReadyRead(); - + // we need this for tab completion m_L = luaL_newstate(); luaL_openlibs(m_L); @@ -69,18 +70,18 @@ } void LuaSession::readIntroMessage() -{ +{ while(m_process->bytesAvailable()) { m_output.append(QString::fromLocal8Bit(m_process->readLine())); } - if(!m_output.isEmpty() && m_output.trimmed().endsWith(QLatin1String(">"))) { - qDebug() << " reading the intro message " << m_output ; + if(!m_output.isEmpty() && m_output.endsWith(QLatin1String("> "))) { + qDebug() << " reading the intro message " << m_output; m_output.clear(); disconnect(m_process, SIGNAL(readyReadStandardOutput()), this , SLOT(readIntroMessage())); - connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput())); - connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(readError())); + connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput())); + connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(readError())); } } @@ -88,20 +89,67 @@ { /* * parse the output - * clear all the garbage + * clear all the garbage, like our input commands, appearing in output (lua interpreter problem) * set it as output */ - // keep reading till the output ends with '>'. - // '>' marks the end of output for a particular command; + // We do it here, instead parseOutput(), because we don't know, when output ends + // without input commands counting + bool isReadLastPrompt = false; while(m_process->bytesAvailable()) { - m_output.append(QString::fromLocal8Bit(m_process->readLine())); + // work only with ended line (with '\n') + QString newLine =QString::fromLocal8Bit(m_process->readLine()); + if (!m_commands.empty() && !newLine.contains(QLatin1Char('\n'))) + { + m_tmpline += newLine; + return; + } + newLine = m_tmpline + newLine; + m_tmpline.clear(); + + if (m_commands.empty()) + isReadLastPrompt = newLine == QLatin1String("> "); + if (m_isFirstReadedLine) + { + if (m_commands.empty()) + { + if (!isReadLastPrompt) + m_output.append(newLine); + } + else + { + if (m_commands[0] == newLine || QLatin1String("> ") + m_commands[0] == newLine) + m_commands.removeFirst(); + else + m_output.append(newLine); + } + } + else + { + if (m_commands.empty()) + { + if (!isReadLastPrompt) + m_output.append(newLine); + } + else + { + bool isOurInputCommand = + QLatin1String("> ") + m_commands[0] == newLine || + QLatin1String(">> ") + m_commands[0] == newLine; + if (isOurInputCommand) + m_commands.removeFirst(); + else + m_output.append(newLine); + } + } + m_isFirstReadedLine = false; } - if(m_currentExpression && !m_output.isEmpty() && m_output.trimmed().endsWith(QLatin1String(">"))) { + if(m_currentExpression && m_commands.empty() && isReadLastPrompt) { // we have our complete output - // clean the output and parse it and clear m_output; + // clean the output and parse it and clear m_output and m_tmpline; m_currentExpression->parseOutput(m_output); m_output.clear(); - + m_tmpline.clear(); + m_isFirstReadedLine = true; } } @@ -114,6 +162,9 @@ { return; } + m_output.clear(); + m_tmpline.clear(); + m_isFirstReadedLine = true; m_currentExpression->parseError(error); } @@ -155,6 +206,10 @@ */ QString command = currentExpression->command(); + m_commands = command.split(QLatin1String("\n")); + for(auto i = 0; i < m_commands.size(); i++) + m_commands[i].append(QLatin1Char('\n')); + command += QLatin1String("\n"); qDebug() << "final command to be executed " << command << endl;