aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.lush/scripts/example.lua7
-rw-r--r--src/lua_api.c28
2 files changed, 35 insertions, 0 deletions
diff --git a/.lush/scripts/example.lua b/.lush/scripts/example.lua
index ffd04c1..53ee6e8 100644
--- a/.lush/scripts/example.lua
+++ b/.lush/scripts/example.lua
@@ -27,6 +27,7 @@ print("Welcome to Lunar Shell scripting")
-- done like so: example.lua arg1 arg2 ...
-- args can be read using the global args tables
if args ~= nil then
+ print("Printing args:")
for i = 1, #args do
print(args[i])
end
@@ -75,3 +76,9 @@ end
if lush.isWriteable("~/.lush/scripts/example.lua") then
print("example.lua is writeable")
end
+
+-- you can fetch the most recently executed command in history
+print("Most recent history: " .. lush.lastHistory())
+
+-- you can also fetch history at a certain index in the past (1 being most recent)
+print("Most recent history indexed: " .. lush.getHistory(1))
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");
}