-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.cpp
More file actions
165 lines (129 loc) · 5.06 KB
/
repl.cpp
File metadata and controls
165 lines (129 loc) · 5.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "repl.h"
#include <unistd.h>
#include <fstream>
#include <utility>
#include <numeric>
#include <filesystem>
#include "executor.h"
#include "Global.h"
#include "highlight.h"
#include "utils.h"
#include "replxx.hxx"
#include "completion.h"
using replxx::Replxx;
namespace repl {
const int MAX_HISTORY_DISPLAY_HINTS = 4;
const int HISTORY_LOAD_LIMIT_FOR_HINTS = 1000;
static std::string prompt() {
auto prompt_fun = g.functions.find("prompt_PS1");
if(prompt_fun != g.functions.end()) {
std::string output;
// Don't let prompt_PS1 modify $?
int last_return_value = g.last_return_value;
executor::subshell_capture_output(prompt_fun->second, output);
g.last_return_value = last_return_value;
return output;
} else {
std::error_code ec;
std::filesystem::path path = std::filesystem::current_path(ec);
if(ec) {
return " $ ";
}
return path.string() + " $ ";
}
}
static std::optional<std::string> get_history_path() {
if(std::optional<std::string> home = g.get_variable("HOME")) {
std::string path = home.value() + "/.kish_history";
return { path };
}
return {};
}
static std::string read_line(Replxx &replxx) {
char const * cinput { nullptr }; // should not be freed
do {
cinput = replxx.input(prompt());
} while(cinput == nullptr && errno == EAGAIN);
if (cinput == nullptr) {
exit(0);
}
return { cinput };
}
static std::vector<std::string> history_commands_starting_with(std::string_view str, std::size_t how_many, Replxx::HistoryScan scan) {
std::vector<std::string> history_commands;
int history_read_limit = HISTORY_LOAD_LIMIT_FOR_HINTS;
while(scan.next()) {
if(scan.get().text().starts_with(str)) {
history_commands.emplace_back(scan.get().text());
}
if(history_read_limit-- == 0)
break;
if(history_commands.size() >= how_many)
break;
}
return history_commands;
}
static std::vector<std::string> hint_callback(Replxx &replxx, std::string const &input, int &contextLen, Replxx::Color &) {
std::vector<std::string> history_commands = history_commands_starting_with(input, MAX_HISTORY_DISPLAY_HINTS, replxx.history_scan());
if(! history_commands.empty()) {
contextLen = utils::utf8_codepoint_len(input);
return history_commands;
}
return {};
}
static bool is_cursor_at_end_of_line(const Replxx::State &state) {
return state.cursor_position() == utils::utf8_codepoint_len(state.text());
}
static Replxx::ACTION_RESULT right_arrow_key_press(Replxx &replxx, char32_t code) {
if(is_cursor_at_end_of_line(replxx.get_state())) {
std::vector<std::string> history_commands = history_commands_starting_with(replxx.get_state().text(), MAX_HISTORY_DISPLAY_HINTS, replxx.history_scan());
if(! history_commands.empty()) {
std::string common_prefix = utils::common_prefix(history_commands);
if(replxx.get_state().cursor_position() == utils::utf8_codepoint_len(common_prefix)) {
// already completed or typed be the user to the common prefix - complete to the topmost pick
replxx.set_state(Replxx::State(history_commands.at(0).c_str(), std::numeric_limits<int>::max()));
} else {
// multiple valid point to complete - only complete to the common prefix
replxx.set_state(Replxx::State(common_prefix.c_str(), std::numeric_limits<int>::max()));
}
return Replxx::ACTION_RESULT::CONTINUE;
}
}
replxx.invoke(Replxx::ACTION::MOVE_CURSOR_RIGHT, code);
return Replxx::ACTION_RESULT::CONTINUE;
}
static void repl_loop(Replxx &replxx) {
std::optional<std::string> history_path = get_history_path();
if(history_path.has_value()) {
std::ifstream history_file(history_path.value().c_str());
replxx.history_load(history_file);
}
while(true) {
std::string input = read_line(replxx);
if(history_path.has_value()) {
if(!input.empty()) {
replxx.history_add(input);
}
replxx.history_sync(history_path.value());
}
executor::run_from_string(input);
}
}
void run() {
Replxx replxx;
replxx.install_window_change_handler();
replxx.set_no_color(false);
replxx.set_highlighter_callback(highlight::highlighter_callback);
replxx.set_completion_callback([&] (const std::string &input, int &contextLen) -> std::vector<Replxx::Completion> {
return completion::completion_callback(replxx, input, contextLen);
});
replxx.set_immediate_completion(true);
replxx.set_beep_on_ambiguous_completion(true);
replxx.set_max_hint_rows(MAX_HISTORY_DISPLAY_HINTS);
replxx.set_hint_callback(std::bind_front(hint_callback, std::ref(replxx)));
replxx.bind_key(Replxx::KEY::RIGHT, std::bind_front(right_arrow_key_press, std::ref(replxx)));
// TODO: does this improve typing latency?
//replxx.set_hint_delay(1);
repl_loop(replxx);
}
} // namespace repl