我一直在修改C ++中的Lua解释器以与其他库一起使用。 问题是,它充当终端,但是您不能像使用箭头键那样找到以前的命令等使用功能。
代码类似于:http://www.wesnoth.org/devdocs/lua_8cpp_source.html ,我认为函数 pushline(第259行)和loadline(第280行)存在问题。各个人的版权。
static int pushline (lua_State *L, int firstline) {
char buffer[LUA_MAXINPUT]; /* and using char affects the result as arrow UP keys is shown as ^[[A */
char *b = buffer;
size_t l;
const char *prmt = get_prompt(L, firstline);
int readstatus = lua_readline(L, b, prmt); /*readline does not accept arrow key but its characters */
lua_pop(L, 1);
if (readstatus == 0)
return 0;
l = strlen(b);
if (l > 0 && b[l-1] == '\n')
b[l-1] = '\0';
if (firstline && b[0] == '=')
lua_pushfstring(L, "return %s", b+1);
else
lua_pushstring(L, b);
lua_freeline(L, b);
return 1;
}
static int loadline (lua_State *L) {
int status;
lua_settop(L, 0);
if (!pushline(L, 1))
return -1;
for (;;) {
size_t l;
const char *line = lua_tolstring(L, 1, &l);
status = luaL_loadbuffer(L, line, l, "=stdin"); /* I think the error is using stdin */
if (!incomplete(L, status)) break;
if (!pushline(L, 0))
return -1;
lua_pushliteral(L, "\n");
lua_insert(L, -2);
lua_concat(L, 3);
}
lua_saveline(L, 1);
lua_remove(L, 1);
return status;
}
预期输出:
>> foo = 3 /*press enter after typing */
>> /* pressing arrow keys should show 'foo = 3' or pressing down key should show the current typed words */
实际输出:
>> foo = 3 /*press enter after typing */
>> /* pressing arrow keys should shows '^[[A' or pressing down key shows '^[[D' */
我不知道该怎么做。我当时在考虑使用getch或getchar,但这不起作用。 ncurses是个好主意吗?带有解释的编码答案将对我有很大帮助!另外,链接到文档到所使用的任何相关功能文档。