fix: implicitly expand tilde on args to all fs functions

insensitive-tab^2
TorchedSammy 2022-05-01 00:49:59 -04:00
parent c890b86e08
commit db437905e0
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 21 additions and 13 deletions

View File

@ -24,7 +24,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
fileCompletions, filePref := matchPath(query) fileCompletions, filePref := matchPath(query)
if len(fileCompletions) != 0 { if len(fileCompletions) != 0 {
for _, f := range fileCompletions { 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 { if err := findExecutable(fullPath, false, true); err != nil {
continue continue
} }
@ -71,7 +71,7 @@ func matchPath(query string) ([]string, string) {
var entries []string var entries []string
var baseName string var baseName string
path, _ := filepath.Abs(expandHome(filepath.Dir(query))) path, _ := filepath.Abs(util.ExpandHome(filepath.Dir(query)))
if string(query) == "" { if string(query) == "" {
// filepath base below would give us "." // filepath base below would give us "."
// which would cause a match of only dotfiles // which would cause a match of only dotfiles

View File

@ -30,7 +30,7 @@ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
util.Document(mod, `The fs module provides easy and simple access to util.Document(mod, `The fs module provides easy and simple access to
filesystem functions and other things, and acts an 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 return rt.TableValue(mod), nil
} }
@ -46,8 +46,9 @@ func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
path = util.ExpandHome(strings.TrimSpace(path))
err = os.Chdir(strings.TrimSpace(path)) err = os.Chdir(path)
if err != nil { if err != nil {
return nil, err 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 { if err := c.CheckNArgs(2); err != nil {
return nil, err return nil, err
} }
dirname, err := c.StringArg(0) path, err := c.StringArg(0)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -71,7 +72,7 @@ func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
path := strings.TrimSpace(dirname) path = util.ExpandHome(strings.TrimSpace(path))
if recursive { if recursive {
err = os.MkdirAll(path, 0744) err = os.MkdirAll(path, 0744)
@ -96,6 +97,7 @@ func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
path = util.ExpandHome(path)
pathinfo, err := os.Stat(path) pathinfo, err := os.Stat(path)
if err != nil { if err != nil {
@ -122,6 +124,7 @@ func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
dir = util.ExpandHome(dir)
names := rt.NewTable() names := rt.NewTable()
dirEntries, err := os.ReadDir(dir) dirEntries, err := os.ReadDir(dir)
@ -143,6 +146,7 @@ func fabs(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
path = util.ExpandHome(path)
abspath, err := filepath.Abs(path) abspath, err := filepath.Abs(path)
if err != nil { if err != nil {

View File

@ -56,13 +56,13 @@ func main() {
defaultConfDir = filepath.Join(confDir, "hilbish") defaultConfDir = filepath.Join(confDir, "hilbish")
} else { } else {
// else do ~ substitution // else do ~ substitution
defaultConfDir = filepath.Join(expandHome(defaultConfDir), "hilbish") defaultConfDir = filepath.Join(util.ExpandHome(defaultConfDir), "hilbish")
} }
defaultConfPath = filepath.Join(defaultConfDir, "init.lua") defaultConfPath = filepath.Join(defaultConfDir, "init.lua")
if defaultHistDir == "" { if defaultHistDir == "" {
defaultHistDir = filepath.Join(userDataDir, "hilbish") defaultHistDir = filepath.Join(userDataDir, "hilbish")
} else { } else {
defaultHistDir = filepath.Join(expandHome(defaultHistDir), "hilbish") defaultHistDir = filepath.Join(util.ExpandHome(defaultHistDir), "hilbish")
} }
defaultHistPath = filepath.Join(defaultHistDir, ".hilbish-history") defaultHistPath = filepath.Join(defaultHistDir, ".hilbish-history")
helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags") 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) // 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 { func removeDupes(slice []string) []string {
all := make(map[string]bool) all := make(map[string]bool)
newSlice := []string{} newSlice := []string{}

View File

@ -3,7 +3,9 @@ package util
import ( import (
"bufio" "bufio"
"io" "io"
"strings"
"os" "os"
"os/user"
rt "github.com/arnodel/golua/runtime" 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) cb(key, val)
} }
} }
func ExpandHome(path string) string {
curuser, _ := user.Current()
homedir := curuser.HomeDir
return strings.Replace(path, "~", homedir, 1)
}