mirror of https://github.com/Hilbis/Hilbish
feat: add hilbish.opts and autocd opt
this adds `hilbish.opts`, a table to set simple options akin to shopt or setopt on other shells. this commit specifically also includes the autocd opt, which functions the way you expect it to to set opts, simply do `hilbish.opts.name = val`, where `name` is the opt you want to set and `val` being the opt setting. ie: `hilbish.opts.autocd = true` to turn on autocdinsensitive-tab^2
parent
a8406657f9
commit
c890b86e08
|
@ -6,6 +6,7 @@ package.path = package.path .. ';' .. hilbish.dataDir .. '/?/init.lua'
|
||||||
|
|
||||||
require 'nature.commands'
|
require 'nature.commands'
|
||||||
require 'nature.completions'
|
require 'nature.completions'
|
||||||
|
require 'nature.opts'
|
||||||
|
|
||||||
local shlvl = tonumber(os.getenv 'SHLVL')
|
local shlvl = tonumber(os.getenv 'SHLVL')
|
||||||
if shlvl ~= nil then
|
if shlvl ~= nil then
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
local fs = require 'fs'
|
||||||
|
|
||||||
|
function cdHandle(inp)
|
||||||
|
local input, exit, err = hilbish.runner.lua(inp)
|
||||||
|
|
||||||
|
if not err then
|
||||||
|
return input, exit, err
|
||||||
|
end
|
||||||
|
|
||||||
|
input, exit, err = hilbish.runner.sh(inp)
|
||||||
|
|
||||||
|
if exit ~= 0 and hilbish.opts.autocd then
|
||||||
|
local ok, stat = pcall(fs.stat, input)
|
||||||
|
if ok and stat.isDir then
|
||||||
|
-- discard here to not append the cd, which will be in history
|
||||||
|
_, exit, err = hilbish.runner.sh('cd ' .. input)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return input, exit, err
|
||||||
|
end
|
||||||
|
|
||||||
|
hilbish.runner.setMode(cdHandle)
|
|
@ -0,0 +1,28 @@
|
||||||
|
local opts = {}
|
||||||
|
hilbish.opts = {}
|
||||||
|
|
||||||
|
setmetatable(hilbish.opts, {
|
||||||
|
__newindex = function(_, k, v)
|
||||||
|
if opts[k] == nil then
|
||||||
|
error(string.format('opt %s does not exist', k))
|
||||||
|
end
|
||||||
|
|
||||||
|
opts[k] = v
|
||||||
|
end,
|
||||||
|
__index = function(_, k)
|
||||||
|
return opts[k]
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
local function setupOpt(name, default)
|
||||||
|
opts[name] = default
|
||||||
|
require('nature.opts.' .. name)
|
||||||
|
end
|
||||||
|
|
||||||
|
local defaultOpts = {
|
||||||
|
autocd = false
|
||||||
|
}
|
||||||
|
|
||||||
|
for optsName, default in pairs(defaultOpts) do
|
||||||
|
setupOpt(optsName, default)
|
||||||
|
end
|
Loading…
Reference in New Issue