feat: add cdr command

the cdr command will change to <index> 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 <index>`
the `cdr help` command also gives this bit of info
pull/78/head
sammyette 2021-10-17 18:39:26 -04:00
parent d51ae7d310
commit 7615e56ffd
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
1 changed files with 41 additions and 0 deletions

View File

@ -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 if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end
-- Builtins -- Builtins
local recentDirs = {}
commander.register('cd', function (args) commander.register('cd', function (args)
if #args > 0 then if #args > 0 then
local path = table.concat(args, ' '):gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv) local path = table.concat(args, ' '):gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
@ -27,6 +28,11 @@ commander.register('cd', function (args)
return 1 return 1
end end
bait.throw('cd', path) bait.throw('cd', path)
-- add to table of recent dirs
table.insert(recentDirs, 1, path)
recentDirs[11] = nil
return return
end end
fs.cd(hilbish.home) fs.cd(hilbish.home)
@ -120,6 +126,41 @@ do
end) end)
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 <index>
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 -- Hook handles
bait.catch('command.not-found', function(cmd) bait.catch('command.not-found', function(cmd)
print(string.format('hilbish: %s not found', cmd)) print(string.format('hilbish: %s not found', cmd))