2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-03-13 18:00:41 +00:00
Hilbish/nature/opts/autocd.lua
TorchedSammy 2e192be2e1
refactor: setup autocd opt in a better way
with the previous commit allowing users to override
hilbish.runner.sh and it being ran by hilbish, the
code for the autocd opt can just override that
function and do the autocd functionality instead
of reimplementing a hybrid runner.

this means that if any other custom runner wants
autocd functionality they can have it with the sh runner
2022-08-30 23:08:22 -04:00

19 lines
454 B
Lua

local fs = require 'fs'
local oldShRunner = hilbish.runner.sh
function hilbish.runner.sh(input)
local res = oldShRunner(input)
if res.exit ~= 0 and hilbish.opts.autocd then
local ok, stat = pcall(fs.stat, res.input)
if ok and stat.isDir then
-- discard here to not append the cd, which will be in history
local _, exitCode, err = hilbish.runner.sh('cd ' .. res.input)
res.exitCode = exitCode
res.err = err
end
end
return res
end