From bfa2768e223456fc6d4754dfffa0ada8b88620e9 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Sun, 8 Sep 2024 17:05:31 -0400 Subject: added lua api functions for indexing history --- src/lua_api.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/lua_api.c') diff --git a/src/lua_api.c b/src/lua_api.c index 8314e2a..ac9bf30 100644 --- a/src/lua_api.c +++ b/src/lua_api.c @@ -248,6 +248,30 @@ static int l_is_writeable(lua_State *L) { return 1; } +static int l_last_history(lua_State *L) { + char *history_element = lush_get_past_command(0); + // remove the newline character + history_element[strlen(history_element) - 1] = '\0'; + lua_pushstring(L, history_element); + free(history_element); + return 1; +} + +static int l_get_history(lua_State *L) { + int i = luaL_checkinteger(L, 1); + + // using 1 based indexing to be aligned with Lua + if (i < 1) + return 0; + + char *history_element = lush_get_past_command(i - 1); + // remove the newline character + history_element[strlen(history_element) - 1] = '\0'; + lua_pushstring(L, history_element); + free(history_element); + return 1; +} + // -- register Lua functions -- void lua_register_api(lua_State *L) { @@ -272,6 +296,10 @@ void lua_register_api(lua_State *L) { lua_setfield(L, -2, "isReadable"); lua_pushcfunction(L, l_is_writeable); lua_setfield(L, -2, "isWriteable"); + lua_pushcfunction(L, l_last_history); + lua_setfield(L, -2, "lastHistory"); + lua_pushcfunction(L, l_get_history); + lua_setfield(L, -2, "getHistory"); // set the table as global lua_setglobal(L, "lush"); } -- cgit v1.2.3-59-g8ed1b ats/test/history_test.lua'>stats
path: root/test/history_test.lua (unfollow)
Commit message (Collapse)AuthorFilesLines
2024-09-13added better clarification to help menuGravatar BanceDev 1-1/+2
2024-09-13improved installation instructionsGravatar BanceDev 1-3/+3
2024-09-12v0.1.1Gravatar BanceDev 1-1/+1
2024-09-12fixed bug in input buffer handling due to misplaced printGravatar BanceDev 2-5/+8
2024-09-12Update build.yml checkout v4Gravatar Lance Borden 1-1/+1
2024-09-12Update build.yml to artifact v4Gravatar Lance Borden 1-1/+1
2024-09-12fixed exit status issue with non interative modeGravatar BanceDev 1-1/+1
2024-09-12added non interative mode for running lua scriptsGravatar BanceDev 2-4/+28
2024-09-12temporary github action fix until non-interactive mode is implementedGravatar BanceDev 1-2/+2
2024-09-12attempt to update build script to accept input into lush shellGravatar BanceDev 2-4/+6
2024-09-12prevent lush workflow from getting stuck in testsGravatar Lance Borden 1-1/+4
2024-09-12removed chsh in workflowGravatar Lance Borden 1-4/+1