2022-04-23 03:56:57 +00:00
|
|
|
local commander = require 'commander'
|
|
|
|
local fs = require 'fs'
|
|
|
|
local lunacolors = require 'lunacolors'
|
|
|
|
local dirs = require 'nature.dirs'
|
|
|
|
|
2023-01-20 23:07:42 +00:00
|
|
|
commander.register('cdr', function(args, sinks)
|
2022-04-23 03:56:57 +00:00
|
|
|
if not args[1] then
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln(lunacolors.format [[
|
2022-04-23 03:56:57 +00:00
|
|
|
cdr: change directory to one which has been recently visied
|
|
|
|
|
|
|
|
usage: cdr <index>
|
|
|
|
|
2023-07-09 22:43:21 +00:00
|
|
|
to get a list of recent directories, use {green}cdr list{reset}]])
|
2022-04-23 03:56:57 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if args[1] == 'list' then
|
|
|
|
local recentDirs = dirs.recentDirs
|
|
|
|
if #recentDirs == 0 then
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln 'No directories have been visited.'
|
2022-04-23 03:56:57 +00:00
|
|
|
return 1
|
|
|
|
end
|
2023-07-09 22:43:21 +00:00
|
|
|
for idx, d in ipairs(dirs.recentDirs) do
|
|
|
|
if d:find(hilbish.home, 1, true) then d = fs.join('~', d:sub(hilbish.home:len() + 1)) end
|
|
|
|
sinks.out:writeln(lunacolors.format(string.format('{cyan}%d{reset} %s', idx, d)))
|
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local index = tonumber(args[1])
|
|
|
|
if not index then
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln(string.format('Received %s as index, which isn\'t a number.', index))
|
2022-04-23 03:56:57 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if not dirs.recent(index) then
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln(string.format('No recent directory found at index %s.', index))
|
2022-04-23 03:56:57 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
fs.cd(dirs.recent(index))
|
|
|
|
end)
|