Commit 208aeb79 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh
Browse files

Introduce Utils::SynchronousProcess::normalizeNewlines



Replaces \r\n? with \n.

Some console applications (e.g. git-push, git-rebase) use \r alone to
move the cursor to the line's beginning. This should be replaced by \n
rather than just be erased.

Change-Id: I8d614d2b471e59decdbfa7f173ffa7fbdb11759b
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent 1b13122c
......@@ -476,8 +476,8 @@ void SynchronousProcess::stdErrReady()
QString SynchronousProcess::convertOutput(const QByteArray &ba) const
{
QString output = d->m_codec ? d->m_codec->toUnicode(ba) : QString::fromLocal8Bit(ba.constData(), ba.size());
return output.remove(QLatin1Char('\r'));
return normalizeNewlines(d->m_codec ? d->m_codec->toUnicode(ba)
: QString::fromLocal8Bit(ba.constData(), ba.size()));
}
void SynchronousProcess::processStdOut(bool emitSignals)
......@@ -676,6 +676,25 @@ QString SynchronousProcess::locateBinary(const QString &path, const QString &bin
return QString();
}
QString SynchronousProcess::normalizeNewlines(const QString &text)
{
const QChar cr(QLatin1Char('\r'));
const QChar lf(QLatin1Char('\n'));
QString res;
res.reserve(text.size());
for (int i = 0, count = text.size(); i < count; ++i) {
const QChar c = text.at(i);
if (c == cr) {
res += lf;
if (i + 1 < count && text.at(i + 1) == lf)
++i;
} else {
res += c;
}
}
return res;
}
QString SynchronousProcess::locateBinary(const QString &binary)
{
const QByteArray path = qgetenv("PATH");
......
......@@ -138,6 +138,8 @@ public:
static QString locateBinary(const QString &binary);
static QString locateBinary(const QString &path, const QString &binary);
static QString normalizeNewlines(const QString &text);
signals:
void stdOut(const QByteArray &data, bool firstTime);
void stdErr(const QByteArray &data, bool firstTime);
......
......@@ -33,6 +33,8 @@
#include "coreconstants.h"
#include "icore.h"
#include <utils/synchronousprocess.h>
#include <QAction>
#include <QScrollBar>
......@@ -200,8 +202,7 @@ void OutputWindow::setMaxLineCount(int count)
void OutputWindow::appendMessage(const QString &output, OutputFormat format)
{
QString out = output;
out.remove(QLatin1Char('\r'));
const QString out = Utils::SynchronousProcess::normalizeNewlines(output);
setMaximumBlockCount(m_maxLineCount);
const bool atBottom = isScrollbarAtBottom();
......@@ -251,8 +252,7 @@ void OutputWindow::appendMessage(const QString &output, OutputFormat format)
// TODO rename
void OutputWindow::appendText(const QString &textIn, const QTextCharFormat &format)
{
QString text = textIn;
text.remove(QLatin1Char('\r'));
const QString text = Utils::SynchronousProcess::normalizeNewlines(textIn);
if (m_maxLineCount > 0 && document()->blockCount() >= m_maxLineCount)
return;
const bool atBottom = isScrollbarAtBottom();
......
......@@ -744,9 +744,7 @@ Core::IEditor *locateEditor(const char *property, const QString &entry)
// Return converted command output, remove '\r' read on Windows
static inline QString commandOutputFromLocal8Bit(const QByteArray &a)
{
QString output = QString::fromLocal8Bit(a);
output.remove(QLatin1Char('\r'));
return output;
return Utils::SynchronousProcess::normalizeNewlines(QString::fromLocal8Bit(a));
}
// Return converted command output split into lines
......@@ -3398,7 +3396,7 @@ QString GitClient::readConfig(const QString &workingDirectory, const QStringList
VcsBasePlugin::SuppressCommandLogging))
return QString();
if (Utils::HostOsInfo::isWindowsHost())
return QString::fromUtf8(outputText).remove(QLatin1Char('\r'));
return Utils::SynchronousProcess::normalizeNewlines(QString::fromUtf8(outputText));
return commandOutputFromLocal8Bit(outputText);
}
......
......@@ -187,8 +187,8 @@ QStringList MercurialClient::parentRevisionsSync(const QString &workingDirectory
QByteArray outputData;
if (!vcsFullySynchronousExec(workingDirectory, args, &outputData))
return QStringList();
QString output = QString::fromLocal8Bit(outputData);
output.remove(QLatin1Char('\r'));
const QString output = Utils::SynchronousProcess::normalizeNewlines(
QString::fromLocal8Bit(outputData));
/* Looks like: \code
changeset: 0:031a48610fba
user: ...
......@@ -230,8 +230,7 @@ QString MercurialClient::shortDescriptionSync(const QString &workingDirectory,
QByteArray outputData;
if (!vcsFullySynchronousExec(workingDirectory, args, &outputData))
return revision;
description = QString::fromLocal8Bit(outputData);
description.remove(QLatin1Char('\r'));
description = Utils::SynchronousProcess::normalizeNewlines(QString::fromLocal8Bit(outputData));
if (description.endsWith(QLatin1Char('\n')))
description.truncate(description.size() - 1);
return description;
......
......@@ -253,15 +253,14 @@ void Command::run()
exitCode = process->exitCode();
}
QString stdOutS = d->m_codec ? d->m_codec->toUnicode(stdOut)
: QString::fromLocal8Bit(stdOut.constData(), stdOut.size());
stdOutS.remove(QLatin1Char('\r'));
d->m_lastExecSuccess = ok;
d->m_lastExecExitCode = exitCode;
if (ok)
emit output(stdOutS);
if (ok) {
emit output(Utils::SynchronousProcess::normalizeNewlines(
d->m_codec ? d->m_codec->toUnicode(stdOut)
: QString::fromLocal8Bit(stdOut.constData(), stdOut.size())));
}
if (!error.isEmpty())
emit errorText(error);
......
......@@ -189,9 +189,8 @@ bool VcsBaseClient::synchronousCreateRepository(const QString &workingDirectory,
QByteArray outputData;
if (!vcsFullySynchronousExec(workingDirectory, args, &outputData))
return false;
QString output = QString::fromLocal8Bit(outputData);
output.remove(QLatin1Char('\r'));
::vcsOutputWindow()->append(output);
::vcsOutputWindow()->append(
Utils::SynchronousProcess::normalizeNewlines(QString::fromLocal8Bit(outputData)));
resetCachedVcsInfo(workingDirectory);
......
......@@ -863,15 +863,15 @@ static SynchronousProcessResponse runVcsFullySynchronously(const QString &workin
&stdOut, &stdErr, true);
if (!stdErr.isEmpty()) {
response.stdErr = (outputCodec ? outputCodec->toUnicode(stdErr) : QString::fromLocal8Bit(stdErr));
response.stdErr.remove(QLatin1Char('\r'));
response.stdErr = Utils::SynchronousProcess::normalizeNewlines(
outputCodec ? outputCodec->toUnicode(stdErr) : QString::fromLocal8Bit(stdErr));
if (!(flags & VcsBasePlugin::SuppressStdErrInLogWindow))
outputWindow->append(response.stdErr);
}
if (!stdOut.isEmpty()) {
response.stdOut = (outputCodec ? outputCodec->toUnicode(stdOut) : QString::fromLocal8Bit(stdOut));
response.stdOut.remove(QLatin1Char('\r'));
response.stdOut = Utils::SynchronousProcess::normalizeNewlines(
outputCodec ? outputCodec->toUnicode(stdOut) : QString::fromLocal8Bit(stdOut));
if (flags & VcsBasePlugin::ShowStdOutInLogWindow) {
if (flags & VcsBasePlugin::SilentOutput)
outputWindow->appendSilently(response.stdOut);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment