From db437905e07ea268e879000dddf97179e7b1f9e1 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 1 May 2022 00:49:59 -0400 Subject: [PATCH] fix: implicitly expand tilde on args to all fs functions --- complete.go | 4 ++-- golibs/fs/fs.go | 12 ++++++++---- main.go | 9 ++------- util/util.go | 9 +++++++++ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/complete.go b/complete.go index dc4c5ce..01a8137 100644 --- a/complete.go +++ b/complete.go @@ -24,7 +24,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) { fileCompletions, filePref := matchPath(query) if len(fileCompletions) != 0 { for _, f := range fileCompletions { - fullPath, _ := filepath.Abs(expandHome(query + strings.TrimPrefix(f, filePref))) + fullPath, _ := filepath.Abs(util.ExpandHome(query + strings.TrimPrefix(f, filePref))) if err := findExecutable(fullPath, false, true); err != nil { continue } @@ -71,7 +71,7 @@ func matchPath(query string) ([]string, string) { var entries []string var baseName string - path, _ := filepath.Abs(expandHome(filepath.Dir(query))) + path, _ := filepath.Abs(util.ExpandHome(filepath.Dir(query))) if string(query) == "" { // filepath base below would give us "." // which would cause a match of only dotfiles diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index eda1671..950e966 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -30,7 +30,7 @@ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { util.Document(mod, `The fs module provides easy and simple access to filesystem functions and other things, and acts an -addition to the Lua standard library's I/O and fs functions.`) +addition to the Lua standard library's I/O and filesystem functions.`) return rt.TableValue(mod), nil } @@ -46,8 +46,9 @@ func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err != nil { return nil, err } + path = util.ExpandHome(strings.TrimSpace(path)) - err = os.Chdir(strings.TrimSpace(path)) + err = os.Chdir(path) if err != nil { return nil, err } @@ -63,7 +64,7 @@ func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.CheckNArgs(2); err != nil { return nil, err } - dirname, err := c.StringArg(0) + path, err := c.StringArg(0) if err != nil { return nil, err } @@ -71,7 +72,7 @@ func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err != nil { return nil, err } - path := strings.TrimSpace(dirname) + path = util.ExpandHome(strings.TrimSpace(path)) if recursive { err = os.MkdirAll(path, 0744) @@ -96,6 +97,7 @@ func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err != nil { return nil, err } + path = util.ExpandHome(path) pathinfo, err := os.Stat(path) if err != nil { @@ -122,6 +124,7 @@ func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err != nil { return nil, err } + dir = util.ExpandHome(dir) names := rt.NewTable() dirEntries, err := os.ReadDir(dir) @@ -143,6 +146,7 @@ func fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err != nil { return nil, err } + path = util.ExpandHome(path) abspath, err := filepath.Abs(path) if err != nil { diff --git a/main.go b/main.go index ac79487..c0fce06 100644 --- a/main.go +++ b/main.go @@ -56,13 +56,13 @@ func main() { defaultConfDir = filepath.Join(confDir, "hilbish") } else { // else do ~ substitution - defaultConfDir = filepath.Join(expandHome(defaultConfDir), "hilbish") + defaultConfDir = filepath.Join(util.ExpandHome(defaultConfDir), "hilbish") } defaultConfPath = filepath.Join(defaultConfDir, "init.lua") if defaultHistDir == "" { defaultHistDir = filepath.Join(userDataDir, "hilbish") } else { - defaultHistDir = filepath.Join(expandHome(defaultHistDir), "hilbish") + defaultHistDir = filepath.Join(util.ExpandHome(defaultHistDir), "hilbish") } defaultHistPath = filepath.Join(defaultHistDir, ".hilbish-history") helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags") @@ -273,11 +273,6 @@ func handleHistory(cmd string) { // TODO: load history again (history shared between sessions like this ye) } -func expandHome(path string) string { - homedir := curuser.HomeDir - return strings.Replace(path, "~", homedir, 1) -} - func removeDupes(slice []string) []string { all := make(map[string]bool) newSlice := []string{} diff --git a/util/util.go b/util/util.go index 6b5860e..4bfecc9 100644 --- a/util/util.go +++ b/util/util.go @@ -3,7 +3,9 @@ package util import ( "bufio" "io" + "strings" "os" + "os/user" rt "github.com/arnodel/golua/runtime" ) @@ -150,3 +152,10 @@ func ForEach(tbl *rt.Table, cb func(key rt.Value, val rt.Value)) { cb(key, val) } } + +func ExpandHome(path string) string { + curuser, _ := user.Current() + homedir := curuser.HomeDir + + return strings.Replace(path, "~", homedir, 1) +}