-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscr_python_console.h
More file actions
72 lines (58 loc) · 2.22 KB
/
Copy pathscr_python_console.h
File metadata and controls
72 lines (58 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include "ImGuiColorTextEdit/TextEditor.h"
#include <memory>
#include <string>
#include <vector>
class GUI;
/// One editable script file loaded from scripts/python (path, filename, TextEditor buffer).
struct Python_script_editor
{
std::string path;
std::string filename;
TextEditor editor;
};
/// Result of running one Python snippet (UI console or remote).
struct Python_exec_result
{
bool ok = true;
std::string output; // print / ezy.log lines captured for this request
std::string result; // repr of expression value when present
std::string error; // traceback / error text when ok is false
};
/// ImGui Python console: run Python snippets with bindings to EzyCad (ezy.*, view.*).
/// Available when built with EZYCAD_HAVE_PYTHON (native builds with Python development libraries).
class Python_console
{
public:
explicit Python_console(GUI* gui);
~Python_console();
void render(bool* p_open = nullptr);
static int text_edit_callback(struct ImGuiInputTextCallbackData* data);
static int log_display_resize_callback(struct ImGuiInputTextCallbackData* data);
void append_line_from_python(const std::string& line);
Python_exec_result execute_captured(const std::string& code);
bool is_python_ok() const { return m_python_ok; }
private:
void load_scripts();
void execute(const std::string& code);
void append_line(const std::string& line, bool is_error = false);
bool init_python_();
GUI* m_gui = nullptr;
#ifdef EZYCAD_HAVE_PYTHON
struct Ezycad_python_runtime;
std::unique_ptr<Ezycad_python_runtime> m_py_runtime;
#endif
std::vector<std::string> m_history;
std::vector<std::string> m_cmd_history;
int m_cmd_history_pos = -1;
static constexpr int k_input_buf_size = 1024;
char m_input_buf[k_input_buf_size]{};
bool m_scroll_to_bottom = false;
bool m_python_ok = false;
bool m_capturing = false;
std::string m_capture_buf;
std::string m_log_display_buf;
uint64_t m_log_display_version = 0;
uint64_t m_log_display_built_version = 0;
std::vector<Python_script_editor> m_script_editors;
};