Ok, I thought I should clean the code up a little bit, and maybe add a couple of functions to return the history length ln.historylen() and to print the history ln.print() or ln.history().


3rdparty/lua-linenoise/linenoise.c
Code

static int l_gethistorytable(lua_State *L)
{
    int history_len;
    char ** history = linenoiseHistory(&history_len);

    lua_newtable(L); /* put a table on the stack */

    for (int i=0; i<history_len; i++){
        /* create result table */
        lua_pushnumber(L, i+1);  /* push key */
        lua_pushstring(L, history[i]);  /* push value */
        lua_settable(L, -3);  /* adds key and value to table at position 3 from top of stack */
      }
      return 1;  /* table is on top of stack */
}

static int l_historylen(lua_State *L)
{
     int history_len;
     char ** history = linenoiseHistory(&history_len);
     history = history;  // compiler warning about unused variable
     lua_pushinteger(L, history_len);
     return 1;
}

static int l_historygetmaxlen(lua_State *L)
{
      lua_pushinteger(L, linenoiseHistoryGetMaxLen());
      return 1;
}

static int l_historyprint(lua_State *L)
{
    int history_len;
    char ** history = linenoiseHistory(&history_len);

    printf("print history: %d entries\n",history_len);
    for (int i=0; i<history_len; i++){
       printf("%d %s\n",i+1,history[i]);
      }
    return handle_ln_ok(L);
}


luaL_Reg linenoise_funcs[] = {
    { "linenoise", l_linenoise },
    { "historyadd", l_historyadd },
    { "historysetmaxlen", l_historysetmaxlen },
    { "historysave", l_historysave },
    { "historyload", l_historyload },
    { "clearscreen", l_clearscreen },
    { "setcompletion", l_setcompletion},
    { "addcompletion", l_addcompletion },
    { "preload", l_preloadbuffer },
    { "refresh", l_refresh },

    /* Aliases for more consistent function names */
    { "addhistory", l_historyadd },
    { "sethistorymaxlen", l_historysetmaxlen },
    { "savehistory", l_historysave },
    { "loadhistory", l_historyload },

    { "line", l_linenoise },
    { "lines", l_lines },

    { "gethistorytable", l_gethistorytable},
    { "historygetmaxlen", l_historygetmaxlen},
    { "historylen", l_historylen},
    { "print", l_historyprint},
    { "history", l_historyprint},
    { NULL, NULL }
};

What's kind of interesting is that if you set the history to n, the maximum length is actually n-1.

So for ln.historysetmaxlen(5), the length of the history is actually a maximum of 4 entries.


And if you remove this line from plugins/console/init.lua it will default to a maximum length of 100.

Code
ln.historysetmaxlen(10)
since 3rdparty/linenoise/linenoise.c has this:
Code
#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100

And if you like to make ln a global variable ln for access you can do this in plugins/console/init.lua

Code
  
local ln = require("linenoise")
_G.ln = ln  -- make a global ln for console access