2022-04-23 03:56:57 +00:00
|
|
|
local bait = require 'bait'
|
|
|
|
local commander = require 'commander'
|
|
|
|
local fs = require 'fs'
|
|
|
|
local dirs = require 'nature.dirs'
|
|
|
|
|
|
|
|
dirs.old = hilbish.cwd()
|
2023-01-20 23:07:42 +00:00
|
|
|
commander.register('cd', function (args, sinks)
|
2022-04-23 03:56:57 +00:00
|
|
|
if #args > 1 then
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln("cd: too many arguments")
|
2022-04-23 03:56:57 +00:00
|
|
|
return 1
|
2022-12-28 23:18:04 +00:00
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
|
2022-12-28 23:18:04 +00:00
|
|
|
local path = args[1] and args[1] or hilbish.home
|
|
|
|
if path == '-' then
|
|
|
|
path = dirs.old
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln(path)
|
2022-12-28 23:18:04 +00:00
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
|
2022-12-28 23:18:04 +00:00
|
|
|
dirs.setOld(hilbish.cwd())
|
|
|
|
dirs.push(path)
|
2022-04-23 03:56:57 +00:00
|
|
|
|
2024-07-26 22:08:16 +00:00
|
|
|
local ok, err = pcall(function() fs.cd(fs.abs(path)) end)
|
2022-12-28 23:18:04 +00:00
|
|
|
if not ok then
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln(err)
|
2022-12-28 23:18:04 +00:00
|
|
|
return 1
|
2022-04-23 03:56:57 +00:00
|
|
|
end
|
2022-12-28 23:18:04 +00:00
|
|
|
bait.throw('cd', path)
|
2022-04-23 03:56:57 +00:00
|
|
|
end)
|