簡體   English   中英

在QProcess輸出中保留ANSI轉義序列

[英]Keep ANSI Escape Sequences in QProcess Output

我正在創建一個程序,在其中啟用了C ++ 11的Ubuntu 16.04 Qt 5.5.1上使用QProcess框架在Qt中運行進程。 我將流程輸出流定向到QTextEdit。

我想將此輸出着色為使用與本機終端通過嵌入的ANSI轉義顏色序列解釋的相同的顏色。 但是,我無法解析轉義序列,因為QProcess輸出中似乎缺少這些轉義序列。 我本來以為QString會剝離它們,但是經過一些測試后,我認為情況並非如此。

如果可以將轉義序列保留在QProcess輸出中,則我發現了一些信息,可將我指向ANSI轉義顏色解釋方向。

這是我在Qt代碼中所做的示例項目。

源文件...

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QProcess>
#include <QStringList>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QStringList input = {"gcc will_not_build.c"};
    QProcess * proc = new QProcess();

    proc->setReadChannel(QProcess::StandardOutput);
    proc->setProcessChannelMode(QProcess::MergedChannels);
    proc->setWorkingDirectory("/path/to/test/c/file/");

    //Start bash
    proc->start("bash");
    proc->waitForStarted();

    // Write as many commands to this process as needed
    foreach(QString str, input){
        proc->write(str.toUtf8() + "\n");
        proc->waitForBytesWritten(-1);
    }

    // Let bash close gracefully
    proc->write("exit $?\n");
    proc->waitForBytesWritten(-1);

    proc->closeWriteChannel();
    proc->waitForFinished();
    proc->waitForReadyRead();

    QByteArray read_data = proc->readAll();

    // The use of tr(read_data) also works here.
    QString output = tr(read_data);//QString::fromStdString (read_data.toStdString ());

    proc->closeReadChannel(QProcess::StandardOutput);

    proc->close();
    delete proc;

    // Add the output to the text box
    ui->textEdit->append (output);
}

MainWindow::~MainWindow()
{
    delete ui;
}

頭文件...

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H 

表單文件...

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QTextEdit" name="textEdit">
    <property name="geometry">
     <rect>
      <x>33</x>
      <y>19</y>
      <width>331</width>
      <height>211</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>19</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

C源文件...

int main(){
    // Intentionally will not build
    I will not build :)
}

我的輸出如下所示:

QProcess gcc輸出

本機Linux終端的輸出如下所示:

Linux終端gcc輸出顏色

有誰知道如何在QProcess輸出中保留ANSI轉義顏色序列,以便我可以模擬Linux終端顏色?

附帶說明一下,我在Qt Creator源代碼中進行了挖掘,並且有一個類可以將ANSI轉義顏色轉換為Rich Text顏色,因此我知道有人在這方面。 同樣,在構建項目時,由於某種原因,Qt Creator不會在其自己的終端中為構建輸出着色。

多虧了對我的問題的深刻見解,我才能夠找到解決問題的方法。 我會分享...

QProcess也不是錯誤,也不是QString。 問題出在程序執行的環境中。 由於這些程序(gcc等)的輸出未連接到TTY設備,因此將剝離所有ANSI轉義序列。 有一種方法可以欺騙輸出,使其看起來好像已連接到TTY設備

只需在命令前添加unbuffer即可。

由於我實際上是在創建Qt Creator插件,因此我已經鏈接了許多Qt Creator源代碼。 碰巧的是,已經存在一個名為AnsiEscapeCodeHandler的方便的類,用於將ANSI轉義序列轉換為QTextCharFormat和相應的ANSI轉義序列剝離的字符串。

為了說明如何使用該類,但現在在我的示例中,我將從可下載的Qt Creator源代碼中將ansieescapecodehandler.hansiescapecodehandler.cpp復制到測試項目中。 我不得不從AnsiEscapeCodeHandler源文件中刪除幾行內容,以在其余Qt Creator源代碼的上下文之外進行編譯,僅此而已。

新的源文件...

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QProcess>
#include <QStringList>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QStringList input = {"unbuffer gcc will_not_build.c"};
    QProcess * proc = new QProcess();

    proc->setReadChannel(QProcess::StandardOutput);
    proc->setProcessChannelMode(QProcess::MergedChannels);
    proc->setWorkingDirectory("/path/to/test/c/file/");

    //Start bash
    proc->start("bash");
    proc->waitForStarted();

    // Write as many commands to this process as needed
    foreach(QString str, input){
        proc->write(str.toUtf8() + "\n");
        proc->waitForBytesWritten(-1);
    }

    // Let bash close gracefully
    proc->write("exit $?\n");
    proc->waitForBytesWritten(-1);

    proc->closeWriteChannel();
    proc->waitForFinished();
    proc->waitForReadyRead();

    QByteArray read_data = proc->readAll();

    // The use of tr(read_data) also works here.
    QString output = tr(read_data);//QString::fromStdString (read_data.toStdString ());

    proc->closeReadChannel(QProcess::StandardOutput);

    proc->close();
    delete proc;

    // Strip default character set escape sequences, since those seem to be left
    // See https://stackoverflow.com/questions/36279015/what-does-x1bb-do
    output.remove("\x1b(B", Qt::CaseInsensitive);

    // Since it is just one single text stream define here instead of globally
    Utils::AnsiEscapeCodeHandler ansi_handler;

    FormattedTextList result = ansi_handler.parseText (Utils::FormattedText(output, ui->textEdit->currentCharFormat ()));

    // Loop through the text/format results
    foreach(Utils::FormattedText ft, result){
        ui->textEdit->setCurrentCharFormat (ft.format);
        ui->textEdit->insertPlainText (ft.text);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

新的頭文件...

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

// This exists in the qtcreator-src code and handles ansi escape code color parsing
#include "ansiescapecodehandler.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    typedef QList<Utils::FormattedText> FormattedTextList;
};

#endif // MAINWINDOW_H

新的彩色輸出... QProcess gcc輸出

QProcess不會干擾進程的輸出,它只是gcc (與其他許多發出彩色輸出的程序一樣),默認情況下,僅當它檢測到正在TTY設備上進行寫入時,才會發出顏色轉義序列。

如果要禁用此啟發式方法並要求始終產生彩色輸出,則必須在編譯器命令行中添加-fdiagnostics-color=always選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM