From 22d8a615447a2a9e6d486b3c0f5f80d946ad56f5 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Thu, 27 Jan 2022 17:02:21 -0400 Subject: [PATCH] feat: add hilbish.history interface (closes #85) --- api.go | 5 +++++ rl.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ rl_hilbiline.go | 37 ++++++++++++++++++++++++++++++ rl_readline-go.go | 37 ++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) diff --git a/api.go b/api.go index 1e85036..338b7cf 100644 --- a/api.go +++ b/api.go @@ -84,6 +84,11 @@ The nice lil shell for {blue}Lua{reset} fanatics! util.Document(L, aliasesModule, "Alias inferface for Hilbish.") L.SetField(mod, "aliases", aliasesModule) + // hilbish.history table + historyModule := lr.Loader(L) + util.Document(L, historyModule, "History interface for Hilbish.") + L.SetField(mod, "history", historyModule) + L.Push(mod) return 1 diff --git a/rl.go b/rl.go index 209b644..4072944 100644 --- a/rl.go +++ b/rl.go @@ -205,3 +205,60 @@ func (lr *lineReader) ClearInput() { func (lr *lineReader) Resize() { readline.Resize() } + +// lua module +func (lr *lineReader) Loader(L *lua.LState) *lua.LTable { + lrLua := map[string]lua.LGFunction{ + "add": lr.luaAddHistory, + "all": lr.luaAllHistory, + "clear": lr.luaClearHistory, + "get": lr.luaGetHistory, + "size": lr.luaSize, + } + + mod := L.SetFuncs(L.NewTable(), lrLua) + + return mod +} + +func (lr *lineReader) luaAddHistory(l *lua.LState) int { + cmd := l.CheckString(1) + lr.AddHistory(cmd) + + return 0 +} + +func (lr *lineReader) luaSize(l *lua.LState) int { + l.Push(lua.LNumber(readline.HistorySize())) + + return 1 +} + +func (lr *lineReader) luaGetHistory(l *lua.LState) int { + idx := l.CheckInt(1) + cmd := readline.GetHistory(idx) + l.Push(lua.LString(cmd)) + + return 1 +} + +func (lr *lineReader) luaAllHistory(l *lua.LState) int { + tbl := l.NewTable() + size := readline.HistorySize() + + for i := 0; i < size; i++ { + cmd := readline.GetHistory(i) + tbl.Append(lua.LString(cmd)) + } + + l.Push(tbl) + + return 1 +} + +func (lr *lineReader) luaClearHistory(l *lua.LState) int { + readline.ClearHistory() + readline.SaveHistory(defaultHistPath) + + return 0 +} diff --git a/rl_hilbiline.go b/rl_hilbiline.go index 1095711..5370cbb 100644 --- a/rl_hilbiline.go +++ b/rl_hilbiline.go @@ -41,3 +41,40 @@ func (lr *lineReader) Resize() { return } +// lua module +func (lr *lineReader) Loader() *lua.LTable { + lrLua := map[string]lua.LGFunction{ + "add": lr.luaAddHistory, + "all": lr.luaAllHistory, + "clear": lr.luaClearHistory, + "get": lr.luaGetHistory, + "size": lr.luaSize, + } + + mod := l.SetFuncs(l.NewTable(), lrLua) + + return mod +} + +func (lr *lineReader) luaAddHistory(l *lua.LState) int { + cmd := l.CheckString(1) + lr.AddHistory(cmd) + + return 0 +} + +func (lr *lineReader) luaSize(l *lua.LState) int { + return 0 +} + +func (lr *lineReader) luaGetHistory(l *lua.LState) int { + return 0 +} + +func (lr *lineReader) luaAllHistory(l *lua.LState) int { + return 0 +} + +func (lr *lineReader) luaClearHistory(l *lua.LState) int { + return 0 +} diff --git a/rl_readline-go.go b/rl_readline-go.go index 24a1550..f22eb21 100644 --- a/rl_readline-go.go +++ b/rl_readline-go.go @@ -63,3 +63,40 @@ func (lr *lineReader) Resize() { return } +// lua module +func (lr *lineReader) Loader() *lua.LTable { + lrLua := map[string]lua.LGFunction{ + "add": lr.luaAddHistory, + "all": lr.luaAllHistory, + "clear": lr.luaClearHistory, + "get": lr.luaGetHistory, + "size": lr.luaSize, + } + + mod := l.SetFuncs(l.NewTable(), lrLua) + + return mod +} + +func (lr *lineReader) luaAddHistory(l *lua.LState) int { + cmd := l.CheckString(1) + lr.AddHistory(cmd) + + return 0 +} + +func (lr *lineReader) luaSize(l *lua.LState) int { + return 0 +} + +func (lr *lineReader) luaGetHistory(l *lua.LState) int { + return 0 +} + +func (lr *lineReader) luaAllHistory(l *lua.LState) int { + return 0 +} + +func (lr *lineReader) luaClearHistory(l *lua.LState) int { + return 0 +}