From 7615e56ffd92217c1f6110d921e09a83f52ca64b Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 17 Oct 2021 18:39:26 -0400 Subject: [PATCH] feat: add cdr command the cdr command will change to directory from a list of 10 most recently moved to directories. this only works for the interactive cd command, and not the fs.cd function. you can find the list of recent directories with `cdr list`. usage: `cdr ` the `cdr help` command also gives this bit of info --- preload.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/preload.lua b/preload.lua index c803a5b..023ae50 100644 --- a/preload.lua +++ b/preload.lua @@ -10,6 +10,7 @@ local shlvl = tonumber(os.getenv 'SHLVL') if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end -- Builtins +local recentDirs = {} commander.register('cd', function (args) if #args > 0 then local path = table.concat(args, ' '):gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv) @@ -27,6 +28,11 @@ commander.register('cd', function (args) return 1 end bait.throw('cd', path) + + -- add to table of recent dirs + table.insert(recentDirs, 1, path) + recentDirs[11] = nil + return end fs.cd(hilbish.home) @@ -120,6 +126,41 @@ do end) end +commander.register('cdr', function(args) + if not args[1] then + print(lunacolors.format [[ +cdr: change directory to one which has been recently visied + +usage: cdr + +to get a list of recent directories, use {green}{underline}cdr list{reset}]]) + return + end + + if args[1] == 'list' then + if #recentDirs == 0 then + print 'No directories have been visited.' + return 1 + end + print(table.concat(recentDirs, '\n')) + return + end + + local index = tonumber(args[1]) + if not index then + print(string.format('received %s as index, which isn\'t a number', index)) + return 1 + end + + if not recentDirs[index] then + print(string.format('no recent directory found at index %s', index)) + return 1 + end + + fs.cd(recentDirs[index]) + return +end) + -- Hook handles bait.catch('command.not-found', function(cmd) print(string.format('hilbish: %s not found', cmd))