1 | // frontend_pyreadline.h
|
2 |
|
3 | #ifndef FRONTEND_PYREADLINE_H
|
4 | #define FRONTEND_PYREADLINE_H
|
5 |
|
6 | #include "mycpp/runtime.h"
|
7 |
|
8 | // hacky forward decl
|
9 | namespace completion {
|
10 | class ReadlineCallback;
|
11 | BigStr* ExecuteReadlineCallback(ReadlineCallback*, BigStr*, int);
|
12 | } // namespace completion
|
13 |
|
14 | // hacky forward decl
|
15 | namespace comp_ui {
|
16 | class _IDisplay;
|
17 | void ExecutePrintCandidates(_IDisplay*, BigStr*, List<BigStr*>*, int);
|
18 | } // namespace comp_ui
|
19 |
|
20 | namespace py_readline {
|
21 |
|
22 | class Readline {
|
23 | public:
|
24 | Readline();
|
25 | BigStr* prompt_input(BigStr* prompt);
|
26 | void parse_and_bind(BigStr* s);
|
27 | void add_history(BigStr* line);
|
28 | void read_history_file(BigStr* path);
|
29 | void write_history_file(BigStr* path);
|
30 | void set_completer(completion::ReadlineCallback* completer);
|
31 | void set_completer_delims(BigStr* delims);
|
32 | void set_completion_display_matches_hook(
|
33 | comp_ui::_IDisplay* display = nullptr);
|
34 | BigStr* get_line_buffer();
|
35 | int get_begidx();
|
36 | int get_endidx();
|
37 | void clear_history();
|
38 | BigStr* get_history_item(int pos);
|
39 | void remove_history_item(int pos);
|
40 | int get_current_history_length();
|
41 | void resize_terminal();
|
42 |
|
43 | static constexpr uint32_t field_mask() {
|
44 | return maskbit(offsetof(Readline, completer_delims_)) |
|
45 | maskbit(offsetof(Readline, completer_)) |
|
46 | maskbit(offsetof(Readline, display_));
|
47 | }
|
48 |
|
49 | static constexpr ObjHeader obj_header() {
|
50 | return ObjHeader::ClassFixed(field_mask(), sizeof(Readline));
|
51 | }
|
52 |
|
53 | int begidx_;
|
54 | int endidx_;
|
55 | BigStr* completer_delims_;
|
56 | completion::ReadlineCallback* completer_;
|
57 | comp_ui::_IDisplay* display_;
|
58 |
|
59 | // readline will set this to NULL when EOF is received, else this will point
|
60 | // to a line of input.
|
61 | char* latest_line_;
|
62 |
|
63 | // readline will set this flag when either:
|
64 | // - it receives EOF
|
65 | // - it has a complete line of input (it has seen "\n")
|
66 | bool ready_;
|
67 | };
|
68 |
|
69 | Readline* MaybeGetReadline();
|
70 |
|
71 | } // namespace py_readline
|
72 |
|
73 | #endif // FRONTEND_PYREADLINE_H
|