diff --git a/arrutil/collection_gte118.go b/arrutil/collection_gte118.go index f580c599e..fe406caf1 100644 --- a/arrutil/collection_gte118.go +++ b/arrutil/collection_gte118.go @@ -2,12 +2,14 @@ package arrutil // type MapFn func(obj T) (target V, find bool) -// Map an object list [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...] +// Map a list to new list +// +// eg: mapping [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...] func Map[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V { flatArr := make([]V, 0, len(list)) for _, obj := range list { - if target, find := mapFn(obj); find { + if target, ok := mapFn(obj); ok { flatArr = append(flatArr, target) } } diff --git a/fsutil/info_windows.go b/fsutil/info_windows.go index f66c5f180..dc21ebe76 100644 --- a/fsutil/info_windows.go +++ b/fsutil/info_windows.go @@ -1,6 +1,8 @@ package fsutil import ( + "path/filepath" + "github.com/gookit/goutil/internal/comfunc" ) diff --git a/internal/comfunc/sysfunc.go b/internal/comfunc/sysfunc.go index 961c95467..aef90e947 100644 --- a/internal/comfunc/sysfunc.go +++ b/internal/comfunc/sysfunc.go @@ -78,23 +78,26 @@ var curShell string // // eg "/bin/zsh" "/bin/bash". // if onlyName=true, will return "zsh", "bash" -func CurrentShell(onlyName bool) (path string) { +func CurrentShell(onlyName bool) (binPath string) { var err error if curShell == "" { - path, err = ShellExec("echo $SHELL") - if err != nil { - return "" + binPath = os.Getenv("SHELL") + if len(binPath) == 0 { + binPath, err = ShellExec("echo $SHELL") + if err != nil { + return "" + } } - path = strings.TrimSpace(path) + binPath = strings.TrimSpace(binPath) // cache result - curShell = path + curShell = binPath } else { - path = curShell + binPath = curShell } - if onlyName && len(path) > 0 { - path = filepath.Base(path) + if onlyName && len(binPath) > 0 { + binPath = filepath.Base(binPath) } return } diff --git a/structs/value.go b/structs/value.go index c6312f2eb..52ae940c9 100644 --- a/structs/value.go +++ b/structs/value.go @@ -107,7 +107,7 @@ func (v *Value) Strings() (ss []string) { return } -// SplitToStrings split string value to strings +// SplitToStrings split string value to strings. sep default is comma(,) func (v *Value) SplitToStrings(sep ...string) (ss []string) { if v.V == nil { return @@ -119,7 +119,7 @@ func (v *Value) SplitToStrings(sep ...string) (ss []string) { return } -// SplitToInts split string value to []int +// SplitToInts split string value to []int. sep default is comma(,) func (v *Value) SplitToInts(sep ...string) (ss []int) { if v.V == nil { return diff --git a/sysutil/sysutil.go b/sysutil/sysutil.go index 88c5ad1b0..9e3b176e6 100644 --- a/sysutil/sysutil.go +++ b/sysutil/sysutil.go @@ -15,8 +15,12 @@ func Workdir() string { // BinDir get func BinDir() string { - binFile := os.Args[0] - return path.Dir(binFile) + return path.Dir(os.Args[0]) +} + +// BinName get +func BinName() string { + return path.Base(os.Args[0]) } // BinFile get