1 #ifndef CONSOLEWIDGET_H
2 #define CONSOLEWIDGET_H
3 
4 #include "core/MainWindow.h"
5 #include "CutterDockWidget.h"
6 #include "common/CommandTask.h"
7 #include "common/DirectionalComboBox.h"
8 
9 #include <QStringListModel>
10 #include <QSocketNotifier>
11 #include <QLocalSocket>
12 
13 #include <memory>
14 
15 class QCompleter;
16 class QShortcut;
17 
18 namespace Ui {
19 class ConsoleWidget;
20 }
21 
22 class ConsoleWidget : public CutterDockWidget
23 {
24     Q_OBJECT
25 
26 public:
27     explicit ConsoleWidget(MainWindow *main);
28 
29     ~ConsoleWidget();
30 
setDebugOutputEnabled(bool enabled)31     void setDebugOutputEnabled(bool enabled)
32     {
33         debugOutputEnabled = enabled;
34     }
35 
setMaxHistoryEntries(int max)36     void setMaxHistoryEntries(int max)
37     {
38         maxHistoryEntries = max;
39     }
40 
41 protected:
42     bool eventFilter(QObject *obj, QEvent *event) override;
43     QWidget* widgetToFocusOnRaise() override;
44 
45 public slots:
46     void focusInputLineEdit();
47 
48     void addOutput(const QString &msg);
49     void addDebugOutput(const QString &msg);
50 
51 private slots:
52     void setupFont();
53 
54     void on_r2InputLineEdit_returnPressed();
55     void on_debugeeInputLineEdit_returnPressed();
56     void onIndexChange();
57 
58     void on_execButton_clicked();
59 
60     void showCustomContextMenu(const QPoint &pt);
61 
62     void historyNext();
63     void historyPrev();
64 
65     void triggerCompletion();
66     void disableCompletion();
67     void updateCompletion();
68 
69     void clear();
70 
71     /**
72      * @brief Passes redirected output from the pipe to the terminal and console
73      */
74     void processQueuedOutput();
75 
76 private:
77     void scrollOutputToEnd();
78     void historyAdd(const QString &input);
79     void invalidateHistoryPosition();
80     void removeLastLine();
81     void executeCommand(const QString &command);
82     void sendToStdin(const QString &input);
83     void setWrap(bool wrap);
84 
85     /**
86      * @brief Redirects stderr and stdout to the output pipe which is handled by
87      *        processQueuedOutput
88      */
89     void redirectOutput();
90 
91     QSharedPointer<CommandTask> commandTask;
92 
93     std::unique_ptr<Ui::ConsoleWidget> ui;
94     QAction *actionWrapLines;
95     QList<QAction *> actions;
96     bool debugOutputEnabled;
97     int maxHistoryEntries;
98     int lastHistoryPosition;
99     QStringList history;
100     bool completionActive;
101     QStringListModel completionModel;
102     QCompleter *completer;
103     QShortcut *historyUpShortcut;
104     QShortcut *historyDownShortcut;
105     FILE *origStderr = nullptr;
106     FILE *origStdout = nullptr;
107     FILE *origStdin = nullptr;
108     QLocalSocket *pipeSocket  = nullptr;
109 #ifdef Q_OS_WIN
110     HANDLE hRead;
111     HANDLE hWrite;
112 #else
113     int redirectPipeFds[2];
114     int stdinFile = -1;
115     QString stdinFifoPath;
116     QVector<char> *redirectionBuffer;
117 #endif
118 };
119 
120 #endif // CONSOLEWIDGET_H
121