From a8475cfa67cdc459f1c9dd7c3a7e0b1ab7018605 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sat, 9 Jul 2022 16:49:56 -0400 Subject: [PATCH] refactor: handle history in lua this also introduces a new opt: history if it is false, command history won't get added --- exec.go | 6 +----- main.go | 5 ----- nature/init.lua | 1 + nature/opts/history.lua | 5 +++++ nature/opts/init.lua | 3 ++- 5 files changed, 9 insertions(+), 11 deletions(-) create mode 100644 nature/opts/history.lua diff --git a/exec.go b/exec.go index cc5016a..e1862ed 100644 --- a/exec.go +++ b/exec.go @@ -540,13 +540,9 @@ func splitInput(input string) ([]string, string) { } func cmdFinish(code uint8, cmdstr string, private bool) { - // if input has space at the beginning, dont put in history - if interactive && !private { - handleHistory(cmdstr) - } util.SetField(l, hshMod, "exitCode", rt.IntValue(int64(code)), "Exit code of last exected command") // using AsValue (to convert to lua type) on an interface which is an int // results in it being unknown in lua .... ???? // so we allow the hook handler to take lua runtime Values - hooks.Em.Emit("command.exit", rt.IntValue(int64(code)), cmdstr) + hooks.Em.Emit("command.exit", rt.IntValue(int64(code)), cmdstr, private) } diff --git a/main.go b/main.go index 99f333a..0664cc1 100644 --- a/main.go +++ b/main.go @@ -268,11 +268,6 @@ func fmtPrompt(prompt string) string { return nprompt } -func handleHistory(cmd string) { - lr.AddHistory(cmd) - // TODO: load history again (history shared between sessions like this ye) -} - func removeDupes(slice []string) []string { all := make(map[string]bool) newSlice := []string{} diff --git a/nature/init.lua b/nature/init.lua index df31d8d..845ade0 100644 --- a/nature/init.lua +++ b/nature/init.lua @@ -10,6 +10,7 @@ require 'nature.completions' require 'nature.opts' require 'nature.vim' require 'nature.runner' +require 'nature.history' local shlvl = tonumber(os.getenv 'SHLVL') if shlvl ~= nil then diff --git a/nature/opts/history.lua b/nature/opts/history.lua new file mode 100644 index 0000000..f7ab1d7 --- /dev/null +++ b/nature/opts/history.lua @@ -0,0 +1,5 @@ +local bait = require 'bait' + +bait.catch('command.exit', function(_, cmd, priv) + if not priv and hilbish.opts.history then hilbish.history.add(cmd) end +end) diff --git a/nature/opts/init.lua b/nature/opts/init.lua index 59d3cbd..3b39834 100644 --- a/nature/opts/init.lua +++ b/nature/opts/init.lua @@ -20,7 +20,8 @@ local function setupOpt(name, default) end local defaultOpts = { - autocd = false + autocd = false, + history = true } for optsName, default in pairs(defaultOpts) do