1 /*
2     SPDX-FileCopyrightText: 2003 John Birch <jbb@kdevelop.org>
3     SPDX-FileCopyrightText: 2007 Hamish Rodda <rodda@kde.org>
4     SPDX-FileCopyrightText: 2016 Aetf <aetf@unlimitedcodeworks.xyz>
5 
6     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
7 */
8 
9 #ifndef DEBUGGERCONSOLEVIEW_H
10 #define DEBUGGERCONSOLEVIEW_H
11 
12 #include <QWidget>
13 #include <QStringList>
14 #include <QTimer>
15 
16 #include "dbgglobal.h"
17 
18 class QTextEdit;
19 class QToolBar;
20 class KHistoryComboBox;
21 
22 namespace KDevelop {
23 class IDebugSession;
24 }
25 
26 namespace KDevMI {
27 class MIDebuggerPlugin;
28 
29 /**
30  * @brief A debugger console gives the user direct access to the debugger command line interface.
31  */
32 class DebuggerConsoleView : public QWidget
33 {
34     Q_OBJECT
35 public:
36     explicit DebuggerConsoleView(MIDebuggerPlugin *plugin, QWidget *parent = nullptr);
37     ~DebuggerConsoleView() override;
38 
39     /**
40      * Whether show a button allowing user to interrupt debugger execution.
41      */
42     void setShowInterrupt(bool enable);
43 
44     /**
45      * If set to a nonempty string, the default "(gdb)" prompt will be replaced.
46      * This only affects output lines after the call.
47      */
48     void setReplacePrompt(const QString &prompt);
49 
50     void setShowInternalCommands(bool enable);
51 
52 Q_SIGNALS:
53     void requestRaise();
54     /**
55      * Proxy signals for DebugSession
56      */
57     void interruptDebugger();
58     void sendCommand(const QString &cmd);
59 
60 
61 protected:
62     void setupUi();
63     void setupToolBar();
64 
65     /**
66      * Arranges for 'line' to be shown to the user.
67      * Adds 'line' to m_pendingOutput and makes sure
68      * m_updateTimer is running.
69      */
70     void appendLine(const QString &line);
71     void updateColors();
72 
73     /**
74      * escape html meta characters and handle line break
75      */
76     QString toHtmlEscaped(QString text);
77 
78     QString colorify(QString text, const QColor &color);
79 
80     /**
81      * Makes 'l' no longer than 'max_size' by
82      * removing excessive elements from the top.
83      */
84     void trimList(QStringList& l, int max_size);
85 
86     void changeEvent(QEvent *e) override;
87     void focusInEvent(QFocusEvent *e) override;
88 
89 protected Q_SLOTS:
90     void showContextMenu(const QPoint &pos);
91     void toggleRepeat(bool checked);
92     void toggleShowInternalCommands(bool checked);
93     void flushPending();
94     void clear();
95 
96     void handleSessionChanged(KDevelop::IDebugSession *session);
97     void handleDebuggerStateChange(DBGStateFlags oldStatus, DBGStateFlags newStatus);
98     void receivedInternalCommandStdout(const QString &line);
99     void receivedUserCommandStdout(const QString &line);
100     void receivedStdout(const QString &line, bool internal);
101     void receivedStderr(const QString &line);
102 
103     void trySendCommand(QString cmd);
104 private:
105     QAction *m_actRepeat;
106     QAction *m_actInterrupt;
107     QAction *m_actShowInternal;
108     QAction *m_actCmdEditor;
109 
110     QTextEdit *m_textView;
111     QToolBar *m_toolBar;
112     KHistoryComboBox *m_cmdEditor;
113 
114     bool m_repeatLastCommand;
115     bool m_showInternalCommands;
116     bool m_cmdEditorHadFocus;
117 
118     /**
119      * The output from user commands only and from all
120      * commands. We keep it here so that if we switch
121      * "Show internal commands" on, we can show previous
122      * internal commands.
123      */
124     QStringList m_allOutput;
125     QStringList m_userOutput;
126 
127     /**
128      * For performance reasons, we don't immediately add new text
129      * to QTExtEdit. Instead we add it to m_pendingOutput and
130      * flush it on timer.
131      */
132     QString m_pendingOutput;
133     QTimer m_updateTimer;
134 
135     QColor m_stdColor;
136     QColor m_errorColor;
137 
138     int m_maxLines;
139 
140     QString m_alterPrompt;
141 };
142 
143 } // end of namespace KDevMI
144 
145 #endif // DEBUGGERCONSOLEVIEW_H
146