-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadline_helper.h
45 lines (34 loc) · 935 Bytes
/
readline_helper.h
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
#include <iostream>
#include <string>
#ifdef USE_GNU_READLINE
#include <readline/readline.h>
#include <readline/history.h>
inline bool readline_init() {
rl_initialize();
rl_bind_keyseq("\\e", rl_vi_editing_mode);
return true;
}
inline void readline_deinit() {
rl_clear_history();
}
inline bool nextline(const char *prompt, std::string &line) {
if (char *input = readline(prompt); input) {
line.assign(input);
free(input);
return true;
}
return false;
}
inline void add_history(const std::string &line) {
add_history(line.data());
}
#else // NO GNU READLINE
inline bool readline_init() { return true; }
inline void readline_deinit() { }
inline bool nextline(const char *prompt, std::string &line) {
std::cout << prompt;
std::cout.flush();
return static_cast<bool>(std::getline(std::cin, line));
}
inline void add_history(const std::string &line) { }
#endif