Debian Codegraph @

CodeLite/clConsoleBase.h

raw
1 #ifndef CLCONSOLEBASE_H
2 #define CLCONSOLEBASE_H
3
4 #include "cl_command_event.h"
5 #include "codelite_exports.h"
6 #include "macros.h"
7
8 #include <wx/arrstr.h>
9 #include <wx/process.h>
10 #include <wx/sharedptr.h>
11 #include <wx/string.h>
12
13 wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_CL, wxEVT_TERMINAL_EXIT, clProcessEvent);
14
15 class WXDLLIMPEXP_CL clConsoleEnvironment
16 {
17 wxStringMap_t m_environment;
18 wxStringMap_t m_oldEnvironment;
19
20 public:
21 clConsoleEnvironment(const wxStringMap_t& env);
22 clConsoleEnvironment();
23 ~clConsoleEnvironment();
24
25 void Add(const wxString& name, const wxString& value);
26 void Apply();
27 void UnApply();
28 };
29
30 class WXDLLIMPEXP_CL clConsoleBase
31 {
32 public:
33 typedef wxSharedPtr<clConsoleBase> Ptr_t;
34
35 protected:
36 wxStringMap_t m_environment;
37 wxString m_workingDirectory;
38 wxString m_command;
39 wxString m_commandArgs;
40 wxString m_tty;
41 wxString m_realPts;
42 long m_pid = wxNOT_FOUND;
43 bool m_waitWhenDone = false;
44 bool m_autoTerminate = false;
45 int m_execExtraFlags = 0;
46 bool m_terminalNeeded = true;
47 wxProcess* m_callback = nullptr;
48 wxEvtHandler* m_sink = nullptr;
49 wxString m_callbackUID;
50
51 protected:
52 /**
53 * @brief create an environment list to be used before we execute our terminal
54 */
55 wxString GetEnvironmentPrefix() const;
56
57 wxString WrapWithQuotesIfNeeded(const wxString& s) const;
58 wxString EscapeString(const wxString& str, const wxString& c = "\"") const;
59 virtual bool StartProcess(const wxString& command);
60
61 public:
62 clConsoleBase();
63 virtual ~clConsoleBase();
64
65 /**
66 * @brief when sink is provided, the terminal will send event when its done
67 * The event sent is wxEVT_TERMINAL_EXIT of type clProcessEvent
68 * @param sink
69 * @param if provided the uid is sent back along with the termination event
70 */
71 void SetSink(wxEvtHandler* sink, const wxString& uid = "")
72 {
73 this->m_sink = sink;
74 this->m_callbackUID = uid;
75 }
76
77 /**
78 * @brief split command line arguments taking double quotes and escaping into account
79 */
80 static wxArrayString SplitArguments(const wxString& args);
81
82 /**
83 * @brief add an environment variable to be applied before we start the terminal
84 */
85 void AddEnvVariable(const wxString& name, const wxString& value);
86
87 /**
88 * @brief start terminal with a given command and an optional working directory
89 */
90 virtual bool Start() = 0;
91
92 /**
93 * @brief start terminal for debugger (i.e. launch an empty terminal so we can obtain its tty and return it
94 */
95 virtual bool StartForDebugger() = 0;
96
97 /**
98 * @brief prepare the execution command
99 */
100 virtual wxString PrepareCommand() = 0;
101
102 /**
103 * @brief return the best terminal for the OS. Pass an empty string to return the default temrinal for the OS
104 */
105 static clConsoleBase::Ptr_t GetTerminal();
106
107 /**
108 * @brief return the default name for the OS
109 */
110 static wxString GetSelectedTerminalName();
111
112 /**
113 * @brief return list of known terminals
114 */
115 static wxArrayString GetAvailaleTerminals();
116
117 // Setters/Getters
118 void SetCommand(const wxString& command, const wxString& args)
119 {
120 this->m_command = command;
121 this->m_commandArgs = args;
122 }
123 void SetEnvironment(const wxStringMap_t& environment) { this->m_environment = environment; }
124 void SetEnvironment(const clEnvList_t& environment);
125 void SetPid(long pid) { this->m_pid = pid; }
126 void SetRealPts(const wxString& realPts) { this->m_realPts = realPts; }
127 void SetTty(const wxString& tty) { this->m_tty = tty; }
128 void SetWaitWhenDone(bool waitWhenDone) { this->m_waitWhenDone = waitWhenDone; }
129 void SetWorkingDirectory(const wxString& workingDirectory) { this->m_workingDirectory = workingDirectory; }
130 const wxString& GetCommand() const { return m_command; }
131 const wxString& GetCommandArgs() const { return m_commandArgs; }
132 const wxStringMap_t& GetEnvironment() const { return m_environment; }
133 long GetPid() const { return m_pid; }
134 const wxString& GetRealPts() const { return m_realPts; }
135 const wxString& GetTty() const { return m_tty; }
136 bool IsWaitWhenDone() const { return m_waitWhenDone; }
137 const wxString& GetWorkingDirectory() const { return m_workingDirectory; }
138 void SetExecExtraFlags(int execExtraFlags) { this->m_execExtraFlags = execExtraFlags; }
139 int GetExecExtraFlags() const { return m_execExtraFlags; }
140 void SetAutoTerminate(bool autoTerminate) { this->m_autoTerminate = autoTerminate; }
141 void SetTerminalNeeded(bool terminalNeeded) { this->m_terminalNeeded = terminalNeeded; }
142 bool IsAutoTerminate() const { return m_autoTerminate; }
143 bool IsTerminalNeeded() const { return m_terminalNeeded; }
144 void SetCallback(wxProcess* callback)
145 {
146 wxDELETE(m_callback);
147 this->m_callback = callback;
148 }
149 };
150
151 #endif // CLCONSOLEBASE_H