switch to modified cpp-linenoise, use it on windows

master
Daniel Kolesa 2021-03-31 22:42:41 +02:00
parent b9c74d86b5
commit 6068a7259b
5 changed files with 2453 additions and 1283 deletions

View File

@ -1,5 +1,4 @@
#ifdef CS_REPL_USE_LINENOISE
#ifndef _WIN32
#ifndef CS_REPL_HAS_EDIT
#define CS_REPL_HAS_EDIT
/* use the bundled linenoise library, default */
@ -14,7 +13,7 @@
static cs::state *ln_cs = nullptr;
inline void ln_complete(char const *buf, linenoiseCompletions *lc) {
inline void ln_complete(char const *buf, std::vector<std::string> &lc) {
std::string_view cmd = get_complete_cmd(buf);
for (auto id: ln_cs->get_idents()) {
if (!id->is_command()) {
@ -25,24 +24,22 @@ inline void ln_complete(char const *buf, linenoiseCompletions *lc) {
continue;
}
if (idname.substr(0, cmd.size()) == cmd) {
linenoiseAddCompletion(lc, idname.data());
lc.emplace_back(idname);
}
}
}
inline char *ln_hint(char const *buf, int *color, int *bold) {
inline std::string ln_hint(char const *buf, int &color, int &bold) {
cs::command *cmd = get_hint_cmd(*ln_cs, buf);
if (!cmd) {
return nullptr;
return std::string{};
}
std::string args = " [";
fill_cmd_args(args, cmd->get_args());
args += ']';
*color = 35;
*bold = 1;
char *ret = new char[args.size() + 1];
memcpy(ret, args.data(), args.size() + 1);
return ret;
color = 35;
bold = 1;
return args;
}
inline void ln_hint_free(void *hint) {
@ -51,16 +48,16 @@ inline void ln_hint_free(void *hint) {
inline void init_lineedit(cs::state &cs, std::string_view) {
/* sensible default history size */
linenoiseHistorySetMaxLen(1000);
linenoise::SetHistoryMaxLen(1000);
ln_cs = &cs;
linenoiseSetCompletionCallback(ln_complete);
linenoiseSetHintsCallback(ln_hint);
linenoiseSetFreeHintsCallback(ln_hint_free);
linenoise::SetCompletionCallback(ln_complete);
linenoise::SetHintsCallback(ln_hint);
}
inline std::optional<std::string> read_line(cs::state &, cs::string_var *pr) {
auto line = linenoise(pr->get_value().data());
if (!line) {
std::string line;
auto quit = linenoise::Readline(pr->get_value().data(), line);
if (quit) {
/* linenoise traps ctrl-c, detect it and let the user exit */
if (errno == EAGAIN) {
raise(SIGINT);
@ -68,16 +65,13 @@ inline std::optional<std::string> read_line(cs::state &, cs::string_var *pr) {
}
return std::string{};
}
std::string ret = line;
linenoiseFree(line);
return ret;
return line;
}
inline void add_history(cs::state &, std::string_view line) {
/* backed by std::string so it's terminated */
linenoiseHistoryAdd(line.data());
linenoise::AddHistory(line.data());
}
#endif
#endif
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -8,12 +8,9 @@ repl_flags = []
if get_option('readline').enabled()
use_readline = true
use_linenoise = false
elif get_option('linenoise').enabled()
elif not get_option('linenoise').disabled()
use_readline = false
use_linenoise = true
elif get_option('linenoise').auto()
use_readline = false
use_linenoise = (host_machine.system() != 'windows')
endif
if not get_option('repl').disabled()
@ -21,7 +18,6 @@ if not get_option('repl').disabled()
repl_deps += [dependency('readline')]
repl_flags = ['-DCS_REPL_USE_READLINE']
elif use_linenoise
repl_src += ['linenoise.cc']
repl_flags = ['-DCS_REPL_USE_LINENOISE']
endif
executable('cubescript',

View File

@ -1,6 +1,10 @@
/* avoid silly complaints about fopen */
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
/* avoid silly complaints about fopen */
# define _CRT_SECURE_NO_WARNINGS 1
/* work around clang bug with std::function (needed by linenoise) */
# if defined(__clang__) && !defined(_HAS_STATIC_RTTI)
# define _HAS_STATIC_RTTI 0
# endif
#endif
#include <signal.h>