mirror of
https://github.com/Hilbis/Hilbish
synced 2025-03-13 18:00:41 +00:00
to write output, you would usually just use the print builtin since commanders are just lua custom commands but this does not consider the fact of pipes or other shell operators being used to redirect or whatever. this adds readable/writable "sinks" which is a type for input or output and is currently only used for commanders but can be used for other hilbish things in the future
29 lines
585 B
Lua
29 lines
585 B
Lua
local bait = require 'bait'
|
|
local commander = require 'commander'
|
|
local fs = require 'fs'
|
|
local dirs = require 'nature.dirs'
|
|
|
|
dirs.old = hilbish.cwd()
|
|
commander.register('cd', function (args, sinks)
|
|
if #args > 1 then
|
|
sinks.out:writeln("cd: too many arguments")
|
|
return 1
|
|
end
|
|
|
|
local path = args[1] and args[1] or hilbish.home
|
|
if path == '-' then
|
|
path = dirs.old
|
|
sinks.out:writeln(path)
|
|
end
|
|
|
|
dirs.setOld(hilbish.cwd())
|
|
dirs.push(path)
|
|
|
|
local ok, err = pcall(function() fs.cd(path) end)
|
|
if not ok then
|
|
sinks.out:writeln(err)
|
|
return 1
|
|
end
|
|
bait.throw('cd', path)
|
|
end)
|