Skip to content

Commit

Permalink
feat: add cdr command
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TorchedSammy committed Oct 17, 2021
1 parent d51ae7d commit 7615e56
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 <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
bait.catch('command.not-found', function(cmd)
print(string.format('hilbish: %s not found', cmd))
Expand Down

0 comments on commit 7615e56

Please sign in to comment.