2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-01 08:42:04 +00:00

refactor: rewrite appendPath and prependPath in lua

This commit is contained in:
sammyette 2025-06-15 18:48:46 -04:00
parent 7d503750c0
commit b49c51cc7c
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 72 additions and 71 deletions

71
api.go
View File

@ -38,7 +38,6 @@ import (
var exports = map[string]util.LuaExport{
"alias": {hlalias, 2, false},
"appendPath": {hlappendPath, 1, false},
"complete": {hlcomplete, 2, false},
"cwd": {hlcwd, 0, false},
"exec": {hlexec, 1, false},
@ -46,7 +45,6 @@ var exports = map[string]util.LuaExport{
"highlighter": {hlhighlighter, 1, false},
"hinter": {hlhinter, 1, false},
"multiprompt": {hlmultiprompt, 1, false},
"prependPath": {hlprependPath, 1, false},
"inputMode": {hlinputMode, 1, false},
"interval": {hlinterval, 2, false},
"timeout": {hltimeout, 2, false},
@ -261,53 +259,6 @@ func hlalias(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
// appendPath(dir)
// Appends the provided dir to the command path (`$PATH`)
// #param dir string|table Directory (or directories) to append to path
/*
#example
hilbish.appendPath '~/go/bin'
-- Will add ~/go/bin to the command path.
-- Or do multiple:
hilbish.appendPath {
'~/go/bin',
'~/.local/bin'
}
#example
*/
func hlappendPath(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
arg := c.Arg(0)
// check if dir is a table or a string
if arg.Type() == rt.TableType {
util.ForEach(arg.AsTable(), func(k rt.Value, v rt.Value) {
if v.Type() == rt.StringType {
appendPath(v.AsString())
}
})
} else if arg.Type() == rt.StringType {
appendPath(arg.AsString())
} else {
return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
}
return c.Next(), nil
}
func appendPath(dir string) {
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
pathenv := os.Getenv("PATH")
// if dir isnt already in $PATH, add it
if !strings.Contains(pathenv, dir) {
os.Setenv("PATH", pathenv+string(os.PathListSeparator)+dir)
}
}
// exec(cmd)
// Replaces the currently running Hilbish instance with the supplied command.
// This can be used to do an in-place restart.
@ -473,28 +424,6 @@ func hlcomplete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
// prependPath(dir)
// Prepends `dir` to $PATH.
// #param dir string
func hlprependPath(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil {
return nil, err
}
dir, err := c.StringArg(0)
if err != nil {
return nil, err
}
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
pathenv := os.Getenv("PATH")
// if dir isnt already in $PATH, add in
if !strings.Contains(pathenv, dir) {
os.Setenv("PATH", dir+string(os.PathListSeparator)+pathenv)
}
return c.Next(), nil
}
// which(name) -> string
// Checks if `name` is a valid command.
// Will return the path of the binary, or a basename if it's a commander.

View File

@ -18,6 +18,14 @@ local function abbrevHome(path)
return path
end
local function expandHome(path)
if path:sub(1, 1) == '~' then
return fs.join(hilbish.home, path:sub(2))
end
return path
end
local function fmtPrompt(p)
return p:gsub('%%(%w)', function(c)
if c == 'd' then
@ -71,6 +79,70 @@ function hilbish.prompt(p, typ)
end
end
local pathSep = ':'
if hilbish.os.family == 'windows' then
pathSep = ';'
end
local function appendPath(path)
os.setenv('PATH', os.getenv 'PATH' .. pathSep .. expandHome(path))
end
--- appendPath(path)
--- Appends the provided dir to the command path (`$PATH`)
--- @param path string|table Directory (or directories) to append to path
--- #example
--- hilbish.appendPath '~/go/bin'
--- -- Will add ~/go/bin to the command path.
---
--- -- Or do multiple:
--- hilbish.appendPath {
--- '~/go/bin',
--- '~/.local/bin'
--- }
--- #example
function hilbish.appendPath(path)
if type(path) == 'table' then
for _, p in ipairs(path) do
appendPath(p)
end
elseif type(path) == 'string' then
appendPath(path)
else
error('bad argument to appendPath (expected string or table, got ' .. type(path) .. ')')
end
end
local function prependPath(path)
print('prepending', path, expandHome(path))
os.setenv('PATH', expandHome(path) .. pathSep .. os.getenv 'PATH')
end
--- prependPath(path)
--- Prepends the provided dir to the command path (`$PATH`)
--- @param path string|table Directory (or directories) to append to path
--- #example
--- hilbish.prependPath '~/go/bin'
--- -- Will add ~/go/bin to the command path.
---
--- -- Or do multiple:
--- hilbish.prependPath {
--- '~/go/bin',
--- '~/.local/bin'
--- }
--- #example
function hilbish.prependPath(path)
if type(path) == 'table' then
for _, p in ipairs(path) do
prependPath(p)
end
elseif type(path) == 'string' then
prependPath(path)
else
error('bad argument to prependPath (expected string or table, got ' .. type(path) .. ')')
end
end
--- read(prompt) -> input (string)
--- Read input from the user, using Hilbish's line editor/input reader.
--- This is a separate instance from the one Hilbish actually uses.